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/base/StrMatcher.h>
16 #include <zypp/AutoDispose.h>
18 #include <zypp/ResPoolProxy.h>
20 #include <zypp/ZYppCallbacks.h>
21 #include <zypp/ResPool.h>
22 #include <zypp/ResFilters.h>
23 #include <zypp/ResObjects.h>
24 #include <zypp/Digest.h>
25 #include <zypp/PackageKeyword.h>
26 #include <zypp/TmpPath.h>
27 #include <zypp/ManagedFile.h>
28 #include <zypp/MediaSetAccess.h>
31 #include <zypp/RepoManager.h>
32 #include <zypp/Repository.h>
33 #include <zypp/RepoInfo.h>
34 #include <zypp/TriBool.h>
35 #include <zypp/repo/PackageProvider.h>
37 #include <zypp/ResPoolProxy.h>
39 #include <zypp/sat/Pool.h>
40 #include <zypp/sat/LocaleSupport.h>
41 #include <zypp/sat/LookupAttr.h>
42 #include <zypp/sat/SolvableSet.h>
43 #include <zypp/sat/SolvIterMixin.h>
44 #include <zypp/sat/detail/PoolImpl.h>
45 #include <zypp/sat/WhatObsoletes.h>
46 #include <zypp/PoolQuery.h>
47 #include <zypp/ServiceInfo.h>
48 #include <zypp/media/MediaPriority.h>
50 #include <zypp/target/rpm/RpmDb.h>
51 #include <zypp/target/rpm/RpmHeader.h>
52 #include <zypp/target/rpm/librpmDb.h>
54 #include <boost/mpl/int.hpp>
57 using namespace zypp::functor;
58 using namespace zypp::ui;
60 ///////////////////////////////////////////////////////////////////
62 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
64 ///////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////
67 { /////////////////////////////////////////////////////////////////
69 bool queryInstalledEditionHelper( const std::string & name_r,
73 if ( ed_r == Edition::noedition )
75 if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
77 if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
84 ManagedFile repoProvidePackage( const PoolItem & pi )
86 ResPool _pool( getZYpp()->pool() );
87 repo::RepoMediaAccess _access;
89 // Redirect PackageProvider queries for installed editions
90 // (in case of patch/delta rpm processing) to rpmDb.
91 repo::PackageProviderPolicy packageProviderPolicy;
92 packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
94 Package::constPtr p = asKind<Package>(pi.resolvable());
96 // Build a repository list for repos
97 // contributing to the pool
98 repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
99 _pool.knownRepositoriesEnd() ) );
100 repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
101 return pkgProvider.providePackage();
104 /////////////////////////////////////////////////////////////////
106 ///////////////////////////////////////////////////////////////////
107 ///////////////////////////////////////////////////////////////////
111 template <class TLIterator, class TRIterator, class TFunction>
112 inline int invokeOnEach( TLIterator lbegin_r, TLIterator lend_r,
113 TRIterator rbegin_r, TRIterator rend_r,
117 for ( TLIterator lit = lbegin_r; lit != lend_r; ++lit )
119 for ( TRIterator rit = rbegin_r; rit != rend_r; ++rit )
122 if ( ! fnc_r( *lit, *rit ) )
131 ///////////////////////////////////////////////////////////////////
133 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
136 Package::constPtr p( asKind<Package>(pi) );
139 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
141 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
143 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
144 l = Locale( "dsdf" );
145 str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
148 OUTS( downloadSize );
149 OUTS( sourcePkgName );
150 OUTS( sourcePkgEdition );
162 bool operator()( const PoolItem & obj_r )
164 //MIL << obj_r << endl;
165 //DBG << " -> " << obj_r->satSolvable() << endl;
170 bool operator()( const sat::Solvable & obj_r )
172 //dumpOn( MIL, obj_r ) << endl;
177 ///////////////////////////////////////////////////////////////////
178 struct SetTransactValue
180 SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
181 : _newVal( newVal_r )
182 , _causer( causer_r )
185 ResStatus::TransactValue _newVal;
186 ResStatus::TransactByValue _causer;
188 bool operator()( const PoolItem & pi ) const
190 bool ret = pi.status().setTransactValue( _newVal, _causer );
192 ERR << _newVal << _causer << " " << pi << endl;
197 struct StatusReset : public SetTransactValue
200 : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
204 struct StatusInstall : public SetTransactValue
207 : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
211 ///////////////////////////////////////////////////////////////////
217 //zypp::base::LogControl::TmpLineWriter shutUp;
218 rres = getZYpp()->resolver()->resolvePool();
222 ERR << "resolve " << rres << endl;
225 MIL << "resolve " << rres << endl;
231 ZYppCommitPolicy pol;
233 pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
234 SEC << getZYpp()->commit( pol ) << endl;
240 getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
243 ///////////////////////////////////////////////////////////////////
245 struct DigestReceive : public callback::ReceiveReport<DigestReport>
252 virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
257 virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
262 virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
264 USR << "fle " << PathInfo(file) << endl;
265 USR << "req " << requested << endl;
266 USR << "fnd " << found << endl;
271 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
273 KeyRingSignalsReceive()
277 virtual void trustedKeyAdded( const PublicKey &/*key*/ )
281 virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
287 ///////////////////////////////////////////////////////////////////
289 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
291 virtual Action requestMedia( Url & source
293 , const std::string & label
295 , const std::string & description
296 , const std::vector<std::string> & devices
297 , unsigned int & dev_current )
299 SEC << __FUNCTION__ << endl
300 << " " << source << endl
301 << " " << mediumNr << endl
302 << " " << label << endl
303 << " " << error << endl
304 << " " << description << endl
305 << " " << devices << endl
306 << " " << dev_current << endl;
311 ///////////////////////////////////////////////////////////////////
316 bool isIn( const std::set<Tp> & cont, const typename std::set<Tp>::value_type & val )
317 { return cont.find( val ) != cont.end(); }
319 ///////////////////////////////////////////////////////////////////
321 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
323 SEC << *l << " - " << *r << endl;
324 INT << "== " << (l==r) << endl;
325 INT << "!= " << (l!=r) << endl;
328 bool isTrue() { return true; }
329 bool isFalse() { return false; }
333 for ( int i = -3; i < 30; ++i )
335 DBG << i << '\t' << IdString( i ) << endl;
339 void ttt( const char * lhs, const char * rhs )
341 DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
348 template <class TMemFun, class TValue>
352 HasValue( TMemFun fun_r, TValue val_r )
353 : _fun( fun_r ), _val( val_r )
356 bool operator()( const Tp & obj_r ) const
357 { return( _fun && (obj_r.*_fun)() == _val ); }
363 template <class TMemFun, class TValue>
364 HasValue<TMemFun, TValue> byValue( TMemFun fun_r, TValue val_r )
365 { return HasValue<TMemFun, TValue>( fun_r, val_r ); }
371 struct TestO { TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
374 std::ostream & operator<<( std::ostream & str, const TestO<L> & obj )
375 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
378 TestO<L> testO( const L & lhs )
379 { return TestO<L>( lhs ); }
381 template <class L, class R>
382 void testCMP( const L & lhs, const R & rhs )
384 MIL << "LHS " << testO(lhs) << endl;
385 MIL << "RHS " << rhs << endl;
387 #define OUTS(S) DBG << #S << ": " << (S) << endl
388 OUTS( lhs.compare(rhs) );
398 #include <zypp/Locks.h>
399 #include <zypp/target/HardLocksFile.h>
400 inline PoolQuery makeTrivialQuery( IdString ident_r )
402 sat::Solvable::SplitIdent ident( ident_r );
405 q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
406 q.addKind( ident.kind() );
408 q.setCaseSensitive(true);
411 inline PoolQuery makeTrivialQuery( const char * ch )
412 { return makeTrivialQuery( IdString(ch) ); }
415 static unsigned i = 0;
416 ResPool pool( ResPool::instance() );
417 target::HardLocksFile::Data newdata;
418 pool.getHardLockQueries( newdata );
419 SEC << '[' << i++ << ']' << newdata << endl;
423 Capability guessPackageSpec( const std::string & str_r )
425 return Capability::guessPackageSpec( str_r );
431 void cut( const Capability & cap )
433 CapDetail detail( cap.detail() );
434 if ( detail.isSimple() )
436 MIL << detail.kind() << ": " << detail.name();
437 if ( detail.hasArch() )
438 MIL << " (" << detail.arch() << ")";
439 if ( detail.isVersioned() )
440 MIL << " " << detail.op() << " " << detail.ed();
445 MIL << "--- ??? ---" << endl;
449 namespace zypp { namespace target {
450 void XRunUpdateMessages( const Pathname & root_r,
451 const Pathname & messagesPath_r,
452 const std::vector<sat::Solvable> & checkPackages_r,
453 ZYppCommitResult & result_r );
456 using zypp::target::XRunUpdateMessages;
458 /******************************************************************
460 ** FUNCTION NAME : main
461 ** FUNCTION TYPE : int
463 int main( int argc, char * argv[] )
467 // download the repo index file
468 media::MediaManager mediamanager;
469 media::MediaAccessId mid = mediamanager.open( Url("http://download.opensuse.org") );
470 mediamanager.attach( mid );
472 zypp::base::LogControl::instance().logToStdErr();
473 INT << "===[START]==========================================" << endl;
476 ResPool pool( ResPool::instance() );
477 sat::Pool satpool( sat::Pool::instance() );
481 Measure x( "INIT TARGET" );
484 //zypp::base::LogControl::TmpLineWriter shutUp;
485 getZYpp()->initializeTarget( sysRoot );
487 getZYpp()->target()->load();
488 USR << "baseproduct: " << getZYpp()->target()->baseProduct() << endl;
489 USR << "targetDistribution: " << getZYpp()->target()->targetDistribution() << endl;
490 USR << "targetDistributionRelease: " << getZYpp()->target()->targetDistributionRelease() << endl;
491 dumpRange( USR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
497 RepoManager repoManager( makeRepoManager( sysRoot ) );
498 ServiceInfoList services = repoManager.knownServices();
500 for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
502 ServiceInfo & nservice( *it );
503 SEC << nservice << endl;
505 if ( ! nservice.enabled() )
508 repoManager.refreshService( nservice );
514 RepoManager repoManager( makeRepoManager( sysRoot ) );
515 RepoInfoList repos = repoManager.knownRepositories();
518 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
520 RepoInfo & nrepo( *it );
521 SEC << nrepo << endl;
523 if ( ! nrepo.enabled() )
526 if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
528 if ( repoManager.isCached( nrepo ) )
530 SEC << "cleanCache" << endl;
531 repoManager.cleanCache( nrepo );
533 SEC << "refreshMetadata" << endl;
534 //repoManager.refreshMetadata( nrepo );
535 SEC << "buildCache" << endl;
536 repoManager.buildCache( nrepo );
540 // create from cache:
542 Measure x( "CREATE FROM CACHE" );
543 for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
545 RepoInfo & nrepo( *it );
546 if ( ! nrepo.enabled() )
549 Measure x( "CREATE FROM CACHE "+nrepo.alias() );
552 repoManager.loadFromCache( nrepo );
554 catch ( const Exception & exp )
556 MIL << "Try to rebuild cache..." << endl;
557 SEC << "cleanCache" << endl;
558 repoManager.cleanCache( nrepo );
559 SEC << "buildCache" << endl;
560 repoManager.buildCache( nrepo );
561 SEC << "Create from cache" << endl;
562 repoManager.loadFromCache( nrepo );
565 //USR << "pool: " << pool << endl;
570 dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
571 USR << "pool: " << pool << endl;
573 ///////////////////////////////////////////////////////////////////
577 Measure x( "Upgrade" );
578 getZYpp()->resolver()->doUpgrade();
581 ///////////////////////////////////////////////////////////////////
588 getZYpp()->resolver()->addRequire( Capability("amarok") );
589 pool.byKindBegin<Package>()->status().setTransact( true, ResStatus::USER );
591 vdumpPoolStats( USR << "Transacting:"<< endl,
592 make_filter_begin<resfilter::ByTransact>(pool),
593 make_filter_end<resfilter::ByTransact>(pool) ) << endl;
596 //////////////////////////////////////////////////////////////////
597 INT << "===[END]============================================" << endl << endl;
598 zypp::base::LogControl::instance().logNothing();
601 catch ( const Exception & exp )
603 INT << exp << endl << exp.historyAsString();