Imported Upstream version 17.0.2
[platform/upstream/libzypp.git] / tools / zypp-list.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( "zypp-list" );
9
10 #define message cerr
11 #define OUT     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 << " [GLOBALOPTS] COMMAND" << endl;
30   cerr << "List pool items according to command." << endl;
31   cerr << endl;
32   cerr << "GLOBALOPTS:" << 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 << "  -i, --installed Process installed packages only." << endl;
36   cerr << endl;
37   cerr << "COMMANDS:" << endl;
38   cerr << "  locks:   List all locked pool items." << endl;
39   cerr << "    ." << endl;
40   cerr << endl;
41   return exit_r;
42 }
43
44 void startup( const Pathname & sysRoot = "/", bool onlyInstalled = false )
45 {
46   ZConfig::instance();
47   sat::Pool satpool( sat::Pool::instance() );
48
49   if ( TestSetup::isTestcase( sysRoot ) )
50   {
51     message << str::form( "*** Load Testcase from '%s'", sysRoot.c_str() ) << endl;
52     TestSetup test;
53     test.loadTestcaseRepos( sysRoot );
54     dumpRange( message, satpool.reposBegin(), satpool.reposEnd() ) << endl;
55   }
56   else if ( TestSetup::isTestSetup( sysRoot ) )
57   {
58     message << str::form( "*** Load TestSetup from '%s'", sysRoot.c_str() ) << endl;
59     TestSetup test( sysRoot, Arch_x86_64 );
60     test.loadRepos();
61     dumpRange( message, satpool.reposBegin(), satpool.reposEnd() ) << endl;
62   }
63   else
64   {
65     // a system
66     message << str::form( "*** Load system at '%s'", sysRoot.c_str() ) << endl;
67     if ( true )
68     {
69       message << "*** load target '" << Repository::systemRepoAlias() << "'\t";
70       getZYpp()->initializeTarget( sysRoot );
71       getZYpp()->target()->load();
72       message << satpool.systemRepo() << endl;
73     }
74
75     if ( !onlyInstalled )
76     {
77       RepoManager repoManager( sysRoot );
78       RepoInfoList repos = repoManager.knownRepositories();
79       for_( it, repos.begin(), repos.end() )
80       {
81         RepoInfo & nrepo( *it );
82
83         if ( ! nrepo.enabled() )
84           continue;
85
86         if ( ! repoManager.isCached( nrepo ) )
87         {
88           message << str::form( "*** omit uncached repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
89           continue;
90         }
91
92         message << str::form( "*** load repo '%s'\t", nrepo.name().c_str() ) << flush;
93         try
94         {
95           repoManager.loadFromCache( nrepo );
96           message << satpool.reposFind( nrepo.alias() ) << endl;
97         }
98         catch ( const Exception & exp )
99         {
100           message << exp.asString() + "\n" + exp.historyAsString() << endl;
101           message << str::form( "*** omit broken repo '%s' (do 'zypper refresh')", nrepo.name().c_str() ) << endl;
102           continue;
103         }
104       }
105     }
106   }
107 }
108
109 /******************************************************************
110 **
111 **      FUNCTION NAME : main
112 **      FUNCTION TYPE : int
113 */
114 int main( int argc, char * argv[] )
115 {
116   INT << "===[START]==========================================" << endl;
117   appname = Pathname::basename( argv[0] );
118   --argc,++argv;
119
120   if ( ! argc )
121   {
122     return usage();
123   }
124
125   ///////////////////////////////////////////////////////////////////
126
127   Pathname sysRoot( "/" );
128   if ( argc && (*argv) == std::string("--root") )
129   {
130     --argc,++argv;
131     if ( ! argc )
132       return errexit("--root requires an argument.");
133
134     if ( ! PathInfo( *argv ).isDir() )
135       return errexit("--root requires a directory.");
136
137     sysRoot = *argv;
138     --argc,++argv;
139   }
140
141   bool onlyInstalled( false );
142   if ( argc && (*argv) == std::string("--installed") )
143   {
144     --argc,++argv;
145     onlyInstalled = true;
146   }
147
148   ///////////////////////////////////////////////////////////////////
149
150   if ( ! argc )
151   {
152     return usage();
153   }
154
155   startup( sysRoot, onlyInstalled );
156   ResPool pool( ResPool::instance() );
157
158   if ( argc && (*argv) == std::string("locked") )
159   {
160     OUT << "*** Locked:" << endl;
161     for_( it, pool.begin(), pool.end() )
162     {
163       if ( (*it).status().isLocked() )
164         OUT << *it << endl;
165     }
166   }
167
168   INT << "===[END]============================================" << endl << endl;
169   return 0;
170 }