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