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/LogTools.h>
6 #include <zypp/base/ProvideNumericId.h>
7 #include <zypp/AutoDispose.h>
8
9 #include "zypp/ZYppFactory.h"
10 #include "zypp/ResPoolProxy.h"
11 #include <zypp/CapMatchHelper.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/Package.h"
19 #include "zypp/Pattern.h"
20 #include "zypp/Language.h"
21 #include "zypp/Digest.h"
22 #include "zypp/PackageKeyword.h"
23 #include "zypp/ManagedFile.h"
24 #include "zypp/NameKindProxy.h"
25 #include "zypp/pool/GetResolvablesToInsDel.h"
26
27 #include "zypp/parser/TagParser.h"
28 #include "zypp/parser/susetags/PackagesFileReader.h"
29 #include "zypp/parser/susetags/PackagesLangFileReader.h"
30 #include "zypp/parser/susetags/PatternFileReader.h"
31 #include "zypp/parser/susetags/ContentFileReader.h"
32 #include "zypp/parser/susetags/RepoIndex.h"
33 #include "zypp/parser/susetags/RepoParser.h"
34 #include "zypp/cache/CacheStore.h"
35 #include "zypp/RepoManager.h"
36 #include "zypp/RepoInfo.h"
37
38 #include "zypp/repo/PackageProvider.h"
39
40 #include "zypp/ui/PatchContents.h"
41 #include "zypp/ResPoolProxy.h"
42
43 using namespace std;
44 using namespace zypp;
45 using namespace zypp::functor;
46 using namespace zypp::ui;
47 using zypp::parser::TagParser;
48
49 ///////////////////////////////////////////////////////////////////
50
51 static const Pathname 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 template<class _Res>
120 Selectable::Ptr getSel( const std::string & name_r )
121 {
122   ResPoolProxy uipool( getZYpp()->poolProxy() );
123   for_(it, uipool.byKindBegin<_Res>(), uipool.byKindEnd<_Res>() )
124   {
125     if ( (*it)->name() == name_r )
126       return (*it);
127   }
128   return 0;
129 }
130
131 template<class _Res>
132 PoolItem getPi( const std::string & name_r, const Edition & ed_r, const Arch & arch_r )
133 {
134   PoolItem ret;
135   ResPool pool( getZYpp()->pool() );
136   for_(it, pool.byNameBegin(name_r), pool.byNameEnd(name_r) )
137   {
138     if ( !ret && isKind<_Res>( (*it).resolvable() )
139          && ( ed_r == Edition() || ed_r == (*it)->edition() )
140          && ( arch_r == Arch()  || arch_r == (*it)->arch()  ) )
141     {
142       ret = (*it);
143       MIL << "    ->" << *it << endl;
144     }
145     else
146     {
147       DBG << "     ?" << *it << endl;
148     }
149   }
150   return ret;
151 }
152 template<class _Res>
153 PoolItem getPi( const std::string & name_r )
154 {
155   return getPi<_Res>( name_r, Edition(), Arch() );
156 }
157 template<class _Res>
158 PoolItem getPi( const std::string & name_r, const Edition & ed_r )
159 {
160   return getPi<_Res>( name_r, ed_r, Arch() );
161 }
162 template<class _Res>
163 PoolItem getPi( const std::string & name_r, const Arch & arch_r )
164 {
165   return getPi<_Res>( name_r, Edition(), arch_r );
166 }
167
168 void dbgDu( Selectable::Ptr sel )
169 {
170   if ( sel->installedPoolItem() )
171   {
172     DBG << "i: " << sel->installedPoolItem() << endl
173         << sel->installedPoolItem()->diskusage() << endl;
174   }
175   if ( sel->candidatePoolItem() )
176   {
177     DBG << "c: " << sel->candidatePoolItem() << endl
178         << sel->candidatePoolItem()->diskusage() << endl;
179   }
180   INT << sel << endl
181       << getZYpp()->diskUsage() << endl;
182 }
183
184 ///////////////////////////////////////////////////////////////////
185
186 struct Xprint
187 {
188   bool operator()( const PoolItem & obj_r )
189   {
190     if ( obj_r.status().isLocked() )
191       SEC << obj_r << endl;
192
193 //     handle( asKind<Package>( obj_r ) );
194 //     handle( asKind<Patch>( obj_r ) );
195 //     handle( asKind<Pattern>( obj_r ) );
196 //     handle( asKind<Product>( obj_r ) );
197     return true;
198   }
199
200   void handle( const Package_constPtr & p )
201   {
202     if ( !p )
203       return;
204
205     WAR << p->size() << endl;
206     MIL << p->diskusage() << endl;
207   }
208
209   void handle( const Patch_constPtr & p )
210   {
211     if ( !p )
212       return;
213   }
214
215   void handle( const Pattern_constPtr & p )
216   {
217     if ( !p )
218       return;
219
220     if ( p->vendor().empty() )
221       ERR << p << endl;
222     else if ( p->vendor() == "SUSE (assumed)" )
223       SEC << p << endl;
224   }
225
226   void handle( const Product_constPtr & p )
227   {
228     if ( !p )
229       return;
230
231     USR << p << endl;
232     USR << p->vendor() << endl;
233     USR << p->type() << endl;
234   }
235
236   template<class _C>
237   bool operator()( const _C & obj_r )
238   {
239     return true;
240   }
241 };
242
243 ///////////////////////////////////////////////////////////////////
244 struct SetTransactValue
245 {
246   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
247   : _newVal( newVal_r )
248   , _causer( causer_r )
249   {}
250
251   ResStatus::TransactValue   _newVal;
252   ResStatus::TransactByValue _causer;
253
254   bool operator()( const PoolItem & pi ) const
255   {
256     bool ret = pi.status().setTransactValue( _newVal, _causer );
257     if ( ! ret )
258       ERR << _newVal <<  _causer << " " << pi << endl;
259     return ret;
260   }
261 };
262
263 struct StatusReset : public SetTransactValue
264 {
265   StatusReset()
266   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
267   {}
268 };
269
270 struct StatusInstall : public SetTransactValue
271 {
272   StatusInstall()
273   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
274   {}
275 };
276
277 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
278 {
279   if ( nkp.availableEmpty() )
280   {
281     ERR << "No Item to select: " << nkp << endl;
282     return false;
283     ZYPP_THROW( Exception("No Item to select") );
284   }
285
286   if ( arch != Arch() )
287   {
288     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
289     for ( ; it != nkp.availableEnd(); ++it )
290     {
291       if ( (*it)->arch() == arch )
292         return (*it).status().setTransact( true, ResStatus::USER );
293     }
294   }
295
296   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
297 }
298
299 ///////////////////////////////////////////////////////////////////
300
301 bool solve( bool establish = false )
302 {
303   if ( establish )
304   {
305     bool eres = false;
306     {
307       zypp::base::LogControl::TmpLineWriter shutUp;
308       eres = getZYpp()->resolver()->establishPool();
309     }
310     if ( ! eres )
311     {
312       ERR << "establish " << eres << endl;
313       return false;
314     }
315     MIL << "establish " << eres << endl;
316   }
317
318   bool rres = false;
319   {
320     zypp::base::LogControl::TmpLineWriter shutUp;
321     rres = getZYpp()->resolver()->resolvePool();
322   }
323   if ( ! rres )
324   {
325     ERR << "resolve " << rres << endl;
326     return false;
327   }
328   MIL << "resolve " << rres << endl;
329   return true;
330 }
331
332 bool install()
333 {
334   SEC << getZYpp()->commit( ZYppCommitPolicy() ) << endl;
335   return true;
336 }
337
338 ///////////////////////////////////////////////////////////////////
339
340 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
341 {
342   virtual void start( const Resolvable::constPtr & script_r,
343                       const Pathname & path_r,
344                       Task task_r )
345   {
346     SEC << __FUNCTION__ << endl
347     << "  " << script_r << endl
348     << "  " << path_r   << endl
349     << "  " << task_r   << endl;
350   }
351
352   virtual bool progress( Notify notify_r, const std::string & text_r )
353   {
354     SEC << __FUNCTION__ << endl
355     << "  " << notify_r << endl
356     << "  " << text_r   << endl;
357     return true;
358   }
359
360   virtual void problem( const std::string & description_r )
361   {
362     SEC << __FUNCTION__ << endl
363     << "  " << description_r << endl;
364   }
365
366   virtual void finish()
367   {
368     SEC << __FUNCTION__ << endl;
369   }
370
371 };
372 ///////////////////////////////////////////////////////////////////
373
374 struct DigestReceive : public callback::ReceiveReport<DigestReport>
375 {
376   DigestReceive()
377   {
378     connect();
379   }
380
381   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
382   {
383     USR << endl;
384     return false;
385   }
386   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
387   {
388     USR << endl;
389     return false;
390   }
391   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
392   {
393     USR << "fle " << PathInfo(file) << endl;
394     USR << "req " << requested << endl;
395     USR << "fnd " << found << endl;
396     return false;
397   }
398 };
399
400 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
401 {
402   KeyRingSignalsReceive()
403   {
404     connect();
405   }
406   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
407   {
408     USR << endl;
409   }
410   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
411   {
412     USR << endl;
413   }
414 };
415
416 ///////////////////////////////////////////////////////////////////
417
418 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
419 {
420   virtual Action requestMedia( Url & source
421                                , unsigned mediumNr
422                                , Error error
423                                , const std::string & description )
424   {
425     SEC << __FUNCTION__ << endl
426     << "  " << source << endl
427     << "  " << mediumNr << endl
428     << "  " << error << endl
429     << "  " << description << endl;
430     return IGNORE;
431   }
432 };
433
434 ///////////////////////////////////////////////////////////////////
435
436 namespace container
437 {
438   template<class _Tp>
439     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
440     { return cont.find( val ) != cont.end(); }
441 }
442
443 struct Xverscmp
444 {
445   Xverscmp( ostream * outs_r = 0 )
446   : _outs( outs_r )
447   {}
448
449   bool operator()( const Edition & lhs, const Edition & rhs )
450   {
451     if ( _outs )
452     {
453       int res = lhs.compare( rhs );
454       (*_outs) << lhs << " <> " << rhs << " = " << res << endl;
455     }
456     return true;
457   }
458
459   ostream * _outs;
460 };
461
462 /******************************************************************
463 **
464 **      FUNCTION NAME : main
465 **      FUNCTION TYPE : int
466 */
467 int main( int argc, char * argv[] )
468 {
469   //zypp::base::LogControl::instance().logfile( "log.restrict" );
470   INT << "===[START]==========================================" << endl;
471
472   setenv( "ZYPP_CONF", (sysRoot/"zypp.conf").c_str(), 1 );
473
474   RepoManager repoManager( makeRepoManager( sysRoot ) );
475   RepoInfoList repos = repoManager.knownRepositories();
476   // SEC << "/Local/ROOT " << repos << endl;
477
478   for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
479   {
480     RepoInfo & nrepo( *it );
481     SEC << nrepo << endl;
482
483     if ( ! nrepo.enabled() )
484       continue;
485
486     if ( ! repoManager.isCached( nrepo ) || 0 )
487     {
488       if ( repoManager.isCached( nrepo ) )
489       {
490         SEC << "cleanCache" << endl;
491         repoManager.cleanCache( nrepo );
492       }
493       SEC << "refreshMetadata" << endl;
494       repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
495       SEC << "buildCache" << endl;
496       repoManager.buildCache( nrepo );
497     }
498
499     SEC << "createFromCache" << endl;
500     Repository nrep( repoManager.createFromCache( nrepo ) );
501     const zypp::ResStore & store( nrep.resolvables() );
502     dumpPoolStats( SEC << "Store: " << endl, store.begin(), store.end() ) << endl;
503     getZYpp()->addResolvables( store );
504   }
505
506   if ( 1 )
507   {
508     {
509       zypp::base::LogControl::TmpLineWriter shutUp;
510       getZYpp()->initTarget( sysRoot );
511       //getZYpp()->initTarget( "/" );
512     }
513     SEC << "Added target " << endl;
514     dumpPoolStats( SEC << "Store: " << endl,
515                    getZYpp()->target()->resolvables().begin(),
516                    getZYpp()->target()->resolvables().end() ) << endl;
517   }
518
519   ResPool pool( getZYpp()->pool() );
520   USR << "pool: " << pool << endl;
521
522   {
523     Measure x( "Cross pool edition compare" );
524     //ofstream out( "verscmp.new" );
525
526     std::set<Edition> editions;
527     for_( it, pool.begin(), pool.end() )
528     {
529       editions.insert( (*it)->edition() );
530     }
531     SEC << "Num Editions " << editions.size() << endl;
532     for ( unsigned i = 5; i; --i )
533     {
534       Measure x( "pass " );
535       invokeOnEach( editions.begin(), editions.end(),
536                     editions.begin(), editions.end(),
537                     Xverscmp() );
538     }
539   }
540
541   //std::for_each( pool.begin(), pool.end(), Xprint() );
542
543  ///////////////////////////////////////////////////////////////////
544   INT << "===[END]============================================" << endl << endl;
545   zypp::base::LogControl::instance().logNothing();
546   return 0;
547 }
548