ca22da356a76879f52df965d824b6e525f394ee3
[platform/upstream/libzypp.git] / tools / zypp-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 #include <zypp/ResObjects.h>
8
9 static std::string appname( "NameReqPrv" );
10
11 #define message cout
12 using std::flush;
13
14 int errexit( const std::string & msg_r = std::string(), int exit_r = 100 )
15 {
16   if ( ! msg_r.empty() )
17   {
18     cerr << endl << msg_r << endl << endl;
19   }
20   return exit_r;
21 }
22
23 int usage( const std::string & msg_r = std::string(), int exit_r = 100 )
24 {
25   if ( ! msg_r.empty() )
26   {
27     cerr << endl << msg_r << endl << endl;
28   }
29   cerr << "Usage: " << appname << " [--root ROOTDIR] [OPTIONS] NAME... [[OPTIONS] NAME...]..." << endl;
30   cerr << "  Load all enabled repositories (no refresh) and search for" << endl;
31   cerr << "  occurrences of NAME (regex) in package names or dependencies" << 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 << "  --installed Process installed packages only." << endl;
35   cerr << "  -i/-I    turn on/off case insensitive search (default on)" << endl;
36   cerr << "  -n/-N    turn on/off looking for names       (default on)" << endl;
37   cerr << "  -p/-P    turn on/off looking for provides    (default off)" << endl;
38   cerr << "  -r/-R    turn on/off looking for requires    (default off)" << endl;
39   cerr << "  -c/-C    turn on/off looking for conflicts   (default off)" << endl;
40   cerr << "  -o/-O    turn on/off looking for obsoletes   (default off)" << endl;
41   cerr << "  -m/-M    turn on/off looking for recommends  (default off)" << endl;
42   cerr << "  -s/-S    turn on/off looking for supplements (default off)" << endl;
43   cerr << "  -a       short for -n -p -r" << endl;
44   cerr << "  -A       short for -n -P -R" << endl;
45   cerr << "  -D <pkg> dump dependencies of <pkg>" << endl;
46   cerr << "" << endl;
47   return exit_r;
48 }
49
50 void tableOut( const std::string & s1 = std::string(),
51                const std::string & s2 = std::string(),
52                const std::string & s3 = std::string(),
53                const std::string & s4 = std::string(),
54                const std::string & s5 = std::string() )
55 {
56   message << "  ";
57 #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() )
58 #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() )
59   TABER( 1 ); TABEL( 2 ); TABEL( 3 ); TABEL( 4 ); TABEL( 5 );
60 #undef TABEL
61   message << endl;
62 }
63
64
65 ///////////////////////////////////////////////////////////////////
66
67 void dDump( const std::string & spec_r )
68 {
69   message << "DUMP " << spec_r << " {";
70
71   sat::WhatProvides q( Capability::guessPackageSpec( spec_r ) );
72   if ( q.empty() )
73   {
74     message << "}" << endl;
75     return;
76   }
77
78   for ( const auto & el : q )
79   {
80     message << endl << "==============================" << endl << dump(el);
81     if ( isKind<Product>(el) )
82     {
83       message << endl << "REPLACES: " << make<Product>(el)->replacedProducts();
84     }
85     else if ( isKind<Pattern>(el) )
86     {
87       message << endl << "CONTENT: " << make<Pattern>(el)->contents();
88     }
89   }
90   message << endl << "}" << endl;
91 }
92
93 /******************************************************************
94 **
95 **      FUNCTION NAME : main
96 **      FUNCTION TYPE : int
97 */
98 int main( int argc, char * argv[] )
99 {
100   INT << "===[START]==========================================" << endl;
101   appname = Pathname::basename( argv[0] );
102   --argc,++argv;
103
104   if ( ! argc )
105   {
106     return usage();
107   }
108
109   ///////////////////////////////////////////////////////////////////
110
111   ZConfig::instance();
112   Pathname sysRoot("/");
113   sat::Pool satpool( sat::Pool::instance() );
114
115   if ( argc && (*argv) == std::string("--root") )
116   {
117     --argc,++argv;
118     if ( ! argc )
119       return errexit("--root requires an argument.");
120
121     if ( ! PathInfo( *argv ).isDir() )
122       return errexit("--root requires a directory.");
123
124     sysRoot = *argv;
125     --argc,++argv;
126   }
127
128   bool onlyInstalled( false );
129   if ( argc && (*argv) == std::string("--installed") )
130   {
131     --argc,++argv;
132     onlyInstalled = true;
133   }
134
135   if ( TestSetup::isTestcase( sysRoot ) )
136   {
137     message << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
138     TestSetup test;
139     test.loadTestcaseRepos( sysRoot );
140     dumpRange( message, satpool.reposBegin(), satpool.reposEnd() ) << endl;
141   }
142   else if ( TestSetup::isTestSetup( sysRoot ) )
143   {
144     message << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
145     TestSetup test( sysRoot, Arch_x86_64 );
146     test.loadRepos();
147     dumpRange( message, satpool.reposBegin(), satpool.reposEnd() ) << endl;
148   }
149   else
150   {
151     // a system
152     message << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
153     if ( true )
154     {
155       message << "*** load target '" << Repository::systemRepoAlias() << "'\t" << endl;
156       getZYpp()->initializeTarget( sysRoot );
157       getZYpp()->target()->load();
158       message << satpool.systemRepo() << endl;
159     }
160
161     if ( !onlyInstalled )
162     {
163       RepoManager repoManager( sysRoot );
164       RepoInfoList repos = repoManager.knownRepositories();
165       for_( it, repos.begin(), repos.end() )
166       {
167         RepoInfo & nrepo( *it );
168
169         if ( ! nrepo.enabled() )
170           continue;
171
172         if ( ! repoManager.isCached( nrepo ) )
173         {
174           message << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
175           continue;
176         }
177
178         message << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
179         try
180         {
181           repoManager.loadFromCache( nrepo );
182           message << satpool.reposFind( nrepo.alias() ) << endl;
183         }
184         catch ( const Exception & exp )
185         {
186           message << exp.asString() + "\n" + exp.historyAsString() << endl;
187           message << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
188           continue;
189         }
190       }
191     }
192   }
193
194   ///////////////////////////////////////////////////////////////////
195
196   bool ignorecase       ( true );
197   bool names            ( true );
198   bool provides         ( false );
199   bool requires         ( false );
200   bool conflicts        ( false );
201   bool obsoletes        ( false );
202   bool recommends       ( false );
203   bool supplements      ( false );
204
205   for ( ; argc; --argc,++argv )
206   {
207     if ( (*argv)[0] == '-' )
208     {
209       switch ( (*argv)[1] )
210       {
211         case 'a': names =       true,   requires = provides =   true;   break;
212         case 'A': names =       true,   requires = provides =   false;  break;
213         case 'D':
214           if ( argc > 1 )
215           {
216             --argc,++argv;
217             dDump( *argv );
218           }
219           else
220             return errexit("-D <pkgspec> requires an argument.");
221           break;
222         case 'i': ignorecase =  true;   break;
223         case 'I': ignorecase =  false;  break;
224         case 'n': names =       true;   break;
225         case 'N': names =       false;  break;
226         case 'r': requires =    true;   break;
227         case 'R': requires =    false;  break;
228         case 'p': provides =    true;   break;
229         case 'P': provides =    false;  break;
230         case 'c': conflicts =   true;   break;
231         case 'C': conflicts =   false;  break;
232         case 'o': obsoletes =   true;   break;
233         case 'O': obsoletes =   false;  break;
234         case 'm': recommends =  true;   break;
235         case 'M': recommends =  false;  break;
236         case 's': supplements = true;   break;
237         case 'S': supplements = false;  break;
238       }
239       continue;
240     }
241
242     PoolQuery q;
243     if ( onlyInstalled )
244       q.setInstalledOnly();
245     std::string qstr( *argv );
246
247     if ( *argv == ResKind::product )
248     {
249       q.addKind( ResKind::product );
250     }
251     else if ( *argv == ResKind::patch )
252     {
253       q.addKind( ResKind::patch );
254     }
255     else if ( *argv == ResKind::pattern )
256     {
257       q.addKind( ResKind::pattern );
258     }
259     else
260     {
261       sat::Solvable::SplitIdent ident( qstr );
262       if ( ident.kind() != ResKind::package )
263       {
264         q.addKind( ident.kind() );
265         q.addString( ident.name().asString() );
266       }
267       else
268         q.addString( qstr );
269
270       q.setMatchRegex();
271       q.setCaseSensitive( ! ignorecase );
272
273       if ( names )
274         q.addAttribute( sat::SolvAttr::name );
275       if ( provides )
276         q.addDependency( sat::SolvAttr::provides );
277       if ( requires )
278         q.addDependency( sat::SolvAttr::requires );
279       if ( conflicts )
280         q.addDependency( sat::SolvAttr::conflicts );
281       if ( obsoletes )
282         q.addDependency( sat::SolvAttr::obsoletes );
283       if ( recommends )
284         q.addDependency( sat::SolvAttr::recommends );
285       if ( supplements )
286         q.addDependency( sat::SolvAttr::supplements );
287     }
288
289     message << *argv << " [" << (ignorecase?'i':'_') << (names?'n':'_') << (requires?'r':'_') << (provides?'p':'_')
290     << (conflicts?'c':'_') << (obsoletes?'o':'_') << (recommends?'m':'_') << (supplements?'s':'_') << "] {" << endl;
291
292     for_( it, q.begin(), q.end() )
293     {
294       tableOut( str::numstring( it->id() ), it->asString(),
295                 str::form( "(%d)%s", it->repository().info().priority(), it->repository().name().c_str() ),
296                 it->vendor().asString(),
297                 str::numstring( PoolItem(*it)->buildtime() ) );
298       if ( ! it.matchesEmpty() )
299       {
300         for_( match, it.matchesBegin(), it.matchesEnd() )
301         {
302           tableOut( "", "", "", match->inSolvAttr().asString().substr( 9, 3 )+": " +match->asString() );
303         }
304       }
305     }
306
307     message << "}" << endl;
308   }
309
310   INT << "===[END]============================================" << endl << endl;
311   return 0;
312 }