Imported Upstream version 17.0.2
[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 #include <zypp/ResObjects.h>
8
9 static std::string appname( "DumpSelectable" );
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..." << endl;
30   cerr << "  Load all enabled repositories (no refresh) and search for" << endl;
31   cerr << "  Selectables names NAME" << 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 << "  -v       Verbose list solvables data." << endl;
35   cerr << "" << endl;
36   return exit_r;
37 }
38
39 void dumpPi( std::ostream & message, const PoolItem & pi )
40 {
41   std::string indent("   ");
42   message << indent << "--------------------------------------------------" << endl;
43   message << indent << (pi->isSystem() ? "i " : "a ") <<  pi->satSolvable().asString() << endl;
44   message << indent << pi->summary() << endl;
45   if ( pi->isKind<Package>() )
46   {
47     message << indent << pi->asKind<Package>()->changelog() << endl;
48   }
49 }
50
51 /******************************************************************
52 **
53 **      FUNCTION NAME : main
54 **      FUNCTION TYPE : int
55 */
56 int main( int argc, char * argv[] )
57 {
58   INT << "===[START]==========================================" << endl;
59   appname = Pathname::basename( argv[0] );
60   --argc,++argv;
61
62   Pathname sysRoot("/");
63   bool verbose = false;
64
65   while ( argc && (*argv)[0] == '-' )
66   {
67     if ( (*argv) == std::string("--root") )
68     {
69       --argc,++argv;
70       if ( ! argc )
71         return errexit("--root requires an argument.");
72
73       if ( ! PathInfo( *argv ).isDir() )
74         return errexit("--root requires a directory.");
75
76       sysRoot = *argv;
77     }
78     else if ( (*argv) == std::string("-v") )
79     {
80       verbose = true;
81     }
82
83     --argc,++argv;
84   }
85
86   if ( ! argc )
87   {
88     return usage();
89   }
90
91   ///////////////////////////////////////////////////////////////////
92
93   ZConfig::instance();
94   sat::Pool satpool( sat::Pool::instance() );
95
96   if ( TestSetup::isTestcase( sysRoot ) )
97   {
98     message << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
99     TestSetup test;
100     test.loadTestcaseRepos( sysRoot );
101   }
102   else if ( TestSetup::isTestSetup( sysRoot ) )
103   {
104     message << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
105     TestSetup test( sysRoot, Arch_x86_64 );
106     test.loadRepos();
107   }
108   else
109   {
110     // a system
111     message << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
112     if ( true )
113     {
114       message << "*** load target '" << Repository::systemRepoAlias() << "'\t" << endl;
115       getZYpp()->initializeTarget( sysRoot );
116       getZYpp()->target()->load();
117       message << satpool.systemRepo() << endl;
118     }
119
120     if ( true )
121     {
122       RepoManager repoManager( sysRoot );
123       RepoInfoList repos = repoManager.knownRepositories();
124       for_( it, repos.begin(), repos.end() )
125       {
126         RepoInfo & nrepo( *it );
127
128         if ( ! nrepo.enabled() )
129           continue;
130
131         if ( ! repoManager.isCached( nrepo ) )
132         {
133           message << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
134           continue;
135         }
136
137         message << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
138         try
139         {
140           repoManager.loadFromCache( nrepo );
141           message << satpool.reposFind( nrepo.alias() ) << endl;
142         }
143         catch ( const Exception & exp )
144         {
145           message << exp.asString() + "\n" + exp.historyAsString() << endl;
146           message << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
147           continue;
148         }
149       }
150     }
151   }
152
153   ///////////////////////////////////////////////////////////////////
154
155   for ( ; argc; --argc,++argv )
156   {
157     ui::Selectable::Ptr sel( ui::Selectable::get( IdString( *argv ) ) );
158     message << dump( sel ) << endl;
159     if ( verbose )
160     {
161       for_( it, sel->installedBegin(), sel->installedEnd() )
162         dumpPi( message, *it );
163       for_( it, sel->availableBegin(), sel->availableEnd() )
164         dumpPi( message, *it );
165     }
166   }
167
168   INT << "===[END]============================================" << endl << endl;
169   return 0;
170 }