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