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