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