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