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