426b63085f2bc40a11f9ea0b0d167decc5abc2d2
[platform/upstream/libzypp.git] / tools / registration / zypp-query-pool.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 #include <iostream>
4
5 #include <zypp/ZYpp.h>
6 #include <zypp/zypp_detail/ZYppReadOnlyHack.h>
7 #include <zypp/ZYppFactory.h>
8 #include <zypp/RepoManager.h>
9 #include <zypp/base/Logger.h>
10 #include <zypp/base/Exception.h>
11 #include <zypp/base/Algorithm.h>
12 #include <zypp/Product.h>
13
14 #undef ZYPP_BASE_LOGGER_LOGGROUP
15 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp-query-pool"
16
17 #include "keyring-callbacks.h"
18
19 using namespace std;
20 using namespace zypp;
21
22 //-----------------------------------------------------------------------------
23
24 class PrintItem : public resfilter::PoolItemFilterFunctor
25 {
26 public:
27   string _repository;
28
29   PrintItem( const string & repository )
30       : _repository( repository )
31   { }
32
33   bool operator()( PoolItem_Ref item )
34   {
35     if (_repository.empty()
36         || _repository == item->repository().info().alias())
37     {
38       cout << (item.status().isInstalled() ? "i" : " ");
39       cout << "|" << item->kind();
40       cout << "|" << item->name();
41       cout << "|" << item->edition().version();
42       if (!item->edition().release().empty())
43         cout << "-" << item->edition().release();
44       cout << "|" << item->arch();
45
46       if ( isKind<Product>( item.resolvable() ) )
47         {
48           Product::constPtr p( asKind<Product>( item ) );
49           cout << "|" << p->distributionName();
50           cout << "|" << p->distributionEdition();
51         }
52       cout << endl;
53     }
54     return true;
55   }
56 };
57
58
59
60
61 static void
62 query_pool( ZYpp::Ptr Z, string filter, const string & repository)
63 {
64   Resolvable::Kind kind;
65
66 #define FILTER_ALL "all"
67   if ( filter.empty() ) filter = FILTER_ALL;
68
69   if (filter == "packages") kind = ResTraits<zypp::Package>::kind;
70   else if (filter == "patches") kind = ResTraits<zypp::Patch>::kind;
71   else if (filter == "patterns") kind = ResTraits<zypp::Pattern>::kind;
72   else if (filter == "selections") kind = ResTraits<zypp::Selection>::kind;
73   else if (filter == "products") kind = ResTraits<zypp::Product>::kind;
74   else if (filter != FILTER_ALL)
75   {
76     std::cerr << "usage: zypp-query-pool [packages|patches|patterns|products] [<alias>]" << endl;
77     exit( 1 );
78   }
79
80   bool system = (repository == "@system");
81
82   MIL << "query_pool kind '" << kind << "', repository '" << repository << "'" << endl;
83
84   if (!system)
85   {
86     try
87     {
88       MIL << "Load enabled repositories..." << endl;
89       RepoManager  repoManager;
90       RepoInfoList repos = repoManager.knownRepositories();
91       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
92       {
93         RepoInfo & repo( *it );
94
95         if ( ! repo.enabled() )
96           continue;
97
98         MIL << "Loading " << repo << endl;
99         if ( ! repoManager.isCached( repo ) )
100         {
101           MIL << "Must build cache..." << repo << endl;
102           repoManager.buildCache( repo );
103         }
104         Z->addResolvables( repoManager.createFromCache( repo ).resolvables() );
105       }
106       MIL << "Loaded enabled repositories." << endl;
107     }
108     catch (Exception & excpt_r)
109     {
110       ZYPP_CAUGHT( excpt_r );
111       ERR << "Couldn't restore sources" << endl;
112       exit( 1 );
113     }
114   }
115
116   // add resolvables from the system
117   if ( filter != FILTER_ALL )
118   {
119     MIL << "Loading target (" << kind << ")..." << endl;
120     ResStore items;
121     for (ResStore::resfilter_const_iterator it = Z->target()->byKindBegin(kind); it != Z->target()->byKindEnd(kind); ++it)
122     {
123       SEC << *it << endl;
124       items.insert(*it);
125     }
126     Z->addResolvables( items, true );
127   }
128   else
129   {
130     // no filter, just add themm all
131     MIL << "Loading target..." << endl;
132     Z->addResolvables( Z->target()->resolvables(), true );
133   }
134   MIL << "Loaded target." << endl;
135
136
137   MIL << "Pool has " << Z->pool().size() << " entries" << endl;
138   if ( filter == FILTER_ALL)
139   {
140     PrintItem printitem( system ? "" : repository );
141     if (system)
142       zypp::invokeOnEach( Z->pool().begin(), Z->pool().end(),                           // all kinds
143                           zypp::resfilter::ByInstalled(),
144                           zypp::functor::functorRef<bool,PoolItem> (printitem) );
145     else
146       zypp::invokeOnEach( Z->pool().begin(), Z->pool().end(),                           // all kinds
147                           zypp::functor::functorRef<bool,PoolItem> (printitem) );
148
149   }
150   else
151   {
152     PrintItem printitem( system ? "" : repository );
153     if (system)
154       zypp::invokeOnEach( Z->pool().byKindBegin( kind ), Z->pool().byKindEnd( kind ),   // filter kind
155                           zypp::resfilter::ByInstalled(),
156                           zypp::functor::functorRef<bool,PoolItem> (printitem) );
157     else
158       zypp::invokeOnEach( Z->pool().byKindBegin( kind ), Z->pool().byKindEnd( kind ),   // filter kind
159                           zypp::functor::functorRef<bool,PoolItem> (printitem) );
160   }
161   return;
162 }
163
164 //-----------------------------------------------------------------------------
165
166 int
167 main (int argc, char **argv)
168 {
169   MIL << "-------------------------------------" << endl;
170   string filter;
171   if (argc > 1)
172     filter = argv[1];
173   string repository;
174   if (argc > 2)
175     repository = argv[2];
176
177   MIL << "START zypp-query-pool " << filter << " " << repository << endl;
178
179   zypp::zypp_readonly_hack::IWantIt();
180   ZYpp::Ptr Z = zypp::getZYpp();
181
182   KeyRingCallbacks keyring_callbacks;
183   DigestCallbacks digest_callbacks;
184
185   Z->initializeTarget( "/" );
186
187   query_pool( Z, filter, repository );
188
189   MIL << "END zypp-query-pool, result 0" << endl;
190
191   return 0;
192 }