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