- Disable fast creation of @System.solv. It may produce wrong results
[platform/upstream/libzypp.git] / devel / devel.ma / NewPool.cc
1 #include "Tools.h"
2
3 #include <zypp/base/PtrTypes.h>
4 #include <zypp/base/Exception.h>
5 #include <zypp/base/Gettext.h>
6 #include <zypp/base/LogTools.h>
7 #include <zypp/base/Debug.h>
8 #include <zypp/base/Functional.h>
9 #include <zypp/base/IOStream.h>
10 #include <zypp/base/ProvideNumericId.h>
11 #include <zypp/AutoDispose.h>
12
13 #include "zypp/ResPoolProxy.h"
14
15 #include "zypp/ZYppCallbacks.h"
16 #include "zypp/NVRAD.h"
17 #include "zypp/ResPool.h"
18 #include "zypp/ResFilters.h"
19 #include "zypp/ResObjects.h"
20 #include "zypp/Digest.h"
21 #include "zypp/PackageKeyword.h"
22 #include "zypp/TmpPath.h"
23 #include "zypp/ManagedFile.h"
24 #include "zypp/NameKindProxy.h"
25 #include "zypp/pool/GetResolvablesToInsDel.h"
26
27 #include "zypp/RepoManager.h"
28 #include "zypp/Repository.h"
29 #include "zypp/RepoInfo.h"
30
31 #include "zypp/repo/PackageProvider.h"
32
33 #include "zypp/ui/PatchContents.h"
34 #include "zypp/ResPoolProxy.h"
35
36 #include "zypp/sat/Pool.h"
37 #include "zypp/sat/LocaleSupport.h"
38 #include "zypp/sat/LookupAttr.h"
39 #include "zypp/sat/SolvableSet.h"
40 #include "zypp/sat/SolvIterMixin.h"
41 #include "zypp/sat/detail/PoolImpl.h"
42 #include "zypp/PoolQuery.h"
43
44 #include <zypp/base/GzStream.h>
45
46 #include <boost/mpl/int.hpp>
47
48 using namespace std;
49 using namespace zypp;
50 using namespace zypp::functor;
51 using namespace zypp::ui;
52
53 ///////////////////////////////////////////////////////////////////
54
55 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
56
57 ///////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////
59 namespace zypp
60 { /////////////////////////////////////////////////////////////////
61
62 bool queryInstalledEditionHelper( const std::string & name_r,
63                                   const Edition &     ed_r,
64                                   const Arch &        arch_r )
65 {
66   if ( ed_r == Edition::noedition )
67     return true;
68   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
69     return true;
70   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
71     return true;
72
73   return false;
74 }
75
76
77 ManagedFile repoProvidePackage( const PoolItem & pi )
78 {
79   ResPool _pool( getZYpp()->pool() );
80   repo::RepoMediaAccess _access;
81
82   // Redirect PackageProvider queries for installed editions
83   // (in case of patch/delta rpm processing) to rpmDb.
84   repo::PackageProviderPolicy packageProviderPolicy;
85   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
86
87   Package::constPtr p = asKind<Package>(pi.resolvable());
88
89   // Build a repository list for repos
90   // contributing to the pool
91   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
92                                                            _pool.knownRepositoriesEnd() ) );
93   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
94   return pkgProvider.providePackage();
95 }
96
97   /////////////////////////////////////////////////////////////////
98 } // namespace zypp
99 ///////////////////////////////////////////////////////////////////
100 ///////////////////////////////////////////////////////////////////
101
102 namespace zypp
103 {
104   template <class _LIterator, class _RIterator, class _Function>
105       inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
106                                _RIterator rbegin_r, _RIterator rend_r,
107                                _Function fnc_r )
108       {
109         int cnt = 0;
110         for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
111         {
112           for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
113           {
114             ++cnt;
115             if ( ! fnc_r( *lit, *rit ) )
116               return -cnt;
117           }
118         }
119         return cnt;
120       }
121 }
122
123
124 void dbgDu( Selectable::Ptr sel )
125 {
126   if ( sel->installedObj() )
127   {
128     DBG << "i: " << sel->installedObj() << endl
129         << sel->installedObj()->diskusage() << endl;
130   }
131   if ( sel->candidateObj() )
132   {
133     DBG << "c: " << sel->candidateObj() << endl
134         << sel->candidateObj()->diskusage() << endl;
135   }
136   INT << sel << endl
137       << getZYpp()->diskUsage() << endl;
138 }
139
140 ///////////////////////////////////////////////////////////////////
141
142 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
143 {
144   str << pi << endl;
145   Package::constPtr p( asKind<Package>(pi) );
146   if ( p )
147   {
148 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
149     Locale l( "de" );
150     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
151     l = Locale( "fr" );
152     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
153     l = Locale( "dsdf" );
154     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
155     OUTS( summary );
156     OUTS( installsize );
157     OUTS( downloadSize );
158     OUTS( sourcePkgName );
159     OUTS( sourcePkgEdition );
160     OUTS( checksum );
161     OUTS( location );
162 #undef OUTS
163
164
165   }
166   return str;
167 }
168
169 struct Xprint
170 {
171   bool operator()( const PoolItem & obj_r )
172   {
173     //MIL << obj_r << endl;
174     //DBG << " -> " << obj_r->satSolvable() << endl;
175
176     return true;
177   }
178
179   bool operator()( const sat::Solvable & obj_r )
180   {
181     //dumpOn( MIL, obj_r ) << endl;
182     return true;
183   }
184 };
185
186 ///////////////////////////////////////////////////////////////////
187 struct SetTransactValue
188 {
189   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
190   : _newVal( newVal_r )
191   , _causer( causer_r )
192   {}
193
194   ResStatus::TransactValue   _newVal;
195   ResStatus::TransactByValue _causer;
196
197   bool operator()( const PoolItem & pi ) const
198   {
199     bool ret = pi.status().setTransactValue( _newVal, _causer );
200     if ( ! ret )
201       ERR << _newVal <<  _causer << " " << pi << endl;
202     return ret;
203   }
204 };
205
206 struct StatusReset : public SetTransactValue
207 {
208   StatusReset()
209   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
210   {}
211 };
212
213 struct StatusInstall : public SetTransactValue
214 {
215   StatusInstall()
216   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
217   {}
218 };
219
220 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
221 {
222   if ( nkp.availableEmpty() )
223   {
224     ERR << "No Item to select: " << nkp << endl;
225     return false;
226     ZYPP_THROW( Exception("No Item to select") );
227   }
228
229   if ( arch != Arch() )
230   {
231     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
232     for ( ; it != nkp.availableEnd(); ++it )
233     {
234       if ( (*it)->arch() == arch )
235         return (*it).status().setTransact( true, ResStatus::USER );
236     }
237   }
238
239   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
240 }
241
242 ///////////////////////////////////////////////////////////////////
243
244 bool solve()
245 {
246   bool rres = false;
247   {
248     //zypp::base::LogControl::TmpLineWriter shutUp;
249     rres = getZYpp()->resolver()->resolvePool();
250   }
251   if ( ! rres )
252   {
253     ERR << "resolve " << rres << endl;
254     return false;
255   }
256   MIL << "resolve " << rres << endl;
257   return true;
258 }
259
260 bool install()
261 {
262   SEC << getZYpp()->commit( ZYppCommitPolicy() ) << endl;
263   return true;
264 }
265
266 ///////////////////////////////////////////////////////////////////
267
268 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
269 {
270   virtual void start( const Resolvable::constPtr & script_r,
271                       const Pathname & path_r,
272                       Task task_r )
273   {
274     SEC << __FUNCTION__ << endl
275     << "  " << script_r << endl
276     << "  " << path_r   << endl
277     << "  " << task_r   << endl;
278   }
279
280   virtual bool progress( Notify notify_r, const std::string & text_r )
281   {
282     SEC << __FUNCTION__ << endl
283     << "  " << notify_r << endl
284     << "  " << text_r   << endl;
285     return true;
286   }
287
288   virtual void problem( const std::string & description_r )
289   {
290     SEC << __FUNCTION__ << endl
291     << "  " << description_r << endl;
292   }
293
294   virtual void finish()
295   {
296     SEC << __FUNCTION__ << endl;
297   }
298
299 };
300 ///////////////////////////////////////////////////////////////////
301
302 struct DigestReceive : public callback::ReceiveReport<DigestReport>
303 {
304   DigestReceive()
305   {
306     connect();
307   }
308
309   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
310   {
311     USR << endl;
312     return false;
313   }
314   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
315   {
316     USR << endl;
317     return false;
318   }
319   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
320   {
321     USR << "fle " << PathInfo(file) << endl;
322     USR << "req " << requested << endl;
323     USR << "fnd " << found << endl;
324     return false;
325   }
326 };
327
328 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
329 {
330   KeyRingSignalsReceive()
331   {
332     connect();
333   }
334   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
335   {
336     USR << endl;
337   }
338   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
339   {
340     USR << endl;
341   }
342 };
343
344 ///////////////////////////////////////////////////////////////////
345
346 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
347 {
348   virtual Action requestMedia( Url & source
349                                , unsigned mediumNr
350                                , const std::string & label
351                                , Error error
352                                , const std::string & description
353                                , const std::vector<std::string> & devices
354                                , unsigned int & dev_current )
355   {
356     SEC << __FUNCTION__ << endl
357     << "  " << source << endl
358     << "  " << mediumNr << endl
359     << "  " << label << endl
360     << "  " << error << endl
361     << "  " << description << endl
362     << "  " << devices << endl
363     << "  " << dev_current << endl;
364     return IGNORE;
365   }
366 };
367
368 ///////////////////////////////////////////////////////////////////
369
370 namespace container
371 {
372   template<class _Tp>
373     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
374     { return cont.find( val ) != cont.end(); }
375 }
376 ///////////////////////////////////////////////////////////////////
377
378 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
379 {
380   SEC << *l << " - " << *r << endl;
381   INT << "== " << (l==r) << endl;
382   INT << "!= " << (l!=r) << endl;
383 }
384
385 bool isTrue()  { return true; }
386 bool isFalse() { return false; }
387
388 void dumpIdStr()
389 {
390   for ( int i = -3; i < 30; ++i )
391   {
392     DBG << i << '\t' << IdString( i ) << endl;
393   }
394 }
395
396 void ttt( const char * lhs, const char * rhs )
397 {
398   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
399 }
400
401 namespace zypp
402 {
403 namespace filter
404 {
405   template <class _MemFun, class _Value>
406   class HasValue
407   {
408     public:
409       HasValue( _MemFun fun_r, _Value val_r )
410       : _fun( fun_r ), _val( val_r )
411       {}
412       template <class _Tp>
413       bool operator()( const _Tp & obj_r ) const
414       { return( _fun && (obj_r.*_fun)() == _val ); }
415     private:
416       _MemFun _fun;
417       _Value  _val;
418   };
419
420   template <class _MemFun, class _Value>
421   HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
422   { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
423 }
424
425 }
426
427 template <class L>
428 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
429
430 template <class L>
431 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
432 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
433
434 template <class L>
435 _TestO<L> testO( const L & lhs )
436 { return _TestO<L>( lhs ); }
437
438 template <class L, class R>
439 void testCMP( const L & lhs, const R & rhs )
440 {
441   MIL << "LHS " << testO(lhs) << endl;
442   MIL << "RHS " << rhs << endl;
443
444 #define OUTS(S) DBG << #S << ": " << (S) << endl
445   OUTS( lhs.compare(rhs) );
446   OUTS( lhs != rhs );
447   OUTS( lhs <  rhs );
448   OUTS( lhs <= rhs );
449   OUTS( lhs == rhs );
450   OUTS( lhs >= rhs );
451   OUTS( lhs >  rhs );
452 #undef OUTS
453 }
454
455 namespace zypp
456 {
457 //   poolItemIterator
458   template <class _Filter>
459   class PoolFilter
460   {
461     typedef filter_iterator<_Filter,ResPool::const_iterator> iterator;
462
463     public:
464       PoolFilter()
465       {}
466
467       PoolFilter( const _Filter & filter_r )
468       : _filter( filter_r )
469       {}
470
471       iterator begin() const
472       { return ResPool::instance().filterBegin(_filter); }
473
474       iterator end() const
475       { return ResPool::instance().filterEnd(_filter); }
476
477     private:
478       _Filter _filter;
479   };
480
481   template <class _Filter>
482   PoolFilter<_Filter> makePoolFilter( const _Filter & filter_r )
483   { return PoolFilter<_Filter>( filter_r ); }
484 }
485
486 void tt( const std::string & name_r, ResKind kind_r = ResKind::package )
487 {
488   Capability cap( name_r, kind_r );
489   sat::WhatProvides possibleProviders(cap);
490   (possibleProviders.empty()?WAR:MIL) << name_r << endl;
491   for_(iter, possibleProviders.begin(), possibleProviders.end())
492   {
493     MIL << name_r << endl;
494     PoolItem provider = ResPool::instance().find(*iter);
495   }
496 }
497
498 bool myfilter( const PoolItem & pi )
499 {
500   if ( pi->name() == "ruby" )
501     return true;
502   return false;
503 }
504
505 struct Foo : private debug::TraceCAD<Foo>
506 {};
507
508 namespace zypp
509 {
510   namespace sat
511   {
512   }
513 }
514
515 void dit( const Pattern::Contents & c_r )
516 {
517   {
518   sat::WhatProvides c( Capability("amarok") );
519   dumpRange( MIL, c.solvableBegin(), c.solvableEnd() ) << endl;
520   dumpRange( MIL, c.poolItemBegin(), c.poolItemEnd() ) << endl;
521   dumpRange( MIL, c.selectableBegin(), c.selectableEnd() ) << endl;
522   }
523   if(0){
524   const Pattern::Contents & c( c_r );
525   dumpRange( MIL, c.solvableBegin(), c.solvableEnd() ) << endl;
526   dumpRange( WAR, c.poolItemBegin(), c.poolItemEnd() ) << endl;
527   dumpRange( ERR, c.selectableBegin(), c.selectableEnd() ) << endl;
528   }
529 }
530
531 void ditest( sat::Solvable slv_r )
532 {
533   MIL << slv_r << endl;
534   Package::Ptr   pkg( make<Package>( slv_r ) );
535   dumpRange( DBG, pkg->keywords().begin(), pkg->keywords().end() ) << endl;
536   return;
537 }
538
539 void ditest( const PoolItem & pi_r )
540 {
541   ditest( pi_r.satSolvable() );
542 }
543
544
545 /******************************************************************
546 **
547 **      FUNCTION NAME : main
548 **      FUNCTION TYPE : int
549 */
550 int main( int argc, char * argv[] )
551 try {
552   --argc;
553   ++argv;
554   zypp::base::LogControl::instance().logToStdErr();
555   INT << "===[START]==========================================" << endl;
556   ZConfig::instance().setTextLocale(Locale("de"));
557
558   sat::Pool satpool( sat::Pool::instance() );
559   ResPool   pool( ResPool::instance() );
560   USR << "pool: " << pool << endl;
561
562   if ( 0 )
563   {
564     RepoManager repoManager( makeRepoManager( sysRoot ) );
565     RepoInfoList repos = repoManager.knownRepositories();
566
567     // launch repos
568     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
569     {
570       RepoInfo & nrepo( *it );
571       SEC << nrepo << endl;
572
573       if ( ! nrepo.enabled() )
574         continue;
575
576       if ( ! repoManager.isCached( nrepo ) || /*force*/false )
577       {
578         if ( repoManager.isCached( nrepo ) )
579         {
580           SEC << "cleanCache" << endl;
581           repoManager.cleanCache( nrepo );
582         }
583         //SEC << "refreshMetadata" << endl;
584         //repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
585         SEC << "buildCache" << endl;
586         repoManager.buildCache( nrepo );
587       }
588     }
589
590     // create from cache:
591     {
592       Measure x( "CREATE FROM CACHE" );
593       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
594       {
595         RepoInfo & nrepo( *it );
596         if ( ! nrepo.enabled() )
597           continue;
598
599         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
600         try
601         {
602           repoManager.loadFromCache( nrepo );
603         }
604         catch ( const Exception & exp )
605         {
606           MIL << "Try to rebuild cache..." << endl;
607           SEC << "cleanCache" << endl;
608           repoManager.cleanCache( nrepo );
609           SEC << "buildCache" << endl;
610           repoManager.buildCache( nrepo );
611           SEC << "Create from cache" << endl;
612           repoManager.loadFromCache( nrepo );
613         }
614
615         USR << "pool: " << pool << endl;
616       }
617     }
618   }
619
620   if ( 1 )
621   {
622     Measure x( "INIT TARGET" );
623     {
624       getZYpp()->initializeTarget( sysRoot );
625       getZYpp()->target()->load();
626     }
627   }
628
629   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
630   USR << "pool: " << pool << endl;
631
632   ///////////////////////////////////////////////////////////////////
633   if ( 0 )
634   {
635     Measure x( "Upgrade" );
636     UpgradeStatistics u;
637     getZYpp()->resolver()->doUpgrade( u );
638   }
639   ///////////////////////////////////////////////////////////////////
640   ///////////////////////////////////////////////////////////////////
641
642   //vdumpPoolStats( USR << "Pool:"<< endl, pool.begin(), pool.end() ) << endl;
643
644   if ( !pool.empty() )
645   {
646     std::string name( pool.begin()->resolvable()->name() );
647     PoolItem pi ( getPi<Package>( name ) );
648     MIL << pi << endl;
649     if ( pi )
650     {
651       pi.status().setTransact( true, ResStatus::USER );
652       //solve();
653       vdumpPoolStats( USR << "Transacting:"<< endl,
654                       make_filter_begin<resfilter::ByTransact>(pool),
655                       make_filter_end<resfilter::ByTransact>(pool) ) << endl;
656       install();
657
658       pi = getPi<Package>( name );
659       if ( pi )
660       {
661         INT << "Still here? " << getPi<Package>("ant") << endl;
662         return 1;
663       }
664     }
665   }
666   ///////////////////////////////////////////////////////////////////
667   INT << "===[END]============================================" << endl << endl;
668   zypp::base::LogControl::instance().logNothing();
669   return 0;
670
671   if ( 0 ) {
672     Measure x( "PROCXY" );
673     pool.proxy();
674   }
675
676   if ( 0 ) {
677     Measure x( "ALL PATTERNS" );
678
679     for_( it, pool.byKindBegin<Pattern>(), pool.byKindEnd<Pattern>() )
680     {
681       Measure x( string("  ") + (*it)->name() );
682       Pattern::Contents c( asKind<Pattern>(*it)->contents() );
683       {
684         Measure x( "    poolitem" );
685         dumpRange( WAR, c.poolItemBegin(), c.poolItemEnd() ) << endl;
686       }
687       {
688         Measure x( "    selectable" );
689         dumpRange( ERR, c.selectableBegin(), c.selectableEnd() ) << endl;
690       }
691       break;
692     }
693   }
694
695   SEC << zypp::getZYpp()->diskUsage() << endl;
696
697   if ( 1 )
698   {
699     PoolItem pi ( getPi<Package>("amarok") );
700     MIL << pi << endl;
701     if ( pi )
702     {
703       pi.status().setTransact( true, ResStatus::USER );
704       solve();
705       vdumpPoolStats( USR << "Transacting:"<< endl,
706                       make_filter_begin<resfilter::ByTransact>(pool),
707                       make_filter_end<resfilter::ByTransact>(pool) ) << endl;
708
709     }
710   }
711
712   SEC << zypp::getZYpp()->diskUsage() << endl;
713
714   //vdumpPoolStats( USR << "Pool:"<< endl, pool.begin(), pool.end() ) << endl;
715   //waitForInput();
716
717   //std::for_each( pool.begin(), pool.end(), Xprint() );
718
719   ///////////////////////////////////////////////////////////////////
720   INT << "===[END]============================================" << endl << endl;
721   zypp::base::LogControl::instance().logNothing();
722   return 0;
723 }
724 catch ( const Exception & exp )
725 {
726   INT << exp << endl << exp.historyAsString();
727 }
728 catch (...)
729 {}
730
731