- Fix computation of Product::flavor.
[platform/upstream/libzypp.git] / devel / devel.ma / NewPool.cc
1 #include "Tools.h"
2
3 #include "zypp/PoolQueryResult.h"
4
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>
16
17 #include "zypp/ResPoolProxy.h"
18
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"
29
30 #include "zypp/RepoManager.h"
31 #include "zypp/Repository.h"
32 #include "zypp/RepoInfo.h"
33
34 #include "zypp/repo/PackageProvider.h"
35
36 #include "zypp/ResPoolProxy.h"
37
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"
47
48 #include "zypp/parser/ProductConfReader.h"
49
50 #include <boost/mpl/int.hpp>
51
52 using namespace std;
53 using namespace zypp;
54 using namespace zypp::functor;
55 using namespace zypp::ui;
56
57 ///////////////////////////////////////////////////////////////////
58
59 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
60
61 ///////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////
63 namespace zypp
64 { /////////////////////////////////////////////////////////////////
65
66 bool queryInstalledEditionHelper( const std::string & name_r,
67                                   const Edition &     ed_r,
68                                   const Arch &        arch_r )
69 {
70   if ( ed_r == Edition::noedition )
71     return true;
72   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
73     return true;
74   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
75     return true;
76
77   return false;
78 }
79
80
81 ManagedFile repoProvidePackage( const PoolItem & pi )
82 {
83   ResPool _pool( getZYpp()->pool() );
84   repo::RepoMediaAccess _access;
85
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 );
90
91   Package::constPtr p = asKind<Package>(pi.resolvable());
92
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();
99 }
100
101   /////////////////////////////////////////////////////////////////
102 } // namespace zypp
103 ///////////////////////////////////////////////////////////////////
104 ///////////////////////////////////////////////////////////////////
105
106 namespace zypp
107 {
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,
111                                _Function fnc_r )
112       {
113         int cnt = 0;
114         for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
115         {
116           for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
117           {
118             ++cnt;
119             if ( ! fnc_r( *lit, *rit ) )
120               return -cnt;
121           }
122         }
123         return cnt;
124       }
125 }
126
127
128 void dbgDu( Selectable::Ptr sel )
129 {
130   if ( sel->installedObj() )
131   {
132     DBG << "i: " << sel->installedObj() << endl
133         << sel->installedObj()->diskusage() << endl;
134   }
135   if ( sel->candidateObj() )
136   {
137     DBG << "c: " << sel->candidateObj() << endl
138         << sel->candidateObj()->diskusage() << endl;
139   }
140   INT << sel << endl
141       << getZYpp()->diskUsage() << endl;
142 }
143
144 ///////////////////////////////////////////////////////////////////
145
146 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
147 {
148   str << pi << endl;
149   Package::constPtr p( asKind<Package>(pi) );
150   if ( p )
151   {
152 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
153     Locale l( "de" );
154     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
155     l = Locale( "fr" );
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;
159     OUTS( summary );
160     OUTS( installsize );
161     OUTS( downloadSize );
162     OUTS( sourcePkgName );
163     OUTS( sourcePkgEdition );
164     OUTS( checksum );
165     OUTS( location );
166 #undef OUTS
167
168
169   }
170   return str;
171 }
172
173 struct Xprint
174 {
175   bool operator()( const PoolItem & obj_r )
176   {
177     //MIL << obj_r << endl;
178     //DBG << " -> " << obj_r->satSolvable() << endl;
179
180     return true;
181   }
182
183   bool operator()( const sat::Solvable & obj_r )
184   {
185     //dumpOn( MIL, obj_r ) << endl;
186     return true;
187   }
188 };
189
190 ///////////////////////////////////////////////////////////////////
191 struct SetTransactValue
192 {
193   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
194   : _newVal( newVal_r )
195   , _causer( causer_r )
196   {}
197
198   ResStatus::TransactValue   _newVal;
199   ResStatus::TransactByValue _causer;
200
201   bool operator()( const PoolItem & pi ) const
202   {
203     bool ret = pi.status().setTransactValue( _newVal, _causer );
204     if ( ! ret )
205       ERR << _newVal <<  _causer << " " << pi << endl;
206     return ret;
207   }
208 };
209
210 struct StatusReset : public SetTransactValue
211 {
212   StatusReset()
213   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
214   {}
215 };
216
217 struct StatusInstall : public SetTransactValue
218 {
219   StatusInstall()
220   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
221   {}
222 };
223
224 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
225 {
226   if ( nkp.availableEmpty() )
227   {
228     ERR << "No Item to select: " << nkp << endl;
229     return false;
230     ZYPP_THROW( Exception("No Item to select") );
231   }
232
233   if ( arch != Arch() )
234   {
235     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
236     for ( ; it != nkp.availableEnd(); ++it )
237     {
238       if ( (*it)->arch() == arch )
239         return (*it).status().setTransact( true, ResStatus::USER );
240     }
241   }
242
243   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
244 }
245
246 ///////////////////////////////////////////////////////////////////
247
248 bool solve()
249 {
250   bool rres = false;
251   {
252     //zypp::base::LogControl::TmpLineWriter shutUp;
253     rres = getZYpp()->resolver()->resolvePool();
254   }
255   if ( ! rres )
256   {
257     ERR << "resolve " << rres << endl;
258     return false;
259   }
260   MIL << "resolve " << rres << endl;
261   return true;
262 }
263
264 bool install()
265 {
266   ZYppCommitPolicy pol;
267   pol.dryRun(true);
268   pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
269   SEC << getZYpp()->commit( pol ) << endl;
270   return true;
271 }
272
273 void testcase()
274 {
275   getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
276 }
277
278 ///////////////////////////////////////////////////////////////////
279
280 struct DigestReceive : public callback::ReceiveReport<DigestReport>
281 {
282   DigestReceive()
283   {
284     connect();
285   }
286
287   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
288   {
289     USR << endl;
290     return false;
291   }
292   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
293   {
294     USR << endl;
295     return false;
296   }
297   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
298   {
299     USR << "fle " << PathInfo(file) << endl;
300     USR << "req " << requested << endl;
301     USR << "fnd " << found << endl;
302     return false;
303   }
304 };
305
306 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
307 {
308   KeyRingSignalsReceive()
309   {
310     connect();
311   }
312   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
313   {
314     USR << endl;
315   }
316   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
317   {
318     USR << endl;
319   }
320 };
321
322 ///////////////////////////////////////////////////////////////////
323
324 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
325 {
326   virtual Action requestMedia( Url & source
327                                , unsigned mediumNr
328                                , const std::string & label
329                                , Error error
330                                , const std::string & description
331                                , const std::vector<std::string> & devices
332                                , unsigned int & dev_current )
333   {
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;
342     return IGNORE;
343   }
344 };
345
346 ///////////////////////////////////////////////////////////////////
347
348 namespace container
349 {
350   template<class _Tp>
351     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
352     { return cont.find( val ) != cont.end(); }
353 }
354 ///////////////////////////////////////////////////////////////////
355
356 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
357 {
358   SEC << *l << " - " << *r << endl;
359   INT << "== " << (l==r) << endl;
360   INT << "!= " << (l!=r) << endl;
361 }
362
363 bool isTrue()  { return true; }
364 bool isFalse() { return false; }
365
366 void dumpIdStr()
367 {
368   for ( int i = -3; i < 30; ++i )
369   {
370     DBG << i << '\t' << IdString( i ) << endl;
371   }
372 }
373
374 void ttt( const char * lhs, const char * rhs )
375 {
376   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
377 }
378
379 namespace zypp
380 {
381 namespace filter
382 {
383   template <class _MemFun, class _Value>
384   class HasValue
385   {
386     public:
387       HasValue( _MemFun fun_r, _Value val_r )
388       : _fun( fun_r ), _val( val_r )
389       {}
390       template <class _Tp>
391       bool operator()( const _Tp & obj_r ) const
392       { return( _fun && (obj_r.*_fun)() == _val ); }
393     private:
394       _MemFun _fun;
395       _Value  _val;
396   };
397
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 ); }
401 }
402
403 }
404
405 template <class L>
406 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
407
408 template <class L>
409 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
410 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
411
412 template <class L>
413 _TestO<L> testO( const L & lhs )
414 { return _TestO<L>( lhs ); }
415
416 template <class L, class R>
417 void testCMP( const L & lhs, const R & rhs )
418 {
419   MIL << "LHS " << testO(lhs) << endl;
420   MIL << "RHS " << rhs << endl;
421
422 #define OUTS(S) DBG << #S << ": " << (S) << endl
423   OUTS( lhs.compare(rhs) );
424   OUTS( lhs != rhs );
425   OUTS( lhs <  rhs );
426   OUTS( lhs <= rhs );
427   OUTS( lhs == rhs );
428   OUTS( lhs >= rhs );
429   OUTS( lhs >  rhs );
430 #undef OUTS
431 }
432
433 /******************************************************************
434 **
435 **      FUNCTION NAME : main
436 **      FUNCTION TYPE : int
437 */
438 int main( int argc, char * argv[] )
439 try {
440   --argc;
441   ++argv;
442   zypp::base::LogControl::instance().logToStdErr();
443   INT << "===[START]==========================================" << endl;
444   ZConfig::instance();
445
446   {
447   Capability cap("flavor(dvd9)");
448   DBG << cap.detail().isSimple() << endl;
449   DBG << cap.detail().isNamed() << endl;
450   DBG << cap.detail().name() << endl;
451   std::string capstr = str::stripPrefix( cap.asString(), "flavor(" );
452   DBG << capstr << endl;
453   capstr.erase(capstr.size()-1);
454   DBG << capstr << endl;
455   }
456
457
458   ///////////////////////////////////////////////////////////////////
459   INT << "===[END]============================================" << endl << endl;
460   zypp::base::LogControl::instance().logNothing();
461   return 0;
462
463 #if 0
464
465   RepoManager repoManager( makeRepoManager( sysRoot ) );
466
467   ServiceInfoList services = repoManager.knownServices();
468   WAR << services << endl;
469
470   //RepoInfoList repos = repoManager.knownRepositories();
471   //DBG << repos << endl;
472
473
474   ServiceInfo s( repoManager.getService( "STest" ) );
475   if ( s == ServiceInfo::noService )
476   {
477     Measure x( "Add service STest" );
478     repoManager.addService( "STest", Url("dir:///Local/Service") );
479     s = repoManager.getService( "STest" );
480     USR << "Add service " << s << endl;
481   }
482
483   {
484     Measure x( "Refresh service STest" );
485     repoManager.refreshService( s );
486   }
487
488
489   RepoInfo nrepo;
490   nrepo.setAlias( alias );
491   nrepo.setName( alias );
492   nrepo.setEnabled( true );
493   nrepo.setAutorefresh( false );
494   nrepo.addBaseUrl( Url(url) );
495
496   if ( ! repoManager.isCached( nrepo ) )
497   {
498     repoManager.buildCache( nrepo );
499   }
500
501   repoManager.loadFromCache( nrepo );
502
503
504   ///////////////////////////////////////////////////////////////////
505   INT << "===[END]============================================" << endl << endl;
506   zypp::base::LogControl::instance().logNothing();
507   return 0;
508 #endif
509
510   ResPool   pool( ResPool::instance() );
511   sat::Pool satpool( sat::Pool::instance() );
512
513   if ( 1 )
514   {
515     Measure x( "INIT TARGET" );
516     {
517       {
518         zypp::base::LogControl::TmpLineWriter shutUp;
519         getZYpp()->initializeTarget( sysRoot );
520       }
521       getZYpp()->target()->load();
522       USR << getZYpp()->target()->targetDistribution() << endl;
523       USR << getZYpp()->target()->targetDistributionRelease() << endl;
524     }
525   }
526
527   if ( 1 )
528   {
529     RepoManager repoManager( makeRepoManager( sysRoot ) );
530     ServiceInfoList services = repoManager.knownServices();
531
532     for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
533     {
534       ServiceInfo & nservice( *it );
535       SEC << nservice << endl;
536
537       if ( ! nservice.enabled() )
538         continue;
539
540       repoManager.refreshService( nservice );
541
542     }
543   }
544
545   if ( 1 )
546   {
547     RepoManager repoManager( makeRepoManager( sysRoot ) );
548     RepoInfoList repos = repoManager.knownRepositories();
549
550     // launch repos
551     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
552     {
553       RepoInfo & nrepo( *it );
554       SEC << nrepo << endl;
555
556       if ( ! nrepo.enabled() )
557         continue;
558
559       if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
560       {
561         if ( repoManager.isCached( nrepo ) )
562         {
563           SEC << "cleanCache" << endl;
564           repoManager.cleanCache( nrepo );
565         }
566         SEC << "refreshMetadata" << endl;
567         //repoManager.refreshMetadata( nrepo );
568         SEC << "buildCache" << endl;
569         repoManager.buildCache( nrepo );
570       }
571     }
572
573     // create from cache:
574     {
575       Measure x( "CREATE FROM CACHE" );
576       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
577       {
578         RepoInfo & nrepo( *it );
579         if ( ! nrepo.enabled() )
580           continue;
581
582         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
583         try
584         {
585           repoManager.loadFromCache( nrepo );
586         }
587         catch ( const Exception & exp )
588         {
589           MIL << "Try to rebuild cache..." << endl;
590           SEC << "cleanCache" << endl;
591           repoManager.cleanCache( nrepo );
592           SEC << "buildCache" << endl;
593           repoManager.buildCache( nrepo );
594           SEC << "Create from cache" << endl;
595           repoManager.loadFromCache( nrepo );
596         }
597
598         USR << "pool: " << pool << endl;
599       }
600     }
601   }
602
603   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
604   USR << "pool: " << pool << endl;
605
606   ///////////////////////////////////////////////////////////////////
607
608   if ( 0 )
609   {
610     Measure x( "Upgrade" );
611     UpgradeStatistics u;
612     getZYpp()->resolver()->doUpgrade( u );
613   }
614
615   ///////////////////////////////////////////////////////////////////
616   ///////////////////////////////////////////////////////////////////
617
618   if ( 0 )
619   {
620     PoolItem pi ( getPi<Package>("amarok") );
621     MIL << pi << endl;
622     if ( pi )
623     {
624       pi.status().setTransact( true, ResStatus::USER );
625       solve();
626       vdumpPoolStats( USR << "Transacting:"<< endl,
627                       make_filter_begin<resfilter::ByTransact>(pool),
628                       make_filter_end<resfilter::ByTransact>(pool) ) << endl;
629     }
630   }
631
632
633   PoolItem pi ( getPi<Package>("amarok") );
634   MIL << pi << endl;
635   MIL << pi->asKind<Package>() << endl;
636   MIL << pi->asKind<Product>() << endl;
637   if ( pi->isKind<Package>() )
638     SEC << pi->asKind<Package>() << endl;
639
640  //////////////////////////////////////////////////////////////////
641   INT << "===[END]============================================" << endl << endl;
642   zypp::base::LogControl::instance().logNothing();
643   return 0;
644   for_( it, pool.byKindBegin<Product>(), pool.byKindEnd<Product>() )
645   {
646     MIL << *it << endl;
647     DBG << (*it)->provides() << endl;
648   }
649
650   sat::WhatProvides prv( Capability("product()") );
651   SEC << prv << endl;
652   for_( it, prv.begin(), prv.end() )
653   {
654
655     MIL << *it << endl;
656     MIL << it->provides() << endl;
657   }
658
659   Capability cap;
660
661   cap = Capability( "sles-release" );
662   prv = sat::WhatProvides( cap );
663   MIL << cap << ": " << prv << endl;
664
665   cap = Capability( "sles-release == 11" );
666   prv = sat::WhatProvides( cap );
667   MIL << cap << ": " << prv << endl;
668
669
670 //   PoolItem pi ( getPi<Package>("sles-release", Edition("11.0") ) );
671
672   //////////////////////////////////////////////////////////////////
673   INT << "===[END]============================================" << endl << endl;
674   zypp::base::LogControl::instance().logNothing();
675   return 0;
676
677
678   //SEC << zypp::getZYpp()->diskUsage() << endl;
679
680   //vdumpPoolStats( USR << "Pool:"<< endl, pool.begin(), pool.end() ) << endl;
681
682   sat::WhatProvides prodcap( Capability("product()") );
683   dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
684   dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
685
686   prodcap.poolItemBegin()->status().setTransact( true, ResStatus::APPL_LOW );
687   dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
688   dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
689
690   pool.byKindBegin<Product>()->status().setTransact( true, ResStatus::USER );
691   dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
692   dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
693
694   prodcap.poolItemBegin()->status().setLock( true, ResStatus::USER );
695   dumpRange( WAR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
696   dumpRange( WAR << "ProdPac " , prodcap.poolItemBegin(), prodcap.poolItemEnd() ) << endl;
697
698
699
700   //std::for_each( pool.begin(), pool.end(), Xprint() );
701
702   ///////////////////////////////////////////////////////////////////
703   INT << "===[END]============================================" << endl << endl;
704   zypp::base::LogControl::instance().logNothing();
705   return 0;
706 }
707 catch ( const Exception & exp )
708 {
709   INT << exp << endl << exp.historyAsString();
710 }
711 catch (...)
712 {}
713
714