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