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