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