1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
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>
14 #undef ZYPP_BASE_LOGGER_LOGGROUP
15 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp-query-pool"
17 #include "keyring-callbacks.h"
22 //-----------------------------------------------------------------------------
24 class PrintItem : public resfilter::PoolItemFilterFunctor
29 PrintItem( const string & repository )
30 : _repository( repository )
33 bool operator()( PoolItem_Ref item )
35 if (_repository.empty()
36 || _repository == item->repository().info().alias())
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();
46 if ( isKind<Product>( item.resolvable() ) )
48 Product::constPtr p( asKind<Product>( item ) );
49 cout << "|" << p->distributionName();
50 cout << "|" << p->distributionEdition();
62 query_pool( ZYpp::Ptr Z, string filter, const string & repository)
64 Resolvable::Kind kind;
66 #define FILTER_ALL "all"
67 if ( filter.empty() ) filter = FILTER_ALL;
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)
76 std::cerr << "usage: zypp-query-pool [packages|patches|patterns|products] [<alias>]" << endl;
80 bool system = (repository == "@system");
82 MIL << "query_pool kind '" << kind << "', repository '" << repository << "'" << endl;
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 )
93 RepoInfo & repo( *it );
95 if ( ! repo.enabled() )
98 MIL << "Loading " << repo << endl;
99 if ( ! repoManager.isCached( repo ) )
101 MIL << "Must build cache..." << repo << endl;
102 repoManager.buildCache( repo );
104 Z->addResolvables( repoManager.createFromCache( repo ).resolvables() );
106 MIL << "Loaded enabled repositories." << endl;
108 catch (Exception & excpt_r)
110 ZYPP_CAUGHT( excpt_r );
111 ERR << "Couldn't restore sources" << endl;
116 // add resolvables from the system
117 if ( filter != FILTER_ALL )
119 MIL << "Loading target (" << kind << ")..." << endl;
121 for (ResStore::resfilter_const_iterator it = Z->target()->byKindBegin(kind); it != Z->target()->byKindEnd(kind); ++it)
126 Z->addResolvables( items, true );
130 // no filter, just add themm all
131 MIL << "Loading target..." << endl;
132 Z->addResolvables( Z->target()->resolvables(), true );
134 MIL << "Loaded target." << endl;
137 MIL << "Pool has " << Z->pool().size() << " entries" << endl;
138 if ( filter == FILTER_ALL)
140 PrintItem printitem( system ? "" : repository );
142 zypp::invokeOnEach( Z->pool().begin(), Z->pool().end(), // all kinds
143 zypp::resfilter::ByInstalled(),
144 zypp::functor::functorRef<bool,PoolItem> (printitem) );
146 zypp::invokeOnEach( Z->pool().begin(), Z->pool().end(), // all kinds
147 zypp::functor::functorRef<bool,PoolItem> (printitem) );
152 PrintItem printitem( system ? "" : repository );
154 zypp::invokeOnEach( Z->pool().byKindBegin( kind ), Z->pool().byKindEnd( kind ), // filter kind
155 zypp::resfilter::ByInstalled(),
156 zypp::functor::functorRef<bool,PoolItem> (printitem) );
158 zypp::invokeOnEach( Z->pool().byKindBegin( kind ), Z->pool().byKindEnd( kind ), // filter kind
159 zypp::functor::functorRef<bool,PoolItem> (printitem) );
164 //-----------------------------------------------------------------------------
167 main (int argc, char **argv)
169 MIL << "-------------------------------------" << endl;
175 repository = argv[2];
177 MIL << "START zypp-query-pool " << filter << " " << repository << endl;
179 zypp::zypp_readonly_hack::IWantIt();
180 ZYpp::Ptr Z = zypp::getZYpp();
182 KeyRingCallbacks keyring_callbacks;
183 DigestCallbacks digest_callbacks;
185 Z->initializeTarget( "/" );
187 query_pool( Z, filter, repository );
189 MIL << "END zypp-query-pool, result 0" << endl;