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/base/Flags.h>
15 #include <zypp/AutoDispose.h>
17 #include "zypp/ResPoolProxy.h"
19 #include "zypp/ZYppCallbacks.h"
20 #include "zypp/ResPool.h"
21 #include "zypp/ResFilters.h"
22 #include "zypp/ResObjects.h"
23 #include "zypp/Digest.h"
24 #include "zypp/PackageKeyword.h"
25 #include "zypp/TmpPath.h"
26 #include "zypp/ManagedFile.h"
27 #include "zypp/NameKindProxy.h"
28 #include "zypp/pool/GetResolvablesToInsDel.h"
30 #include "zypp/RepoManager.h"
31 #include "zypp/Repository.h"
32 #include "zypp/RepoInfo.h"
34 #include "zypp/repo/PackageProvider.h"
36 #include "zypp/ResPoolProxy.h"
38 #include "zypp/sat/Pool.h"
39 #include "zypp/sat/LocaleSupport.h"
40 #include "zypp/sat/LookupAttr.h"
41 #include "zypp/sat/SolvableSet.h"
42 #include "zypp/sat/SolvIterMixin.h"
43 #include "zypp/sat/detail/PoolImpl.h"
44 #include "zypp/sat/WhatObsoletes.h"
45 #include "zypp/PoolQuery.h"
46 #include "zypp/ServiceInfo.h"
48 #include "zypp/parser/ProductConfReader.h"
50 #include <boost/mpl/int.hpp>
54 using namespace zypp::functor;
55 using namespace zypp::ui;
57 ///////////////////////////////////////////////////////////////////
59 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
61 ///////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////
64 { /////////////////////////////////////////////////////////////////
66 bool queryInstalledEditionHelper( const std::string & name_r,
70 if ( ed_r == Edition::noedition )
72 if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
74 if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
81 ManagedFile repoProvidePackage( const PoolItem & pi )
83 ResPool _pool( getZYpp()->pool() );
84 repo::RepoMediaAccess _access;
86 // Redirect PackageProvider queries for installed editions
87 // (in case of patch/delta rpm processing) to rpmDb.
88 repo::PackageProviderPolicy packageProviderPolicy;
89 packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
91 Package::constPtr p = asKind<Package>(pi.resolvable());
93 // Build a repository list for repos
94 // contributing to the pool
95 repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
96 _pool.knownRepositoriesEnd() ) );
97 repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
98 return pkgProvider.providePackage();
101 /////////////////////////////////////////////////////////////////
103 ///////////////////////////////////////////////////////////////////
104 ///////////////////////////////////////////////////////////////////
108 template <class _LIterator, class _RIterator, class _Function>
109 inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
110 _RIterator rbegin_r, _RIterator rend_r,
114 for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
116 for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
119 if ( ! fnc_r( *lit, *rit ) )
128 void dbgDu( Selectable::Ptr sel )
130 if ( sel->installedObj() )
132 DBG << "i: " << sel->installedObj() << endl
133 << sel->installedObj()->diskusage() << endl;
135 if ( sel->candidateObj() )
137 DBG << "c: " << sel->candidateObj() << endl
138 << sel->candidateObj()->diskusage() << endl;
141 << getZYpp()->diskUsage() << endl;
144 ///////////////////////////////////////////////////////////////////
146 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
149 Package::constPtr p( asKind<Package>(pi) );
152 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
154 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
156 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
157 l = Locale( "dsdf" );
158 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
161 OUTS( downloadSize );
162 OUTS( sourcePkgName );
163 OUTS( sourcePkgEdition );
175 bool operator()( const PoolItem & obj_r )
177 //MIL << obj_r << endl;
178 //DBG << " -> " << obj_r->satSolvable() << endl;
183 bool operator()( const sat::Solvable & obj_r )
185 //dumpOn( MIL, obj_r ) << endl;
190 ///////////////////////////////////////////////////////////////////
191 struct SetTransactValue
193 SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
194 : _newVal( newVal_r )
195 , _causer( causer_r )
198 ResStatus::TransactValue _newVal;
199 ResStatus::TransactByValue _causer;
201 bool operator()( const PoolItem & pi ) const
203 bool ret = pi.status().setTransactValue( _newVal, _causer );
205 ERR << _newVal << _causer << " " << pi << endl;
210 struct StatusReset : public SetTransactValue
213 : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
217 struct StatusInstall : public SetTransactValue
220 : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
224 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
226 if ( nkp.availableEmpty() )
228 ERR << "No Item to select: " << nkp << endl;
230 ZYPP_THROW( Exception("No Item to select") );
233 if ( arch != Arch() )
235 typeof( nkp.availableBegin() ) it = nkp.availableBegin();
236 for ( ; it != nkp.availableEnd(); ++it )
238 if ( (*it)->arch() == arch )
239 return (*it).status().setTransact( true, ResStatus::USER );
243 return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
246 ///////////////////////////////////////////////////////////////////
252 //zypp::base::LogControl::TmpLineWriter shutUp;
253 rres = getZYpp()->resolver()->resolvePool();
257 ERR << "resolve " << rres << endl;
260 MIL << "resolve " << rres << endl;
266 ZYppCommitPolicy pol;
268 pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
269 SEC << getZYpp()->commit( pol ) << endl;
275 getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
278 ///////////////////////////////////////////////////////////////////
280 struct DigestReceive : public callback::ReceiveReport<DigestReport>
287 virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
292 virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
297 virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
299 USR << "fle " << PathInfo(file) << endl;
300 USR << "req " << requested << endl;
301 USR << "fnd " << found << endl;
306 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
308 KeyRingSignalsReceive()
312 virtual void trustedKeyAdded( const PublicKey &/*key*/ )
316 virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
322 ///////////////////////////////////////////////////////////////////
324 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
326 virtual Action requestMedia( Url & source
328 , const std::string & label
330 , const std::string & description
331 , const std::vector<std::string> & devices
332 , unsigned int & dev_current )
334 SEC << __FUNCTION__ << endl
335 << " " << source << endl
336 << " " << mediumNr << endl
337 << " " << label << endl
338 << " " << error << endl
339 << " " << description << endl
340 << " " << devices << endl
341 << " " << dev_current << endl;
346 ///////////////////////////////////////////////////////////////////
351 bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
352 { return cont.find( val ) != cont.end(); }
354 ///////////////////////////////////////////////////////////////////
356 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
358 SEC << *l << " - " << *r << endl;
359 INT << "== " << (l==r) << endl;
360 INT << "!= " << (l!=r) << endl;
363 bool isTrue() { return true; }
364 bool isFalse() { return false; }
368 for ( int i = -3; i < 30; ++i )
370 DBG << i << '\t' << IdString( i ) << endl;
374 void ttt( const char * lhs, const char * rhs )
376 DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
383 template <class _MemFun, class _Value>
387 HasValue( _MemFun fun_r, _Value val_r )
388 : _fun( fun_r ), _val( val_r )
391 bool operator()( const _Tp & obj_r ) const
392 { return( _fun && (obj_r.*_fun)() == _val ); }
398 template <class _MemFun, class _Value>
399 HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
400 { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
406 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
409 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
410 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
413 _TestO<L> testO( const L & lhs )
414 { return _TestO<L>( lhs ); }
416 template <class L, class R>
417 void testCMP( const L & lhs, const R & rhs )
419 MIL << "LHS " << testO(lhs) << endl;
420 MIL << "RHS " << rhs << endl;
422 #define OUTS(S) DBG << #S << ": " << (S) << endl
423 OUTS( lhs.compare(rhs) );
434 /******************************************************************
436 ** FUNCTION NAME : main
437 ** FUNCTION TYPE : int
439 int main( int argc, char * argv[] )
443 zypp::base::LogControl::instance().logToStdErr();
444 INT << "===[START]==========================================" << endl;
448 ServiceInfo s( "STest", Url("dir:///somewhere") );
450 s.addCatalogToEnable( "foo" );
451 s.addCatalogToEnable( "ba a" );
452 s.addCatalogToEnable( "kaa" );
454 RepoManager repoManager( makeRepoManager( sysRoot ) );
455 RepoInfoList repos = repoManager.knownRepositories();
456 RepoInfoList services = repoManager.knownServices();
462 ///////////////////////////////////////////////////////////////////
463 INT << "===[END]============================================" << endl << endl;
464 zypp::base::LogControl::instance().logNothing();
468 ResPool pool( ResPool::instance() );
469 sat::Pool satpool( sat::Pool::instance() );
473 Measure x( "INIT TARGET" );
476 zypp::base::LogControl::TmpLineWriter shutUp;
477 getZYpp()->initializeTarget( sysRoot );
479 getZYpp()->target()->load();
480 USR << getZYpp()->target()->targetDistribution() << endl;
481 USR << getZYpp()->target()->targetDistributionRelease() << endl;
482 USR << getZYpp()->target()->targetDistributionFlavor() << endl;
488 RepoManager repoManager( makeRepoManager( sysRoot ) );
489 RepoInfoList repos = repoManager.knownRepositories();
492 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
494 RepoInfo & nrepo( *it );
495 SEC << nrepo << endl;
497 if ( ! nrepo.enabled() )
500 if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
502 if ( repoManager.isCached( nrepo ) )
504 SEC << "cleanCache" << endl;
505 repoManager.cleanCache( nrepo );
507 SEC << "refreshMetadata" << endl;
508 //repoManager.refreshMetadata( nrepo );
509 SEC << "buildCache" << endl;
510 repoManager.buildCache( nrepo );
514 // create from cache:
516 Measure x( "CREATE FROM CACHE" );
517 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
519 RepoInfo & nrepo( *it );
520 if ( ! nrepo.enabled() )
523 Measure x( "CREATE FROM CACHE "+nrepo.alias() );
526 repoManager.loadFromCache( nrepo );
528 catch ( const Exception & exp )
530 MIL << "Try to rebuild cache..." << endl;
531 SEC << "cleanCache" << endl;
532 repoManager.cleanCache( nrepo );
533 SEC << "buildCache" << endl;
534 repoManager.buildCache( nrepo );
535 SEC << "Create from cache" << endl;
536 repoManager.loadFromCache( nrepo );
539 USR << "pool: " << pool << endl;
544 dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
545 USR << "pool: " << pool << endl;
547 ///////////////////////////////////////////////////////////////////
551 Measure x( "Upgrade" );
553 getZYpp()->resolver()->doUpgrade( u );
555 ///////////////////////////////////////////////////////////////////
556 ///////////////////////////////////////////////////////////////////
560 ///////////////////////////////////////////////////////////////////
561 INT << "===[END]============================================" << endl << endl;
562 zypp::base::LogControl::instance().logNothing();
567 PoolItem pi ( getPi<Package>("amarok") );
571 pi.status().setTransact( true, ResStatus::USER );
573 vdumpPoolStats( USR << "Transacting:"<< endl,
574 make_filter_begin<resfilter::ByTransact>(pool),
575 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
579 //SEC << zypp::getZYpp()->diskUsage() << endl;
581 //vdumpPoolStats( USR << "Pool:"<< endl, pool.begin(), pool.end() ) << endl;
583 sat::WhatProvides prodcap( Capability("product()") );
584 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
585 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
587 prodcap.poolItemBegin()->status().setTransact( true, ResStatus::APPL_LOW );
588 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
589 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
591 pool.byKindBegin<Product>()->status().setTransact( true, ResStatus::USER );
592 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
593 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
595 prodcap.poolItemBegin()->status().setLock( true, ResStatus::USER );
596 dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
597 dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
601 //std::for_each( pool.begin(), pool.end(), Xprint() );
603 ///////////////////////////////////////////////////////////////////
604 INT << "===[END]============================================" << endl << endl;
605 zypp::base::LogControl::instance().logNothing();
608 catch ( const Exception & exp )
610 INT << exp << endl << exp.historyAsString();