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