backup
[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/ProvideNumericId.h>
8 #include <zypp/AutoDispose.h>
9
10 #include "zypp/ZYppFactory.h"
11 #include "zypp/ResPoolProxy.h"
12 #include <zypp/CapMatchHelper.h>
13
14 #include "zypp/ZYppCallbacks.h"
15 #include "zypp/NVRAD.h"
16 #include "zypp/ResPool.h"
17 #include "zypp/ResFilters.h"
18 #include "zypp/CapFilters.h"
19 #include "zypp/Package.h"
20 #include "zypp/Pattern.h"
21 #include "zypp/Language.h"
22 #include "zypp/Digest.h"
23 #include "zypp/PackageKeyword.h"
24 #include "zypp/ManagedFile.h"
25 #include "zypp/NameKindProxy.h"
26 #include "zypp/pool/GetResolvablesToInsDel.h"
27
28 #include "zypp/parser/TagParser.h"
29 #include "zypp/parser/susetags/PackagesFileReader.h"
30 #include "zypp/parser/susetags/PackagesLangFileReader.h"
31 #include "zypp/parser/susetags/PatternFileReader.h"
32 #include "zypp/parser/susetags/ContentFileReader.h"
33 #include "zypp/parser/susetags/RepoIndex.h"
34 #include "zypp/parser/susetags/RepoParser.h"
35 #include "zypp/cache/CacheStore.h"
36 #include "zypp/RepoManager.h"
37 #include "zypp/RepoInfo.h"
38
39 #include "zypp/repo/PackageProvider.h"
40
41 #include "zypp/ui/PatchContents.h"
42 #include "zypp/ResPoolProxy.h"
43
44 #include "zypp/sat/Pool.h"
45 #include "zypp/sat/Repo.h"
46 #include "zypp/sat/Solvable.h"
47 #include "zypp/sat/detail/PoolImpl.h"
48 #include "zypp/sat/IdStrType.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 using zypp::parser::TagParser;
57
58 ///////////////////////////////////////////////////////////////////
59
60 static const Pathname sysRoot( "/Local/ROOT" );
61
62 ///////////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////
64 namespace zypp
65 { /////////////////////////////////////////////////////////////////
66
67 bool queryInstalledEditionHelper( const std::string & name_r,
68                                   const Edition &     ed_r,
69                                   const Arch &        arch_r )
70 {
71   if ( ed_r == Edition::noedition )
72     return true;
73   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
74     return true;
75   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
76     return true;
77
78   return false;
79 }
80
81
82 ManagedFile repoProvidePackage( const PoolItem & pi )
83 {
84   ResPool _pool( getZYpp()->pool() );
85   repo::RepoMediaAccess _access;
86
87   // Redirect PackageProvider queries for installed editions
88   // (in case of patch/delta rpm processing) to rpmDb.
89   repo::PackageProviderPolicy packageProviderPolicy;
90   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
91
92   Package::constPtr p = asKind<Package>(pi.resolvable());
93
94   // Build a repository list for repos
95   // contributing to the pool
96   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
97                                                            _pool.knownRepositoriesEnd() ) );
98   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
99   return pkgProvider.providePackage();
100 }
101
102   /////////////////////////////////////////////////////////////////
103 } // namespace zypp
104 ///////////////////////////////////////////////////////////////////
105 ///////////////////////////////////////////////////////////////////
106
107 namespace zypp
108 {
109   template <class _LIterator, class _RIterator, class _Function>
110       inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
111                                _RIterator rbegin_r, _RIterator rend_r,
112                                _Function fnc_r )
113       {
114         int cnt = 0;
115         for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
116         {
117           for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
118           {
119             ++cnt;
120             if ( ! fnc_r( *lit, *rit ) )
121               return -cnt;
122           }
123         }
124         return cnt;
125       }
126 }
127
128
129 void dbgDu( Selectable::Ptr sel )
130 {
131   if ( sel->installedPoolItem() )
132   {
133     DBG << "i: " << sel->installedPoolItem() << endl
134         << sel->installedPoolItem()->diskusage() << endl;
135   }
136   if ( sel->candidatePoolItem() )
137   {
138     DBG << "c: " << sel->candidatePoolItem() << endl
139         << sel->candidatePoolItem()->diskusage() << endl;
140   }
141   INT << sel << endl
142       << getZYpp()->diskUsage() << endl;
143 }
144
145 ///////////////////////////////////////////////////////////////////
146
147 struct Xprint
148 {
149   bool operator()( const PoolItem & obj_r )
150   {
151     MIL << obj_r << endl;
152     DBG << " -> " << obj_r .satSolvable() << endl;
153
154     return true;
155   }
156
157   bool operator()( const sat::Solvable & obj_r )
158   {
159     dumpOn( MIL, obj_r ) << endl;
160     return true;
161   }
162 };
163
164 ///////////////////////////////////////////////////////////////////
165 struct SetTransactValue
166 {
167   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
168   : _newVal( newVal_r )
169   , _causer( causer_r )
170   {}
171
172   ResStatus::TransactValue   _newVal;
173   ResStatus::TransactByValue _causer;
174
175   bool operator()( const PoolItem & pi ) const
176   {
177     bool ret = pi.status().setTransactValue( _newVal, _causer );
178     if ( ! ret )
179       ERR << _newVal <<  _causer << " " << pi << endl;
180     return ret;
181   }
182 };
183
184 struct StatusReset : public SetTransactValue
185 {
186   StatusReset()
187   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
188   {}
189 };
190
191 struct StatusInstall : public SetTransactValue
192 {
193   StatusInstall()
194   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
195   {}
196 };
197
198 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
199 {
200   if ( nkp.availableEmpty() )
201   {
202     ERR << "No Item to select: " << nkp << endl;
203     return false;
204     ZYPP_THROW( Exception("No Item to select") );
205   }
206
207   if ( arch != Arch() )
208   {
209     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
210     for ( ; it != nkp.availableEnd(); ++it )
211     {
212       if ( (*it)->arch() == arch )
213         return (*it).status().setTransact( true, ResStatus::USER );
214     }
215   }
216
217   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
218 }
219
220 ///////////////////////////////////////////////////////////////////
221
222 bool solve( bool establish = false )
223 {
224   if ( establish )
225   {
226     bool eres = false;
227     {
228       zypp::base::LogControl::TmpLineWriter shutUp;
229       eres = getZYpp()->resolver()->establishPool();
230     }
231     if ( ! eres )
232     {
233       ERR << "establish " << eres << endl;
234       return false;
235     }
236     MIL << "establish " << eres << endl;
237   }
238
239   bool rres = false;
240   {
241     zypp::base::LogControl::TmpLineWriter shutUp;
242     rres = getZYpp()->resolver()->resolvePool();
243   }
244   if ( ! rres )
245   {
246     ERR << "resolve " << rres << endl;
247     return false;
248   }
249   MIL << "resolve " << rres << endl;
250   return true;
251 }
252
253 bool install()
254 {
255   SEC << getZYpp()->commit( ZYppCommitPolicy() ) << endl;
256   return true;
257 }
258
259 ///////////////////////////////////////////////////////////////////
260
261 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
262 {
263   virtual void start( const Resolvable::constPtr & script_r,
264                       const Pathname & path_r,
265                       Task task_r )
266   {
267     SEC << __FUNCTION__ << endl
268     << "  " << script_r << endl
269     << "  " << path_r   << endl
270     << "  " << task_r   << endl;
271   }
272
273   virtual bool progress( Notify notify_r, const std::string & text_r )
274   {
275     SEC << __FUNCTION__ << endl
276     << "  " << notify_r << endl
277     << "  " << text_r   << endl;
278     return true;
279   }
280
281   virtual void problem( const std::string & description_r )
282   {
283     SEC << __FUNCTION__ << endl
284     << "  " << description_r << endl;
285   }
286
287   virtual void finish()
288   {
289     SEC << __FUNCTION__ << endl;
290   }
291
292 };
293 ///////////////////////////////////////////////////////////////////
294
295 struct DigestReceive : public callback::ReceiveReport<DigestReport>
296 {
297   DigestReceive()
298   {
299     connect();
300   }
301
302   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
303   {
304     USR << endl;
305     return false;
306   }
307   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
308   {
309     USR << endl;
310     return false;
311   }
312   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
313   {
314     USR << "fle " << PathInfo(file) << endl;
315     USR << "req " << requested << endl;
316     USR << "fnd " << found << endl;
317     return false;
318   }
319 };
320
321 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
322 {
323   KeyRingSignalsReceive()
324   {
325     connect();
326   }
327   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
328   {
329     USR << endl;
330   }
331   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
332   {
333     USR << endl;
334   }
335 };
336
337 ///////////////////////////////////////////////////////////////////
338
339 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
340 {
341   virtual Action requestMedia( Url & source
342                                , unsigned mediumNr
343                                , Error error
344                                , const std::string & description )
345   {
346     SEC << __FUNCTION__ << endl
347     << "  " << source << endl
348     << "  " << mediumNr << endl
349     << "  " << error << endl
350     << "  " << description << endl;
351     return IGNORE;
352   }
353 };
354
355 ///////////////////////////////////////////////////////////////////
356
357 namespace container
358 {
359   template<class _Tp>
360     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
361     { return cont.find( val ) != cont.end(); }
362 }
363 ///////////////////////////////////////////////////////////////////
364
365 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
366 {
367   SEC << *l << " - " << *r << endl;
368   INT << "== " << (l==r) << endl;
369   INT << "!= " << (l!=r) << endl;
370 }
371
372 bool isTrue()  { return true; }
373 bool isFalse() { return false; }
374
375 void dumpIdStr()
376 {
377   for ( int i = -3; i < 30; ++i )
378   {
379     DBG << i << '\t' << sat::IdStr( i ) << endl;
380   }
381 }
382
383 void ttt( const char * lhs, const char * rhs )
384 {
385   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
386 }
387
388 namespace filter
389 {
390   template <class _MemFun, class _Value>
391   class HasValue
392   {
393     public:
394       HasValue( _MemFun fun_r, _Value val_r )
395       : _fun( fun_r ), _val( val_r )
396       {}
397       template <class _Tp>
398       bool operator()( const _Tp & obj_r ) const
399       { return( _fun && (obj_r.*_fun)() == _val ); }
400     private:
401       _MemFun _fun;
402       _Value  _val;
403   };
404
405   template <class _MemFun, class _Value>
406   HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
407   { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
408 }
409
410 namespace zypp
411 {
412   namespace sat
413   {
414
415
416   } // namespace sat
417
418   namespace par
419   {
420     class Foo : public sat::IdStrType<Foo>
421     {
422       public:
423         Foo() {}
424         explicit Foo( sat::detail::IdType id_r )   : _str( str::toLower(sat::IdStr(id_r).c_str()) ) {}
425         explicit Foo( const sat::IdStr & idstr_r ) : _str( str::toLower(idstr_r.c_str()) ) {}
426         explicit Foo( const char * cstr_r )        : _str( str::toLower(cstr_r) ) {}
427         explicit Foo( const std::string & str_r )  : _str( str::toLower(str_r) ) {}
428       private:
429         int _doDompareC( const char * rhs )  const
430         { return str::compareCI( _str.c_str(), rhs ); }
431       private:
432         friend class sat::IdStrType<Foo>;
433         sat::IdStr _str;
434     };
435
436   } // namespace par
437 }
438
439 template <class L>
440 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
441
442 template <class L>
443 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
444 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
445
446 template <class L>
447 _TestO<L> testO( const L & lhs )
448 { return _TestO<L>( lhs ); }
449
450
451 template <class L, class R>
452 void testCMP( const L & lhs, const R & rhs )
453 {
454   MIL << "LHS " << testO(lhs) << endl;
455   MIL << "RHS " << rhs << endl;
456
457 #define OUTS(S) DBG << #S << ": " << (S) << endl
458   OUTS( lhs.compare(rhs) );
459   OUTS( lhs != rhs );
460   OUTS( lhs <  rhs );
461   OUTS( lhs <= rhs );
462   OUTS( lhs == rhs );
463   OUTS( lhs >= rhs );
464   OUTS( lhs >  rhs );
465 #undef OUTS
466 }
467
468 /******************************************************************
469 **
470 **      FUNCTION NAME : main
471 **      FUNCTION TYPE : int
472 */
473 int main( int argc, char * argv[] )
474 {
475   //zypp::base::LogControl::instance().logfile( "log.restrict" );
476   INT << "===[START]==========================================" << endl;
477
478   sat::Pool satpool( sat::Pool::instance() );
479
480 #if 0
481   sat::Repo s( satpool.addRepoSolv( "10.3.solv" ) );
482   //sat::Repo s( satpool.addRepoSolv( "target.solv" ) );
483
484   sat::Capabilities r( (*satpool.solvablesBegin())[Dep::PROVIDES] );
485   MIL << r << endl;
486   sat::Capabilities::const_iterator it = r.begin();
487   DBG << *it << endl;
488   it = ++r.begin();
489   DBG << *it << endl;
490
491   if ( 1 )
492   {
493     std::for_each( make_filter_iterator( filter::byValue( &sat::Solvable::name, "bash" ),
494                                          satpool.solvablesBegin(), satpool.solvablesEnd() ),
495                    make_filter_iterator( filter::byValue( &sat::Solvable::name, "bash" ),
496                                          satpool.solvablesEnd(), satpool.solvablesEnd() ),
497                    Xprint() );
498     std::for_each( make_filter_iterator( filter::byValue( &sat::Solvable::name, "pattern:yast2_install_wf" ),
499                                          satpool.solvablesBegin(), satpool.solvablesEnd() ),
500                    make_filter_iterator( filter::byValue( &sat::Solvable::name, "pattern:yast2_install_wf" ),
501                                          satpool.solvablesEnd(), satpool.solvablesEnd() ),
502                    Xprint() );
503   }
504
505
506
507   // make_filter_iterator(detail::ByRepo( *this ),
508   // Repo.cc-                                  detail::SolvableIterator(_repo->end),
509   // Repo.cc-                                  detail::SolvableIterator(_repo->end) );
510
511
512 //   DBG << satpool.solvablesBegin()->name() << endl;
513 //   DBG << (*satpool.solvablesBegin())[Dep::PROVIDES] << endl;
514
515   //std::for_each( satpool.solvablesBegin(), satpool.solvablesEnd(), Xprint() );
516
517   ///////////////////////////////////////////////////////////////////
518   INT << "===[END]============================================" << endl << endl;
519   zypp::base::LogControl::instance().logNothing();
520   return 0;
521 #endif
522
523   setenv( "ZYPP_CONF", (sysRoot/"zypp.conf").c_str(), 1 );
524
525   ResPool pool( getZYpp()->pool() );
526   USR << "pool: " << pool << endl;
527   pool.satSync();
528
529   RepoManager repoManager( makeRepoManager( sysRoot ) );
530   RepoInfoList repos = repoManager.knownRepositories();
531   // SEC << "/Local/ROOT " << repos << endl;
532
533   // launch repos
534   for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
535   {
536     RepoInfo & nrepo( *it );
537     SEC << nrepo << endl;
538
539     if ( ! nrepo.enabled() )
540       continue;
541
542     if ( ! repoManager.isCached( nrepo ) || 0 )
543     {
544       if ( repoManager.isCached( nrepo ) )
545       {
546         SEC << "cleanCache" << endl;
547         repoManager.cleanCache( nrepo );
548       }
549       SEC << "refreshMetadata" << endl;
550       repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
551       SEC << "buildCache" << endl;
552       repoManager.buildCache( nrepo );
553     }
554   }
555
556   // create from cache:
557   std::list<Repository> repositories;
558
559   {
560     Measure x( "CREATE FROM CACHE" );
561     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
562     {
563       RepoInfo & nrepo( *it );
564       if ( ! nrepo.enabled() )
565         continue;
566
567       Measure x( "CREATE FROM CACHE "+nrepo.alias() );
568       Repository nrep( repoManager.createFromCache( nrepo ) );
569       const zypp::ResStore & store( nrep.resolvables() );
570       repositories.push_back( nrep );
571     }
572   }
573
574   // load pool:
575   {
576     Measure x( "LOAD POOL" );
577     for_( it, repositories.begin(), repositories.end() )
578     {
579       Measure x( "LOAD POOL "+(*it).info().alias() );
580       const zypp::ResStore & store( (*it).resolvables() );
581       getZYpp()->addResolvables( store );
582     }
583   }
584
585
586   if ( 0 )
587   {
588     Measure x( "INIT TARGET" );
589     {
590       zypp::base::LogControl::TmpLineWriter shutUp;
591       getZYpp()->initTarget( sysRoot );
592       //getZYpp()->initTarget( "/" );
593     }
594     dumpPoolStats( SEC << "TargetStore: " << endl,
595                    getZYpp()->target()->resolvables().begin(),
596                    getZYpp()->target()->resolvables().end() ) << endl;
597   }
598
599   USR << "pool: " << pool << endl;
600   pool.satSync();
601
602   //waitForInput();
603   //std::for_each( pool.begin(), pool.end(), Xprint() );
604
605   MIL << satpool << endl;
606   for_( it, satpool.solvablesBegin(), satpool.solvablesEnd() )
607   {
608     dumpOn( MIL, *it );
609   }
610
611   ///////////////////////////////////////////////////////////////////
612   INT << "===[END]============================================" << endl << endl;
613   zypp::base::LogControl::instance().logNothing();
614   return 0;
615 }
616