1 #define INCLUDE_TESTSETUP_WITHOUT_BOOST
2 #include "../tests/lib/TestSetup.h"
3 #undef INCLUDE_TESTSETUP_WITHOUT_BOOST
6 #include <zypp/PoolQuery.h>
7 #include <zypp/ResObjects.h>
8 #include <zypp/ui/SelectableTraits.h>
10 static std::string appname( "NameReqPrv" );
15 int errexit( const std::string & msg_r = std::string(), int exit_r = 100 )
17 if ( ! msg_r.empty() )
19 cerr << endl << msg_r << endl << endl;
24 int usage( const std::string & msg_r = std::string(), int exit_r = 100 )
26 if ( ! msg_r.empty() )
28 cerr << endl << msg_r << endl << endl;
30 cerr << "Usage: " << appname << " [--root ROOTDIR] [OPTIONS] NAME... [[OPTIONS] NAME...]..." << endl;
31 cerr << " Load all enabled repositories (no refresh) and search for" << endl;
32 cerr << " occurrences of NAME (regex or -x) in package names or dependencies" << endl;
33 cerr << " --root Load repos from the system located below ROOTDIR. If ROOTDIR" << endl;
34 cerr << " denotes a sover testcase, the testcase is loaded." << endl;
35 cerr << " --installed Process installed packages only." << endl;
36 cerr << " -i/-I turn on/off case insensitive search (default on)" << endl;
37 cerr << " -n/-N turn on/off looking for names (default on)" << endl;
38 cerr << " -p/-P turn on/off looking for provides (default off)" << endl;
39 cerr << " -r/-R turn on/off looking for requires (default off)" << endl;
40 cerr << " -c/-C turn on/off looking for conflicts (default off)" << endl;
41 cerr << " -o/-O turn on/off looking for obsoletes (default off)" << endl;
42 cerr << " -m/-M turn on/off looking for recommends (default off)" << endl;
43 cerr << " -s/-S turn on/off looking for supplements (default off)" << endl;
44 cerr << " -e/-E turn on/off looking for enhan./sugg.(default off)" << endl;
45 cerr << " -a short for -n -p -r" << endl;
46 cerr << " -A short for -n -P -R" << endl;
47 cerr << " -x do exact matching (glob) rather than regex (substring)" << endl;
48 cerr << " -D <pkg> dump dependencies of <pkg>" << endl;
53 #define COL_R "\033[0;31m"
54 #define COL_G "\033[0;32m"
55 #define COL_B "\033[0;34m"
56 #define COL_C "\033[0;36m"
57 #define COL_M "\033[0;35m"
58 #define COL_Y "\033[0;33m"
59 #define COL_BL "\033[0;30m"
60 #define COL_WH "\033[1;37m"
61 #define COL_OFF "\033[0m"
66 bool operator()( const sat::Solvable & lhs, const sat::Solvable & rhs ) const
69 bool l = lhs.isSystem();
70 bool r = rhs.isSystem();
75 IdString l { lhs.ident() };
76 IdString r { rhs.ident() };
80 return avo( PoolItem(lhs), PoolItem(rhs) );
81 return lhs.id() > rhs.id();
84 ui::SelectableTraits::AVOrder avo;
89 std::vector<std::string> & row( const sat::Solvable & solv_r )
92 smax( _maxNAME, solv_r.ident().size() + solv_r.edition().size() + solv_r.arch().size() );
93 smax( _maxREPO, solv_r.repository().name().size(), solv_r.repository().name() );
95 //smax( _maxVEND, solv_r.vendor().size() );
99 void row( PoolQuery::const_iterator it_r )
101 std::vector<std::string> & details { row( *it_r ) };
102 for_( match, it_r.matchesBegin(), it_r.matchesEnd() ) {
103 details.push_back( match->inSolvAttr().asString().substr( 9, 3 )+": " +match->asString() );
107 std::ostream & dumpOn( std::ostream & str ) const
112 #define argSID _maxSID, slv.id()
114 #define fmtNAME COL_B "%s" COL_OFF "-" COL_G "%s" COL_OFF ".%-*s"
115 #define argNAME slv.ident().c_str(), slv.edition().c_str(), _maxNAME-slv.ident().size()-slv.edition().size(), slv.arch().c_str()
117 #define fmtREPO "(%2d)%-*s"
118 #define argREPO slv.repository().info().priority(), _maxREPO, slv.repository().name().c_str()
120 #define fmtTIME "%10ld"
121 #define argTIME time_t( slv.isSystem() ? slv.installtime() : slv.buildtime() )
124 #define argVEND slv.vendor().c_str()
126 std::string dind( _maxSID + _maxNAME+2/*-.*/ + 2*strlen(S), ' ' );
128 for ( const auto & el : _map ) {
129 sat::Solvable slv { el.first };
130 const char * tagCol = slv.isSystem() ? COL_M : ui::Selectable::get(slv)->identicalInstalled(PoolItem(slv)) ? COL_C : "";
132 str << str::form( "%s" fmtSID S fmtNAME S "%s" fmtREPO S fmtTIME S fmtVEND COL_OFF "\n",
133 tagCol, argSID, argNAME, tagCol, argREPO, argTIME, argVEND );
135 for ( const auto & d : el.second )
136 str << dind << d << endl;
142 static void smax( unsigned & var_r, unsigned val_r, std::string_view n = {} )
143 { if ( val_r > var_r ) var_r = val_r; }
146 unsigned _maxSID = 7; // size + indent
147 unsigned _maxNAME = 0;
148 unsigned _maxREPO = 0;
149 //unsigned _maxTIME = 10;
150 //unsigned _maxVEND = 0;
151 std::map<sat::Solvable, std::vector<std::string>, PQSort> _map;
154 inline std::ostream & operator<<( std::ostream & str, const Table & table_r )
155 { return table_r.dumpOn( str ); }
157 ///////////////////////////////////////////////////////////////////
159 void dDump( const std::string & spec_r )
161 message << "DUMP " << spec_r << " {";
163 sat::WhatProvides q( Capability::guessPackageSpec( spec_r ) );
166 message << "}" << endl;
170 for ( const auto & el : q )
172 message << endl << "==============================" << endl << dump(el);
173 if ( isKind<Product>(el) )
175 message << endl << "REPLACES: " << make<Product>(el)->replacedProducts();
177 else if ( isKind<Pattern>(el) )
179 message << endl << "CONTENT: " << make<Pattern>(el)->contents();
181 else if ( isKind<Patch>(el) )
183 message << endl << "STATUS: " << PoolItem(el);
186 message << endl << "}" << endl;
189 ///////////////////////////////////////////////////////////////////
191 void dTree( const std::string & cmd_r, const std::string & spec_r )
193 message << "tree " << spec_r << " {";
195 sat::WhatProvides spec( Capability::guessPackageSpec( spec_r ) );
198 message << "}" << endl;
202 static const std::list<sat::SolvAttr> attrs {
203 sat::SolvAttr::requires,
204 sat::SolvAttr::recommends,
205 sat::SolvAttr::obsoletes,
206 sat::SolvAttr::conflicts,
207 sat::SolvAttr::supplements,
210 std::map<sat::SolvAttr,std::map<sat::Solvable,std::set<std::string>>> result; // solvables recommending provided capability
213 for ( const auto & el : spec )
215 auto & details { t.row( el ) };
216 for ( const auto & cap : el.provides() )
218 //details.push_back( "prv: "+cap.asString() ); // list only matching ones
220 // get attrs matching cap
221 for ( const auto & attr : attrs )
224 q.addDependency( attr, cap );
227 for_( it, q.begin(), q.end() ) {
228 for_( match, it.matchesBegin(), it.matchesEnd() ) {
229 result[attr][*it].insert( match->inSolvAttr().asString().substr( 9, 3 )+": " +match->asString()
230 + " (" + cap.asString() + ")"
237 message << endl << t;
240 for ( const auto & attr : attrs )
242 if ( result[attr].empty() )
245 message << endl << "======== " << attr << " by:" << endl;
247 for ( const auto & el : result[attr] )
249 auto & details { t.row( el.first ) };
250 for ( const auto & cap : el.second )
251 details.push_back( cap );
253 message << endl << t << endl;
255 message << "}" << endl;
258 /******************************************************************
260 ** FUNCTION NAME : main
261 ** FUNCTION TYPE : int
263 int main( int argc, char * argv[] )
265 INT << "===[START]==========================================" << endl;
266 appname = Pathname::basename( argv[0] );
274 ///////////////////////////////////////////////////////////////////
277 Pathname sysRoot("/");
278 sat::Pool satpool( sat::Pool::instance() );
280 if ( argc && (*argv) == std::string("--root") )
284 return errexit("--root requires an argument.");
286 if ( ! PathInfo( *argv ).isDir() )
287 return errexit("--root requires a directory.");
293 bool onlyInstalled( false );
294 if ( argc && (*argv) == std::string("--installed") )
297 onlyInstalled = true;
300 if ( TestSetup::isTestcase( sysRoot ) )
302 message << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
304 test.loadTestcaseRepos( sysRoot );
305 dumpRange( message, satpool.reposBegin(), satpool.reposEnd() ) << endl;
307 else if ( TestSetup::isTestSetup( sysRoot ) )
309 message << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
310 const char * astr = getenv( "ZYPP_TESTSUITE_FAKE_ARCH" );
311 if ( !astr || !*astr )
312 astr = getenv( "ZYPP_ARCH" );
313 if ( !astr || !*astr )
315 TestSetup test( sysRoot, Arch( astr ) );
317 dumpRange( message, satpool.reposBegin(), satpool.reposEnd() ) << endl;
322 message << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
325 message << "*** load target '" << Repository::systemRepoAlias() << "'\t" << endl;
326 getZYpp()->initializeTarget( sysRoot );
327 getZYpp()->target()->load();
328 message << satpool.systemRepo() << endl;
331 if ( !onlyInstalled )
333 RepoManager repoManager( sysRoot );
334 RepoInfoList repos = repoManager.knownRepositories();
335 for_( it, repos.begin(), repos.end() )
337 RepoInfo & nrepo( *it );
339 if ( ! nrepo.enabled() )
342 if ( ! repoManager.isCached( nrepo ) )
344 message << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
348 message << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
351 repoManager.loadFromCache( nrepo );
352 message << satpool.reposFind( nrepo.alias() ) << endl;
354 catch ( const Exception & exp )
356 message << exp.asString() + "\n" + exp.historyAsString() << endl;
357 message << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
363 ///////////////////////////////////////////////////////////////////
365 bool ignorecase ( true );
366 bool matechexact ( false );
367 bool withSrcPackages ( false );
369 bool provides ( false );
370 bool requires ( false );
371 bool conflicts ( false );
372 bool obsoletes ( false );
373 bool recommends ( false );
374 bool supplements ( false );
375 bool enhacements ( false );
378 for ( ; argc; --argc,++argv )
380 if ( (*argv)[0] == '-' )
382 for ( const char * arg = (*argv)+1; *arg != '\0'; ++arg ) // -pr for -p -r
386 case 'a': names = true, requires = provides = true; break;
387 case 'A': names = true, requires = provides = false; break;
395 return errexit("-D <pkgspec> requires an argument.");
400 std::string cmd { *argv };
405 return errexit("-T <pkgspec> requires an argument.");
407 case 'i': ignorecase = true; break;
408 case 'I': ignorecase = false; break;
409 case 'x': matechexact = true; break;
410 case 'n': names = true; break;
411 case 'N': names = false; break;
412 case 'r': requires = true; break;
413 case 'R': requires = false; break;
414 case 'p': provides = true; break;
415 case 'P': provides = false; break;
416 case 'c': conflicts = true; break;
417 case 'C': conflicts = false; break;
418 case 'o': obsoletes = true; break;
419 case 'O': obsoletes = false; break;
420 case 'm': recommends = true; break;
421 case 'M': recommends = false; break;
422 case 's': supplements = true; break;
423 case 'S': supplements = false; break;
424 case 'e': enhacements = true; break;
425 case 'E': enhacements = false; break;
433 q.setInstalledOnly();
434 std::string qstr( *argv );
436 if ( *argv == ResKind::product )
438 q.addKind( ResKind::product );
440 else if ( *argv == ResKind::patch )
442 q.addKind( ResKind::patch );
444 else if ( *argv == ResKind::pattern )
446 q.addKind( ResKind::pattern );
450 sat::Solvable::SplitIdent ident( qstr );
451 if ( ident.kind() != ResKind::package )
453 q.addKind( ident.kind() );
454 q.addString( ident.name().asString() );
463 q.setCaseSensitive( ! ignorecase );
466 q.addAttribute( sat::SolvAttr::name );
469 q.addDependency( sat::SolvAttr::provides );
470 q.addDependency( sat::SolvAttr::provides, Capability(qstr) );
474 q.addDependency( sat::SolvAttr::requires );
475 q.addDependency( sat::SolvAttr::requires, Capability(qstr) );
479 q.addDependency( sat::SolvAttr::conflicts );
480 q.addDependency( sat::SolvAttr::conflicts, Capability(qstr) );
484 q.addDependency( sat::SolvAttr::obsoletes );
485 q.addDependency( sat::SolvAttr::obsoletes, Capability(qstr) );
489 q.addDependency( sat::SolvAttr::recommends );
490 q.addDependency( sat::SolvAttr::recommends, Capability(qstr) );
494 q.addDependency( sat::SolvAttr::supplements );
495 q.addDependency( sat::SolvAttr::supplements, Capability(qstr) );
499 q.addDependency( sat::SolvAttr::enhances );
500 q.addDependency( sat::SolvAttr::enhances, Capability(qstr) );
501 q.addDependency( sat::SolvAttr::suggests );
502 q.addDependency( sat::SolvAttr::suggests, Capability(qstr) );
506 message << *argv << " [" << (ignorecase?'i':'_') << (names?'n':'_') << (requires?'r':'_') << (provides?'p':'_')
507 << (conflicts?'c':'_') << (obsoletes?'o':'_') << (recommends?'m':'_') << (supplements?'s':'_') << (enhacements?'e':'_')
511 for_( it, q.begin(), q.end() )
513 if ( it->isKind( ResKind::srcpackage ) && !withSrcPackages )
517 message << t << "}" << endl;
520 INT << "===[END]============================================" << endl << endl;