Add tool/DumpSelectable
[platform/upstream/libzypp.git] / tools / DumpSelectable.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( "DumpSelectable" );
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..." << endl;
29   cerr << "  Load all enabled repositories (no refresh) and search for" << endl;
30   cerr << "  Selectables names NAME" << endl;
31   cerr << "  --root   Load repos from the system located below ROOTDIR. If ROOTDIR" << endl;
32   cerr << "           denotes a sover testcase, the testcase is loaded." << endl;
33   cerr << "" << endl;
34   return exit_r;
35 }
36
37 /******************************************************************
38 **
39 **      FUNCTION NAME : main
40 **      FUNCTION TYPE : int
41 */
42 int main( int argc, char * argv[] )
43 {
44   INT << "===[START]==========================================" << endl;
45   appname = Pathname::basename( argv[0] );
46   --argc,++argv;
47
48   if ( ! argc )
49   {
50     return usage();
51   }
52
53   ///////////////////////////////////////////////////////////////////
54
55   ZConfig::instance();
56   Pathname sysRoot("/");
57   sat::Pool satpool( sat::Pool::instance() );
58
59   if ( (*argv) == std::string("--root") )
60   {
61     --argc,++argv;
62     if ( ! argc )
63       return errexit("--root requires an argument.");
64
65     if ( ! PathInfo( *argv ).isDir() )
66       return errexit("--root requires a directory.");
67
68     sysRoot = *argv;
69     --argc,++argv;
70   }
71
72   if ( TestSetup::isTestcase( sysRoot ) )
73   {
74     message << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
75     TestSetup test;
76     test.loadTestcaseRepos( sysRoot );
77   }
78   else if ( TestSetup::isTestSetup( sysRoot ) )
79   {
80     message << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
81     TestSetup test( sysRoot, Arch_x86_64 );
82     test.loadRepos();
83   }
84   else
85   {
86     // a system
87     message << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
88     if ( true )
89     {
90       message << "*** load target '" << Repository::systemRepoAlias() << "'\t" << endl;
91       getZYpp()->initializeTarget( sysRoot );
92       getZYpp()->target()->load();
93       message << satpool.systemRepo() << endl;
94     }
95
96     if ( true )
97     {
98       RepoManager repoManager( sysRoot );
99       RepoInfoList repos = repoManager.knownRepositories();
100       for_( it, repos.begin(), repos.end() )
101       {
102         RepoInfo & nrepo( *it );
103
104         if ( ! nrepo.enabled() )
105           continue;
106
107         if ( ! repoManager.isCached( nrepo ) )
108         {
109           message << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
110           continue;
111         }
112
113         message << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
114         try
115         {
116           repoManager.loadFromCache( nrepo );
117           message << satpool.reposFind( nrepo.alias() ) << endl;
118         }
119         catch ( const Exception & exp )
120         {
121           message << exp.asString() + "\n" + exp.historyAsString() << endl;
122           message << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
123           continue;
124         }
125       }
126     }
127   }
128
129   ///////////////////////////////////////////////////////////////////
130
131   for ( ; argc; --argc,++argv )
132   {
133     message << dump( ui::Selectable::get( IdString( *argv ) ) ) << endl;
134   }
135
136   INT << "===[END]============================================" << endl << endl;
137   return 0;
138 }