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