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