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