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