Let --root detect and load testcases or testsetups.
[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 <zypp/PoolQuery.h>
6
7 static std::string appname( "NameReqPrv" );
8
9 void message( const std::string & msg_r )
10 {
11   cerr << "*** " << msg_r << endl;
12 }
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;
19     message( msg_r );
20     cerr << endl;
21   }
22   return exit_r;
23 }
24
25 int usage( const std::string & msg_r = std::string(), int exit_r = 100 )
26 {
27   if ( ! msg_r.empty() )
28   {
29     cerr << endl;
30     message( msg_r );
31     cerr << endl;
32   }
33   cerr << "Usage: " << appname << " [--root ROOTDIR] [OPTIONS] NAME... [[OPTIONS] NAME...]..." << endl;
34   cerr << "  Load all enabled repositories (no refresh) and search for" << endl;
35   cerr << "  occurrences of NAME (substring) in package names, provides or" << endl;
36   cerr << "  requires." << endl;
37   cerr << "  --root   Load repos from the system located below ROOTDIR. If ROOTDIR" << endl;
38   cerr << "           denotes a sover testcase, the testcase is loaded." << endl;
39   cerr << "  -i/-I    turn on/off case insensitive search (default on)" << endl;
40   cerr << "  -n/-N    turn on/off looking for names       (default on)" << endl;
41   cerr << "  -p/-P    turn on/off looking for provides    (default off)" << endl;
42   cerr << "  -r/-R    turn on/off looking for requires    (default off)" << endl;
43   cerr << "  -a       short for -n -p -r" << endl;
44   cerr << "  -A       short for -n -P -R" << endl;
45   cerr << "TODO: Waiting for PoolQuery::allMatches switch and need to beautify output." << endl;
46   cerr << "" << endl;
47   return exit_r;
48 }
49
50 /******************************************************************
51 **
52 **      FUNCTION NAME : main
53 **      FUNCTION TYPE : int
54 */
55 int main( int argc, char * argv[] )
56 {
57   INT << "===[START]==========================================" << endl;
58   appname = Pathname::basename( argv[0] );
59   --argc,++argv;
60
61   if ( ! argc )
62   {
63     return usage();
64   }
65
66   ///////////////////////////////////////////////////////////////////
67
68   ZConfig::instance();
69   Pathname sysRoot("/");
70
71   if ( (*argv) == std::string("--root") )
72   {
73     --argc,++argv;
74     if ( ! argc )
75       return errexit("--root requires an argument.");
76
77     if ( ! PathInfo( *argv ).isDir() )
78       return errexit("--root requires a directory.");
79
80     sysRoot = *argv;
81     --argc,++argv;
82   }
83
84   if ( TestSetup::isTestcase( sysRoot ) )
85   {
86     message( str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) );
87     TestSetup test;
88     test.loadTestcaseRepos( sysRoot );
89   }
90   else if ( TestSetup::isTestSetup( sysRoot ) )
91   {
92     message( str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) );
93     TestSetup test( sysRoot );
94     test.loadRepos();
95   }
96   else
97   {
98     // a system
99     message( str::form( "*** Load system at '%s'", sysRoot.c_str() ) );
100     if ( 1 )
101     {
102       message( "*** load target" );
103       getZYpp()->initializeTarget( sysRoot );
104       getZYpp()->target()->load();
105     }
106
107     if ( 1 )
108     {
109       RepoManager repoManager( sysRoot );
110       RepoInfoList repos = repoManager.knownRepositories();
111       for_( it, repos.begin(), repos.end() )
112       {
113         RepoInfo & nrepo( *it );
114
115         if ( ! nrepo.enabled() )
116           continue;
117
118         if ( ! repoManager.isCached( nrepo ) )
119         {
120           message( str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) );
121           continue;
122         }
123
124         message( str::form( "*** load repo '%s'", nrepo.name().c_str() ) );
125         try
126         {
127           repoManager.loadFromCache( nrepo );
128         }
129         catch ( const Exception & exp )
130         {
131           message( exp.asString() + "\n" + exp.historyAsString() );
132           message( str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) );
133           continue;
134         }
135       }
136     }
137   }
138
139   sat::Pool satpool( sat::Pool::instance() );
140   dumpRange( cerr, satpool.reposBegin(), satpool.reposEnd() ) << endl;
141   ///////////////////////////////////////////////////////////////////
142
143   bool ignorecase( true );
144   bool names     ( true );
145   bool provides  ( false );
146   bool requires  ( false );
147
148   for ( ; argc; --argc,++argv )
149   {
150     if ( (*argv)[0] == '-' )
151     {
152       switch ( (*argv)[1] )
153       {
154         case 'a':  names =      true,   requires = provides =   true;   break;
155         case 'A':  names =      true,   requires = provides =   false;  break;
156         case 'i': ignorecase =  true;   break;
157         case 'I': ignorecase =  false;  break;
158         case 'n': names =       true;   break;
159         case 'N': names =       false;  break;
160         case 'r': requires =    true;   break;
161         case 'R': requires =    false;  break;
162         case 'p': provides =    true;   break;
163         case 'P': provides =    false;  break;
164       }
165       continue;
166     }
167
168     PoolQuery q;
169     q.addString( *argv );
170     q.setMatchSubstring();
171     q.setCaseSensitive( ! ignorecase );
172
173     if ( names )
174       q.addAttribute( sat::SolvAttr::name );
175     if ( provides )
176       q.addDependency( sat::SolvAttr::provides );
177     if ( requires )
178       q.addDependency( sat::SolvAttr::requires );
179
180 //     q.begin();
181 //     cerr << q << endl;
182
183     cerr << *argv << " [" << (ignorecase?'i':'_') << (names?'n':'_') << (requires?'r':'_') << (provides?'p':'_') << "] {" << endl;
184
185     for_( it, q.begin(), q.end() )
186     {
187       cerr << "  " << it << endl;
188     }
189
190     cerr << "}" << endl;
191   }
192
193   INT << "===[END]============================================" << endl << endl;
194   return 0;
195 }