3 #include "zypp/PoolQueryResult.h"
5 #include <zypp/base/PtrTypes.h>
6 #include <zypp/base/Exception.h>
7 #include <zypp/base/Gettext.h>
8 #include <zypp/base/LogTools.h>
9 #include <zypp/base/Debug.h>
10 #include <zypp/base/Functional.h>
11 #include <zypp/base/IOStream.h>
12 #include <zypp/base/InputStream.h>
13 #include <zypp/base/ProvideNumericId.h>
14 #include <zypp/AutoDispose.h>
16 #include "zypp/ResPoolProxy.h"
18 #include "zypp/ZYppCallbacks.h"
19 #include "zypp/ResPool.h"
20 #include "zypp/ResFilters.h"
21 #include "zypp/ResObjects.h"
22 #include "zypp/Digest.h"
23 #include "zypp/PackageKeyword.h"
24 #include "zypp/TmpPath.h"
25 #include "zypp/ManagedFile.h"
26 #include "zypp/NameKindProxy.h"
27 #include "zypp/pool/GetResolvablesToInsDel.h"
29 #include "zypp/RepoManager.h"
30 #include "zypp/Repository.h"
31 #include "zypp/RepoInfo.h"
33 #include "zypp/repo/PackageProvider.h"
35 #include "zypp/ResPoolProxy.h"
37 #include "zypp/sat/Pool.h"
38 #include "zypp/sat/LocaleSupport.h"
39 #include "zypp/sat/LookupAttr.h"
40 #include "zypp/sat/SolvableSet.h"
41 #include "zypp/sat/SolvIterMixin.h"
42 #include "zypp/sat/detail/PoolImpl.h"
43 #include "zypp/PoolQuery.h"
45 #include "zypp/parser/ProductConfReader.h"
47 #include <boost/mpl/int.hpp>
51 using namespace zypp::functor;
52 using namespace zypp::ui;
54 ///////////////////////////////////////////////////////////////////
56 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
58 ///////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////
61 { /////////////////////////////////////////////////////////////////
63 bool queryInstalledEditionHelper( const std::string & name_r,
67 if ( ed_r == Edition::noedition )
69 if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
71 if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
78 ManagedFile repoProvidePackage( const PoolItem & pi )
80 ResPool _pool( getZYpp()->pool() );
81 repo::RepoMediaAccess _access;
83 // Redirect PackageProvider queries for installed editions
84 // (in case of patch/delta rpm processing) to rpmDb.
85 repo::PackageProviderPolicy packageProviderPolicy;
86 packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
88 Package::constPtr p = asKind<Package>(pi.resolvable());
90 // Build a repository list for repos
91 // contributing to the pool
92 repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
93 _pool.knownRepositoriesEnd() ) );
94 repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
95 return pkgProvider.providePackage();
98 /////////////////////////////////////////////////////////////////
100 ///////////////////////////////////////////////////////////////////
101 ///////////////////////////////////////////////////////////////////
105 template <class _LIterator, class _RIterator, class _Function>
106 inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
107 _RIterator rbegin_r, _RIterator rend_r,
111 for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
113 for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
116 if ( ! fnc_r( *lit, *rit ) )
125 void dbgDu( Selectable::Ptr sel )
127 if ( sel->installedObj() )
129 DBG << "i: " << sel->installedObj() << endl
130 << sel->installedObj()->diskusage() << endl;
132 if ( sel->candidateObj() )
134 DBG << "c: " << sel->candidateObj() << endl
135 << sel->candidateObj()->diskusage() << endl;
138 << getZYpp()->diskUsage() << endl;
141 ///////////////////////////////////////////////////////////////////
143 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
146 Package::constPtr p( asKind<Package>(pi) );
149 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
151 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
153 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
154 l = Locale( "dsdf" );
155 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
158 OUTS( downloadSize );
159 OUTS( sourcePkgName );
160 OUTS( sourcePkgEdition );
172 bool operator()( const PoolItem & obj_r )
174 //MIL << obj_r << endl;
175 //DBG << " -> " << obj_r->satSolvable() << endl;
180 bool operator()( const sat::Solvable & obj_r )
182 //dumpOn( MIL, obj_r ) << endl;
187 ///////////////////////////////////////////////////////////////////
188 struct SetTransactValue
190 SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
191 : _newVal( newVal_r )
192 , _causer( causer_r )
195 ResStatus::TransactValue _newVal;
196 ResStatus::TransactByValue _causer;
198 bool operator()( const PoolItem & pi ) const
200 bool ret = pi.status().setTransactValue( _newVal, _causer );
202 ERR << _newVal << _causer << " " << pi << endl;
207 struct StatusReset : public SetTransactValue
210 : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
214 struct StatusInstall : public SetTransactValue
217 : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
221 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
223 if ( nkp.availableEmpty() )
225 ERR << "No Item to select: " << nkp << endl;
227 ZYPP_THROW( Exception("No Item to select") );
230 if ( arch != Arch() )
232 typeof( nkp.availableBegin() ) it = nkp.availableBegin();
233 for ( ; it != nkp.availableEnd(); ++it )
235 if ( (*it)->arch() == arch )
236 return (*it).status().setTransact( true, ResStatus::USER );
240 return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
243 ///////////////////////////////////////////////////////////////////
249 //zypp::base::LogControl::TmpLineWriter shutUp;
250 rres = getZYpp()->resolver()->resolvePool();
254 ERR << "resolve " << rres << endl;
257 MIL << "resolve " << rres << endl;
263 SEC << getZYpp()->commit( ZYppCommitPolicy().dryRun(true) ) << endl;
267 ///////////////////////////////////////////////////////////////////
269 struct DigestReceive : public callback::ReceiveReport<DigestReport>
276 virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
281 virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
286 virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
288 USR << "fle " << PathInfo(file) << endl;
289 USR << "req " << requested << endl;
290 USR << "fnd " << found << endl;
295 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
297 KeyRingSignalsReceive()
301 virtual void trustedKeyAdded( const PublicKey &/*key*/ )
305 virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
311 ///////////////////////////////////////////////////////////////////
313 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
315 virtual Action requestMedia( Url & source
317 , const std::string & label
319 , const std::string & description
320 , const std::vector<std::string> & devices
321 , unsigned int & dev_current )
323 SEC << __FUNCTION__ << endl
324 << " " << source << endl
325 << " " << mediumNr << endl
326 << " " << label << endl
327 << " " << error << endl
328 << " " << description << endl
329 << " " << devices << endl
330 << " " << dev_current << endl;
335 ///////////////////////////////////////////////////////////////////
340 bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
341 { return cont.find( val ) != cont.end(); }
343 ///////////////////////////////////////////////////////////////////
345 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
347 SEC << *l << " - " << *r << endl;
348 INT << "== " << (l==r) << endl;
349 INT << "!= " << (l!=r) << endl;
352 bool isTrue() { return true; }
353 bool isFalse() { return false; }
357 for ( int i = -3; i < 30; ++i )
359 DBG << i << '\t' << IdString( i ) << endl;
363 void ttt( const char * lhs, const char * rhs )
365 DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
372 template <class _MemFun, class _Value>
376 HasValue( _MemFun fun_r, _Value val_r )
377 : _fun( fun_r ), _val( val_r )
380 bool operator()( const _Tp & obj_r ) const
381 { return( _fun && (obj_r.*_fun)() == _val ); }
387 template <class _MemFun, class _Value>
388 HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
389 { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
395 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
398 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
399 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
402 _TestO<L> testO( const L & lhs )
403 { return _TestO<L>( lhs ); }
405 template <class L, class R>
406 void testCMP( const L & lhs, const R & rhs )
408 MIL << "LHS " << testO(lhs) << endl;
409 MIL << "RHS " << rhs << endl;
411 #define OUTS(S) DBG << #S << ": " << (S) << endl
412 OUTS( lhs.compare(rhs) );
426 /******************************************************************
428 ** FUNCTION NAME : main
429 ** FUNCTION TYPE : int
431 int main( int argc, char * argv[] )
435 zypp::base::LogControl::instance().logToStdErr();
436 INT << "===[START]==========================================" << endl;
439 ResPool pool( ResPool::instance() );
440 sat::Pool satpool( sat::Pool::instance() );
444 Measure x( "INIT TARGET" );
447 zypp::base::LogControl::TmpLineWriter shutUp;
448 getZYpp()->initializeTarget( sysRoot );
450 getZYpp()->target()->load();
456 RepoManager repoManager( makeRepoManager( sysRoot ) );
457 RepoInfoList repos = repoManager.knownRepositories();
460 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
462 RepoInfo & nrepo( *it );
463 SEC << nrepo << endl;
465 if ( ! nrepo.enabled() )
468 if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
470 if ( repoManager.isCached( nrepo ) )
472 SEC << "cleanCache" << endl;
473 repoManager.cleanCache( nrepo );
475 SEC << "refreshMetadata" << endl;
476 repoManager.refreshMetadata( nrepo );
477 SEC << "buildCache" << endl;
478 repoManager.buildCache( nrepo );
482 // create from cache:
484 Measure x( "CREATE FROM CACHE" );
485 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
487 RepoInfo & nrepo( *it );
488 if ( ! nrepo.enabled() )
491 Measure x( "CREATE FROM CACHE "+nrepo.alias() );
494 repoManager.loadFromCache( nrepo );
496 catch ( const Exception & exp )
498 MIL << "Try to rebuild cache..." << endl;
499 SEC << "cleanCache" << endl;
500 repoManager.cleanCache( nrepo );
501 SEC << "buildCache" << endl;
502 repoManager.buildCache( nrepo );
503 SEC << "Create from cache" << endl;
504 repoManager.loadFromCache( nrepo );
507 USR << "pool: " << pool << endl;
512 dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
513 USR << "pool: " << pool << endl;
515 ///////////////////////////////////////////////////////////////////
519 Measure x( "Upgrade" );
521 getZYpp()->resolver()->doUpgrade( u );
523 ///////////////////////////////////////////////////////////////////
524 ///////////////////////////////////////////////////////////////////
526 ui::Selectable::Ptr item( ui::Selectable::get( "amarok" ) );
527 MIL << dump(item) << endl;
529 ///////////////////////////////////////////////////////////////////
530 INT << "===[END]============================================" << endl << endl;
531 zypp::base::LogControl::instance().logNothing();
536 PoolItem pi ( getPi<Package>("amarok") );
540 pi.status().setTransact( true, ResStatus::USER );
542 vdumpPoolStats( USR << "Transacting:"<< endl,
543 make_filter_begin<resfilter::ByTransact>(pool),
544 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
548 //SEC << zypp::getZYpp()->diskUsage() << endl;
550 //vdumpPoolStats( USR << "Pool:"<< endl, pool.begin(), pool.end() ) << endl;
552 sat::WhatProvides prodcap( Capability("product()") );
553 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
554 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
556 prodcap.poolItemBegin()->status().setTransact( true, ResStatus::APPL_LOW );
557 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
558 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
560 pool.byKindBegin<Product>()->status().setTransact( true, ResStatus::USER );
561 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
562 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
564 prodcap.poolItemBegin()->status().setLock( true, ResStatus::USER );
565 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
566 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
570 //std::for_each( pool.begin(), pool.end(), Xprint() );
572 ///////////////////////////////////////////////////////////////////
573 INT << "===[END]============================================" << endl << endl;
574 zypp::base::LogControl::instance().logNothing();
577 catch ( const Exception & exp )
579 INT << exp << endl << exp.historyAsString();