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
49 #include <boost/mpl/int.hpp>
50
51 using namespace std;
52 using namespace zypp;
53 using namespace zypp::functor;
54 using namespace zypp::ui;
55 using zypp::parser::TagParser;
56
57 ///////////////////////////////////////////////////////////////////
58
59 static const Pathname 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->installedPoolItem() )
131   {
132     DBG << "i: " << sel->installedPoolItem() << endl
133         << sel->installedPoolItem()->diskusage() << endl;
134   }
135   if ( sel->candidatePoolItem() )
136   {
137     DBG << "c: " << sel->candidatePoolItem() << endl
138         << sel->candidatePoolItem()->diskusage() << endl;
139   }
140   INT << sel << endl
141       << getZYpp()->diskUsage() << endl;
142 }
143
144 ///////////////////////////////////////////////////////////////////
145
146 struct Xprint
147 {
148   bool operator()( const PoolItem & obj_r )
149   {
150     MIL << obj_r << endl;
151     DBG << " -> " << obj_r .satSolvable() << endl;
152
153     return true;
154   }
155
156   bool operator()( const sat::Solvable & obj_r )
157   {
158     dumpOn( MIL, obj_r ) << endl;
159     return true;
160   }
161 };
162
163 ///////////////////////////////////////////////////////////////////
164 struct SetTransactValue
165 {
166   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
167   : _newVal( newVal_r )
168   , _causer( causer_r )
169   {}
170
171   ResStatus::TransactValue   _newVal;
172   ResStatus::TransactByValue _causer;
173
174   bool operator()( const PoolItem & pi ) const
175   {
176     bool ret = pi.status().setTransactValue( _newVal, _causer );
177     if ( ! ret )
178       ERR << _newVal <<  _causer << " " << pi << endl;
179     return ret;
180   }
181 };
182
183 struct StatusReset : public SetTransactValue
184 {
185   StatusReset()
186   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
187   {}
188 };
189
190 struct StatusInstall : public SetTransactValue
191 {
192   StatusInstall()
193   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
194   {}
195 };
196
197 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
198 {
199   if ( nkp.availableEmpty() )
200   {
201     ERR << "No Item to select: " << nkp << endl;
202     return false;
203     ZYPP_THROW( Exception("No Item to select") );
204   }
205
206   if ( arch != Arch() )
207   {
208     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
209     for ( ; it != nkp.availableEnd(); ++it )
210     {
211       if ( (*it)->arch() == arch )
212         return (*it).status().setTransact( true, ResStatus::USER );
213     }
214   }
215
216   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
217 }
218
219 ///////////////////////////////////////////////////////////////////
220
221 bool solve( bool establish = false )
222 {
223   if ( establish )
224   {
225     bool eres = false;
226     {
227       zypp::base::LogControl::TmpLineWriter shutUp;
228       eres = getZYpp()->resolver()->establishPool();
229     }
230     if ( ! eres )
231     {
232       ERR << "establish " << eres << endl;
233       return false;
234     }
235     MIL << "establish " << eres << endl;
236   }
237
238   bool rres = false;
239   {
240     zypp::base::LogControl::TmpLineWriter shutUp;
241     rres = getZYpp()->resolver()->resolvePool();
242   }
243   if ( ! rres )
244   {
245     ERR << "resolve " << rres << endl;
246     return false;
247   }
248   MIL << "resolve " << rres << endl;
249   return true;
250 }
251
252 bool install()
253 {
254   SEC << getZYpp()->commit( ZYppCommitPolicy() ) << endl;
255   return true;
256 }
257
258 ///////////////////////////////////////////////////////////////////
259
260 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
261 {
262   virtual void start( const Resolvable::constPtr & script_r,
263                       const Pathname & path_r,
264                       Task task_r )
265   {
266     SEC << __FUNCTION__ << endl
267     << "  " << script_r << endl
268     << "  " << path_r   << endl
269     << "  " << task_r   << endl;
270   }
271
272   virtual bool progress( Notify notify_r, const std::string & text_r )
273   {
274     SEC << __FUNCTION__ << endl
275     << "  " << notify_r << endl
276     << "  " << text_r   << endl;
277     return true;
278   }
279
280   virtual void problem( const std::string & description_r )
281   {
282     SEC << __FUNCTION__ << endl
283     << "  " << description_r << endl;
284   }
285
286   virtual void finish()
287   {
288     SEC << __FUNCTION__ << endl;
289   }
290
291 };
292 ///////////////////////////////////////////////////////////////////
293
294 struct DigestReceive : public callback::ReceiveReport<DigestReport>
295 {
296   DigestReceive()
297   {
298     connect();
299   }
300
301   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
302   {
303     USR << endl;
304     return false;
305   }
306   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
307   {
308     USR << endl;
309     return false;
310   }
311   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
312   {
313     USR << "fle " << PathInfo(file) << endl;
314     USR << "req " << requested << endl;
315     USR << "fnd " << found << endl;
316     return false;
317   }
318 };
319
320 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
321 {
322   KeyRingSignalsReceive()
323   {
324     connect();
325   }
326   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
327   {
328     USR << endl;
329   }
330   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
331   {
332     USR << endl;
333   }
334 };
335
336 ///////////////////////////////////////////////////////////////////
337
338 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
339 {
340   virtual Action requestMedia( Url & source
341                                , unsigned mediumNr
342                                , Error error
343                                , const std::string & description )
344   {
345     SEC << __FUNCTION__ << endl
346     << "  " << source << endl
347     << "  " << mediumNr << endl
348     << "  " << error << endl
349     << "  " << description << endl;
350     return IGNORE;
351   }
352 };
353
354 ///////////////////////////////////////////////////////////////////
355
356 namespace container
357 {
358   template<class _Tp>
359     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
360     { return cont.find( val ) != cont.end(); }
361 }
362 ///////////////////////////////////////////////////////////////////
363
364 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
365 {
366   SEC << *l << " - " << *r << endl;
367   INT << "== " << (l==r) << endl;
368   INT << "!= " << (l!=r) << endl;
369 }
370
371 bool isTrue()  { return true; }
372 bool isFalse() { return false; }
373
374 void dumpIdStr()
375 {
376   for ( int i = -3; i < 30; ++i )
377   {
378     DBG << i << '\t' << sat::IdStr( i ) << endl;
379   }
380 }
381
382 void ttt( const char * lhs, const char * rhs )
383 {
384   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
385 }
386
387 namespace filter
388 {
389   template <class _MemFun, class _Value>
390   class HasValue
391   {
392     public:
393       HasValue( _MemFun fun_r, _Value val_r )
394       : _fun( fun_r ), _val( val_r )
395       {}
396       template <class _Tp>
397       bool operator()( const _Tp & obj_r ) const
398       { return( _fun && (obj_r.*_fun)() == _val ); }
399     private:
400       _MemFun _fun;
401       _Value  _val;
402   };
403
404   template <class _MemFun, class _Value>
405   HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
406   { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
407 }
408
409
410
411
412 /******************************************************************
413 **
414 **      FUNCTION NAME : main
415 **      FUNCTION TYPE : int
416 */
417 int main( int argc, char * argv[] )
418 {
419   //zypp::base::LogControl::instance().logfile( "log.restrict" );
420   INT << "===[START]==========================================" << endl;
421
422   sat::Pool satpool( sat::Pool::instance() );
423
424 #if 1
425   //sat::Repo r( satpool.addRepoSolv( "sl10.1-beta7-packages.solv" ) );
426   //sat::Repo s( satpool.addRepoSolv( "sl10.1-beta7-selections.solv" ) );
427   sat::Repo s( satpool.addRepoSolv( "target.solv" ) );
428
429   sat::Capabilities r( (*satpool.solvablesBegin())[Dep::PROVIDES] );
430   MIL << r << endl;
431   sat::Capabilities::const_iterator it = r.begin();
432   DBG << *it << endl;
433   it = ++r.begin();
434   DBG << *it << endl;
435
436   if ( 1 )
437   std::for_each( make_filter_iterator( filter::byValue( &sat::Solvable::name, "bash" ),
438                                        satpool.solvablesBegin(), satpool.solvablesEnd() ),
439                  make_filter_iterator( filter::byValue( &sat::Solvable::name, "bash" ),
440                                        satpool.solvablesEnd(), satpool.solvablesEnd() ),
441                  Xprint() );
442
443   // make_filter_iterator(detail::ByRepo( *this ),
444   // Repo.cc-                                  detail::SolvableIterator(_repo->end),
445   // Repo.cc-                                  detail::SolvableIterator(_repo->end) );
446
447
448 //   DBG << satpool.solvablesBegin()->name() << endl;
449 //   DBG << (*satpool.solvablesBegin())[Dep::PROVIDES] << endl;
450
451   //std::for_each( satpool.solvablesBegin(), satpool.solvablesEnd(), Xprint() );
452
453   ///////////////////////////////////////////////////////////////////
454   INT << "===[END]============================================" << endl << endl;
455   zypp::base::LogControl::instance().logNothing();
456   return 0;
457 #endif
458
459   setenv( "ZYPP_CONF", (sysRoot/"zypp.conf").c_str(), 1 );
460
461   ResPool pool( getZYpp()->pool() );
462   USR << "pool: " << pool << endl;
463   pool.satSync();
464
465   RepoManager repoManager( makeRepoManager( sysRoot ) );
466   RepoInfoList repos = repoManager.knownRepositories();
467   // SEC << "/Local/ROOT " << repos << endl;
468
469   // launch repos
470   for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
471   {
472     RepoInfo & nrepo( *it );
473     SEC << nrepo << endl;
474
475     if ( ! nrepo.enabled() )
476       continue;
477
478     if ( ! repoManager.isCached( nrepo ) || 0 )
479     {
480       if ( repoManager.isCached( nrepo ) )
481       {
482         SEC << "cleanCache" << endl;
483         repoManager.cleanCache( nrepo );
484       }
485       SEC << "refreshMetadata" << endl;
486       repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
487       SEC << "buildCache" << endl;
488       repoManager.buildCache( nrepo );
489     }
490   }
491
492   // create from cache:
493   std::list<Repository> repositories;
494
495   {
496     Measure x( "CREATE FROM CACHE" );
497     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
498     {
499       RepoInfo & nrepo( *it );
500       if ( ! nrepo.enabled() )
501         continue;
502
503       Measure x( "CREATE FROM CACHE "+nrepo.alias() );
504       Repository nrep( repoManager.createFromCache( nrepo ) );
505       const zypp::ResStore & store( nrep.resolvables() );
506       repositories.push_back( nrep );
507     }
508   }
509
510   // load pool:
511   {
512     Measure x( "LOAD POOL" );
513     for_( it, repositories.begin(), repositories.end() )
514     {
515       Measure x( "LOAD POOL "+(*it).info().alias() );
516       const zypp::ResStore & store( (*it).resolvables() );
517       getZYpp()->addResolvables( store );
518     }
519   }
520
521
522   if ( 1 )
523   {
524     Measure x( "INIT TARGET" );
525     {
526       zypp::base::LogControl::TmpLineWriter shutUp;
527       getZYpp()->initTarget( sysRoot );
528       //getZYpp()->initTarget( "/" );
529     }
530     dumpPoolStats( SEC << "TargetStore: " << endl,
531                    getZYpp()->target()->resolvables().begin(),
532                    getZYpp()->target()->resolvables().end() ) << endl;
533   }
534
535   USR << "pool: " << pool << endl;
536   pool.satSync();
537
538   waitForInput();
539
540   //std::for_each( pool.begin(), pool.end(), Xprint() );
541
542   //sat::detail::PoolImpl satpool;
543   //sat::Pool satpool;
544   //MIL << satpool << endl;
545
546   ///////////////////////////////////////////////////////////////////
547   INT << "===[END]============================================" << endl << endl;
548   zypp::base::LogControl::instance().logNothing();
549   return 0;
550 }
551