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