backup
[platform/upstream/libzypp.git] / tools / NameReqPrv.cc
1 #define INCLUDE_TESTSETUP_WITHOUT_BOOST
2 #include "zypp/../tests/lib/TestSetup.h"
3 #undef  INCLUDE_TESTSETUP_WITHOUT_BOOST
4
5 #include <zypp/PoolQuery.h>
6
7 static std::string appname( "NameReqPrv" );
8
9 #define message cerr
10 using std::flush;
11
12 int errexit( const std::string & msg_r = std::string(), int exit_r = 100 )
13 {
14   if ( ! msg_r.empty() )
15   {
16     cerr << endl << msg_r << endl << endl;
17   }
18   return exit_r;
19 }
20
21 int usage( const std::string & msg_r = std::string(), int exit_r = 100 )
22 {
23   if ( ! msg_r.empty() )
24   {
25     cerr << endl << msg_r << endl << endl;
26   }
27   cerr << "Usage: " << appname << " [--root ROOTDIR] [OPTIONS] NAME... [[OPTIONS] NAME...]..." << endl;
28   cerr << "  Load all enabled repositories (no refresh) and search for" << endl;
29   cerr << "  occurrences of NAME (substring) in package names, provides or" << endl;
30   cerr << "  requires." << endl;
31   cerr << "  --root   Load repos from the system located below ROOTDIR. If ROOTDIR" << endl;
32   cerr << "           denotes a sover testcase, the testcase is loaded." << endl;
33   cerr << "  -i/-I    turn on/off case insensitive search (default on)" << endl;
34   cerr << "  -n/-N    turn on/off looking for names       (default on)" << endl;
35   cerr << "  -p/-P    turn on/off looking for provides    (default on)" << endl;
36   cerr << "  -r/-R    turn on/off looking for requires    (default off)" << endl;
37   cerr << "  -a       short for -n -p -r" << endl;
38   cerr << "  -A       short for -n -P -R" << endl;
39   cerr << "TODO: Waiting for PoolQuery::allMatches switch and need to beautify output." << endl;
40   cerr << "" << endl;
41   return exit_r;
42 }
43
44 /******************************************************************
45 **
46 **      FUNCTION NAME : main
47 **      FUNCTION TYPE : int
48 */
49 int main( int argc, char * argv[] )
50 {
51   INT << "===[START]==========================================" << endl;
52   appname = Pathname::basename( argv[0] );
53   --argc,++argv;
54
55   if ( ! argc )
56   {
57     return usage();
58   }
59
60   ///////////////////////////////////////////////////////////////////
61
62   ZConfig::instance();
63   Pathname sysRoot("/");
64   sat::Pool satpool( sat::Pool::instance() );
65
66   if ( (*argv) == std::string("--root") )
67   {
68     --argc,++argv;
69     if ( ! argc )
70       return errexit("--root requires an argument.");
71
72     if ( ! PathInfo( *argv ).isDir() )
73       return errexit("--root requires a directory.");
74
75     sysRoot = *argv;
76     --argc,++argv;
77   }
78
79   if ( TestSetup::isTestcase( sysRoot ) )
80   {
81     message << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
82     TestSetup test;
83     test.loadTestcaseRepos( sysRoot );
84   }
85   else if ( TestSetup::isTestSetup( sysRoot ) )
86   {
87     message << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
88     TestSetup test( sysRoot );
89     test.loadRepos();
90   }
91   else
92   {
93     // a system
94     message << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
95     if ( 1 )
96     {
97       message << "*** load target '" << Repository::systemRepoAlias() << "'\t" << endl;
98       getZYpp()->initializeTarget( sysRoot );
99       getZYpp()->target()->load();
100       message << satpool.systemRepo() << endl;
101     }
102
103     if ( 1 )
104     {
105       RepoManager repoManager( sysRoot );
106       RepoInfoList repos = repoManager.knownRepositories();
107       for_( it, repos.begin(), repos.end() )
108       {
109         RepoInfo & nrepo( *it );
110
111         if ( ! nrepo.enabled() )
112           continue;
113
114         if ( ! repoManager.isCached( nrepo ) )
115         {
116           message << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
117           continue;
118         }
119
120         message << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
121         try
122         {
123           repoManager.loadFromCache( nrepo );
124           message << satpool.reposFind( nrepo.alias() ) << endl;
125         }
126         catch ( const Exception & exp )
127         {
128           message << exp.asString() + "\n" + exp.historyAsString() << endl;
129           message << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
130           continue;
131         }
132       }
133     }
134   }
135
136   ///////////////////////////////////////////////////////////////////
137
138   bool ignorecase( true );
139   bool names     ( true );
140   bool provides  ( true );
141   bool requires  ( false );
142
143   for ( ; argc; --argc,++argv )
144   {
145     if ( (*argv)[0] == '-' )
146     {
147       switch ( (*argv)[1] )
148       {
149         case 'a':  names =      true,   requires = provides =   true;   break;
150         case 'A':  names =      true,   requires = provides =   false;  break;
151         case 'i': ignorecase =  true;   break;
152         case 'I': ignorecase =  false;  break;
153         case 'n': names =       true;   break;
154         case 'N': names =       false;  break;
155         case 'r': requires =    true;   break;
156         case 'R': requires =    false;  break;
157         case 'p': provides =    true;   break;
158         case 'P': provides =    false;  break;
159       }
160       continue;
161     }
162
163     PoolQuery q;
164     q.addString( *argv );
165     q.setMatchSubstring();
166     q.setCaseSensitive( ! ignorecase );
167
168     if ( names )
169       q.addAttribute( sat::SolvAttr::name );
170     if ( provides )
171       q.addDependency( sat::SolvAttr::provides );
172     if ( requires )
173       q.addDependency( sat::SolvAttr::requires );
174
175     message << *argv << " [" << (ignorecase?'i':'_') << (names?'n':'_') << (requires?'r':'_') << (provides?'p':'_') << "] {" << endl;
176
177     for_( it, q.begin(), q.end() )
178     {
179       message << "  " << dump(it) << endl;
180     }
181
182     message << "}" << endl;
183   }
184
185   INT << "===[END]============================================" << endl << endl;
186   return 0;
187 }