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