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) );
433 /******************************************************************
435 ** FUNCTION NAME : main
436 ** FUNCTION TYPE : int
438 int main( int argc, char * argv[] )
442 zypp::base::LogControl::instance().logToStdErr();
443 INT << "===[START]==========================================" << endl;
449 RepoManager repoManager( makeRepoManager( sysRoot ) );
451 ServiceInfoList services = repoManager.knownServices();
452 WAR << services << endl;
454 //RepoInfoList repos = repoManager.knownRepositories();
455 //DBG << repos << endl;
458 ServiceInfo s( repoManager.getService( "STest" ) );
459 if ( s == ServiceInfo::noService )
461 Measure x( "Add service STest" );
462 repoManager.addService( "STest", Url("dir:///Local/Service") );
463 s = repoManager.getService( "STest" );
464 USR << "Add service " << s << endl;
468 Measure x( "Refresh service STest" );
469 repoManager.refreshService( s );
474 nrepo.setAlias( alias );
475 nrepo.setName( alias );
476 nrepo.setEnabled( true );
477 nrepo.setAutorefresh( false );
478 nrepo.addBaseUrl( Url(url) );
480 if ( ! repoManager.isCached( nrepo ) )
482 repoManager.buildCache( nrepo );
485 repoManager.loadFromCache( nrepo );
488 ///////////////////////////////////////////////////////////////////
489 INT << "===[END]============================================" << endl << endl;
490 zypp::base::LogControl::instance().logNothing();
494 ResPool pool( ResPool::instance() );
495 sat::Pool satpool( sat::Pool::instance() );
499 Measure x( "INIT TARGET" );
502 zypp::base::LogControl::TmpLineWriter shutUp;
503 getZYpp()->initializeTarget( sysRoot );
505 getZYpp()->target()->load();
506 USR << getZYpp()->target()->targetDistribution() << endl;
507 USR << getZYpp()->target()->targetDistributionRelease() << endl;
508 dumpRange( USR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
514 RepoManager repoManager( makeRepoManager( sysRoot ) );
515 ServiceInfoList services = repoManager.knownServices();
517 for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
519 ServiceInfo & nservice( *it );
520 SEC << nservice << endl;
522 if ( ! nservice.enabled() )
525 repoManager.refreshService( nservice );
531 RepoManager repoManager( makeRepoManager( sysRoot ) );
532 RepoInfoList repos = repoManager.knownRepositories();
535 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
537 RepoInfo & nrepo( *it );
538 SEC << nrepo << endl;
540 if ( ! nrepo.enabled() )
543 if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
545 if ( repoManager.isCached( nrepo ) )
547 SEC << "cleanCache" << endl;
548 repoManager.cleanCache( nrepo );
550 SEC << "refreshMetadata" << endl;
551 //repoManager.refreshMetadata( nrepo );
552 SEC << "buildCache" << endl;
553 repoManager.buildCache( nrepo );
557 // create from cache:
559 Measure x( "CREATE FROM CACHE" );
560 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
562 RepoInfo & nrepo( *it );
563 if ( ! nrepo.enabled() )
566 Measure x( "CREATE FROM CACHE "+nrepo.alias() );
569 repoManager.loadFromCache( nrepo );
571 catch ( const Exception & exp )
573 MIL << "Try to rebuild cache..." << endl;
574 SEC << "cleanCache" << endl;
575 repoManager.cleanCache( nrepo );
576 SEC << "buildCache" << endl;
577 repoManager.buildCache( nrepo );
578 SEC << "Create from cache" << endl;
579 repoManager.loadFromCache( nrepo );
582 USR << "pool: " << pool << endl;
587 dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
588 USR << "pool: " << pool << endl;
590 ///////////////////////////////////////////////////////////////////
594 Measure x( "Upgrade" );
596 getZYpp()->resolver()->doUpgrade( u );
599 ///////////////////////////////////////////////////////////////////
600 ///////////////////////////////////////////////////////////////////
604 PoolItem pi ( getPi<Patch>("xorg-x11-Xvnc") );
608 Patch::constPtr p( pi->asKind<Patch>() );
609 INT << p->contents() << endl;
615 getZYpp()->resolver()->addRequire( Capability("amarok") );
617 vdumpPoolStats( USR << "Transacting:"<< endl,
618 make_filter_begin<resfilter::ByTransact>(pool),
619 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
622 //////////////////////////////////////////////////////////////////
623 INT << "===[END]============================================" << endl << endl;
624 zypp::base::LogControl::instance().logNothing();
627 catch ( const Exception & exp )
629 INT << exp << endl << exp.historyAsString();