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