a8cf1233b8fd976a7a0a3230dc08d4d3b50459ef
[platform/upstream/libzypp.git] / devel / devel.ma / NewPool.cc
1 #include "Tools.h"
2
3 #include "zypp/PoolQueryResult.h"
4
5 #include <zypp/base/PtrTypes.h>
6 #include <zypp/base/Exception.h>
7 #include <zypp/base/Gettext.h>
8 #include <zypp/base/LogTools.h>
9 #include <zypp/base/Debug.h>
10 #include <zypp/base/Functional.h>
11 #include <zypp/base/IOStream.h>
12 #include <zypp/base/InputStream.h>
13 #include <zypp/base/ProvideNumericId.h>
14 #include <zypp/base/Flags.h>
15 #include <zypp/AutoDispose.h>
16
17 #include "zypp/ResPoolProxy.h"
18
19 #include "zypp/ZYppCallbacks.h"
20 #include "zypp/ResPool.h"
21 #include "zypp/ResFilters.h"
22 #include "zypp/ResObjects.h"
23 #include "zypp/Digest.h"
24 #include "zypp/PackageKeyword.h"
25 #include "zypp/TmpPath.h"
26 #include "zypp/ManagedFile.h"
27 #include "zypp/MediaSetAccess.h"
28
29
30 #include "zypp/RepoManager.h"
31 #include "zypp/Repository.h"
32 #include "zypp/RepoInfo.h"
33 #include "zypp/TriBool.h"
34 #include "zypp/repo/PackageProvider.h"
35
36 #include "zypp/ResPoolProxy.h"
37
38 #include "zypp/sat/Pool.h"
39 #include "zypp/sat/LocaleSupport.h"
40 #include "zypp/sat/LookupAttr.h"
41 #include "zypp/sat/AttrMatcher.h"
42 #include "zypp/sat/SolvableSet.h"
43 #include "zypp/sat/SolvIterMixin.h"
44 #include "zypp/sat/detail/PoolImpl.h"
45 #include "zypp/sat/WhatObsoletes.h"
46 #include "zypp/PoolQuery.h"
47 #include "zypp/ServiceInfo.h"
48 #include "zypp/media/MediaPriority.h"
49
50 #include "zypp/target/rpm/RpmDb.h"
51 #include "zypp/target/rpm/RpmHeader.h"
52 #include "zypp/target/rpm/librpmDb.h"
53
54 #include <boost/mpl/int.hpp>
55
56 using namespace std;
57 using namespace zypp;
58 using namespace zypp::functor;
59 using namespace zypp::ui;
60
61 ///////////////////////////////////////////////////////////////////
62
63 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
64
65 ///////////////////////////////////////////////////////////////////
66 ///////////////////////////////////////////////////////////////////
67 namespace zypp
68 { /////////////////////////////////////////////////////////////////
69
70 bool queryInstalledEditionHelper( const std::string & name_r,
71                                   const Edition &     ed_r,
72                                   const Arch &        arch_r )
73 {
74   if ( ed_r == Edition::noedition )
75     return true;
76   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
77     return true;
78   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
79     return true;
80
81   return false;
82 }
83
84
85 ManagedFile repoProvidePackage( const PoolItem & pi )
86 {
87   ResPool _pool( getZYpp()->pool() );
88   repo::RepoMediaAccess _access;
89
90   // Redirect PackageProvider queries for installed editions
91   // (in case of patch/delta rpm processing) to rpmDb.
92   repo::PackageProviderPolicy packageProviderPolicy;
93   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
94
95   Package::constPtr p = asKind<Package>(pi.resolvable());
96
97   // Build a repository list for repos
98   // contributing to the pool
99   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
100                                                            _pool.knownRepositoriesEnd() ) );
101   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
102   return pkgProvider.providePackage();
103 }
104
105   /////////////////////////////////////////////////////////////////
106 } // namespace zypp
107 ///////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////
109
110 namespace zypp
111 {
112   template <class _LIterator, class _RIterator, class _Function>
113       inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
114                                _RIterator rbegin_r, _RIterator rend_r,
115                                _Function fnc_r )
116       {
117         int cnt = 0;
118         for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
119         {
120           for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
121           {
122             ++cnt;
123             if ( ! fnc_r( *lit, *rit ) )
124               return -cnt;
125           }
126         }
127         return cnt;
128       }
129 }
130
131
132 void dbgDu( Selectable::Ptr sel )
133 {
134   if ( sel->installedObj() )
135   {
136     DBG << "i: " << sel->installedObj() << endl
137         << sel->installedObj()->diskusage() << endl;
138   }
139   if ( sel->candidateObj() )
140   {
141     DBG << "c: " << sel->candidateObj() << endl
142         << sel->candidateObj()->diskusage() << endl;
143   }
144   INT << sel << endl
145       << getZYpp()->diskUsage() << endl;
146 }
147
148 ///////////////////////////////////////////////////////////////////
149
150 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
151 {
152   str << pi << endl;
153   Package::constPtr p( asKind<Package>(pi) );
154   if ( p )
155   {
156 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
157     Locale l( "de" );
158     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
159     l = Locale( "fr" );
160     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
161     l = Locale( "dsdf" );
162     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
163     OUTS( summary );
164     OUTS( installSize );
165     OUTS( downloadSize );
166     OUTS( sourcePkgName );
167     OUTS( sourcePkgEdition );
168     OUTS( checksum );
169     OUTS( location );
170 #undef OUTS
171
172
173   }
174   return str;
175 }
176
177 struct Xprint
178 {
179   bool operator()( const PoolItem & obj_r )
180   {
181     //MIL << obj_r << endl;
182     //DBG << " -> " << obj_r->satSolvable() << endl;
183
184     return true;
185   }
186
187   bool operator()( const sat::Solvable & obj_r )
188   {
189     //dumpOn( MIL, obj_r ) << endl;
190     return true;
191   }
192 };
193
194 ///////////////////////////////////////////////////////////////////
195 struct SetTransactValue
196 {
197   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
198   : _newVal( newVal_r )
199   , _causer( causer_r )
200   {}
201
202   ResStatus::TransactValue   _newVal;
203   ResStatus::TransactByValue _causer;
204
205   bool operator()( const PoolItem & pi ) const
206   {
207     bool ret = pi.status().setTransactValue( _newVal, _causer );
208     if ( ! ret )
209       ERR << _newVal <<  _causer << " " << pi << endl;
210     return ret;
211   }
212 };
213
214 struct StatusReset : public SetTransactValue
215 {
216   StatusReset()
217   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
218   {}
219 };
220
221 struct StatusInstall : public SetTransactValue
222 {
223   StatusInstall()
224   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
225   {}
226 };
227
228 ///////////////////////////////////////////////////////////////////
229
230 bool solve()
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   ZYppCommitPolicy pol;
249   pol.dryRun(true);
250   pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
251   SEC << getZYpp()->commit( pol ) << endl;
252   return true;
253 }
254
255 void testcase()
256 {
257   getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
258 }
259
260 ///////////////////////////////////////////////////////////////////
261
262 struct DigestReceive : public callback::ReceiveReport<DigestReport>
263 {
264   DigestReceive()
265   {
266     connect();
267   }
268
269   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
270   {
271     USR << endl;
272     return false;
273   }
274   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
275   {
276     USR << endl;
277     return false;
278   }
279   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
280   {
281     USR << "fle " << PathInfo(file) << endl;
282     USR << "req " << requested << endl;
283     USR << "fnd " << found << endl;
284     return false;
285   }
286 };
287
288 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
289 {
290   KeyRingSignalsReceive()
291   {
292     connect();
293   }
294   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
295   {
296     USR << endl;
297   }
298   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
299   {
300     USR << endl;
301   }
302 };
303
304 ///////////////////////////////////////////////////////////////////
305
306 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
307 {
308   virtual Action requestMedia( Url & source
309                                , unsigned mediumNr
310                                , const std::string & label
311                                , Error error
312                                , const std::string & description
313                                , const std::vector<std::string> & devices
314                                , unsigned int & dev_current )
315   {
316     SEC << __FUNCTION__ << endl
317     << "  " << source << endl
318     << "  " << mediumNr << endl
319     << "  " << label << endl
320     << "  " << error << endl
321     << "  " << description << endl
322     << "  " << devices << endl
323     << "  " << dev_current << endl;
324     return IGNORE;
325   }
326 };
327
328 ///////////////////////////////////////////////////////////////////
329
330 namespace container
331 {
332   template<class _Tp>
333     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
334     { return cont.find( val ) != cont.end(); }
335 }
336 ///////////////////////////////////////////////////////////////////
337
338 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
339 {
340   SEC << *l << " - " << *r << endl;
341   INT << "== " << (l==r) << endl;
342   INT << "!= " << (l!=r) << endl;
343 }
344
345 bool isTrue()  { return true; }
346 bool isFalse() { return false; }
347
348 void dumpIdStr()
349 {
350   for ( int i = -3; i < 30; ++i )
351   {
352     DBG << i << '\t' << IdString( i ) << endl;
353   }
354 }
355
356 void ttt( const char * lhs, const char * rhs )
357 {
358   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
359 }
360
361 namespace zypp
362 {
363 namespace filter
364 {
365   template <class _MemFun, class _Value>
366   class HasValue
367   {
368     public:
369       HasValue( _MemFun fun_r, _Value val_r )
370       : _fun( fun_r ), _val( val_r )
371       {}
372       template <class _Tp>
373       bool operator()( const _Tp & obj_r ) const
374       { return( _fun && (obj_r.*_fun)() == _val ); }
375     private:
376       _MemFun _fun;
377       _Value  _val;
378   };
379
380   template <class _MemFun, class _Value>
381   HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
382   { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
383 }
384
385 }
386
387 template <class L>
388 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
389
390 template <class L>
391 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
392 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
393
394 template <class L>
395 _TestO<L> testO( const L & lhs )
396 { return _TestO<L>( lhs ); }
397
398 template <class L, class R>
399 void testCMP( const L & lhs, const R & rhs )
400 {
401   MIL << "LHS " << testO(lhs) << endl;
402   MIL << "RHS " << rhs << endl;
403
404 #define OUTS(S) DBG << #S << ": " << (S) << endl
405   OUTS( lhs.compare(rhs) );
406   OUTS( lhs != rhs );
407   OUTS( lhs <  rhs );
408   OUTS( lhs <= rhs );
409   OUTS( lhs == rhs );
410   OUTS( lhs >= rhs );
411   OUTS( lhs >  rhs );
412 #undef OUTS
413 }
414
415 #include "zypp/Locks.h"
416 #include "zypp/target/HardLocksFile.h"
417 inline PoolQuery makeTrivialQuery( IdString ident_r )
418 {
419   sat::Solvable::SplitIdent ident( ident_r );
420
421   PoolQuery q;
422   q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
423   q.addKind( ident.kind() );
424   q.setMatchExact();
425   q.setCaseSensitive(true);
426   return q;
427 }
428 inline PoolQuery makeTrivialQuery( const char * ch )
429 { return makeTrivialQuery( IdString(ch) ); }
430 void lktest()
431 {
432   static unsigned i = 0;
433   ResPool pool( ResPool::instance() );
434   target::HardLocksFile::Data newdata;
435   pool.getHardLockQueries( newdata );
436   SEC << '[' << i++ << ']' << newdata << endl;
437 }
438
439
440 Capability guessPackageSpec( const std::string & str_r )
441 {
442   return Capability::guessPackageSpec( str_r );
443 }
444
445
446
447
448 void cut( const Capability & cap )
449 {
450   CapDetail detail( cap.detail() );
451   if ( detail.isSimple() )
452   {
453     MIL << detail.kind() << ": " << detail.name();
454     if ( detail.hasArch() )
455       MIL << " (" << detail.arch() << ")";
456     if ( detail.isVersioned() )
457       MIL << " " << detail.op() << " " << detail.ed();
458     MIL << endl;
459   }
460   else
461   {
462     MIL << "---???---" << endl;
463   }
464 }
465
466 namespace zypp { namespace target {
467   void XRunUpdateMessages( const Pathname & root_r,
468                            const Pathname & messagesPath_r,
469                            const std::vector<sat::Solvable> & checkPackages_r,
470                            ZYppCommitResult & result_r );
471
472 }}
473 using zypp::target::XRunUpdateMessages;
474
475 /******************************************************************
476 **
477 **      FUNCTION NAME : main
478 **      FUNCTION TYPE : int
479 */
480 int main( int argc, char * argv[] )
481 try {
482   --argc,++argv;
483   if (0) {
484     // download the repo index file
485     media::MediaManager mediamanager;
486     media::MediaAccessId mid = mediamanager.open( Url("http://download.opensuse.org") );
487     mediamanager.attach( mid );
488   }
489   zypp::base::LogControl::instance().logToStdErr();
490   INT << "===[START]==========================================" << endl;
491   ZConfig::instance();
492
493   ResPool   pool( ResPool::instance() );
494   sat::Pool satpool( sat::Pool::instance() );
495
496   if ( 0 )
497   {
498     Measure x( "INIT TARGET" );
499     {
500       {
501         //zypp::base::LogControl::TmpLineWriter shutUp;
502         getZYpp()->initializeTarget( sysRoot );
503       }
504       getZYpp()->target()->load();
505       USR << "baseproduct:               " << getZYpp()->target()->baseProduct() << endl;
506       USR << "targetDistribution:        " << getZYpp()->target()->targetDistribution() << endl;
507       USR << "targetDistributionRelease: " << getZYpp()->target()->targetDistributionRelease() << endl;
508       dumpRange( USR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
509     }
510   }
511
512   if ( 0 )
513   {
514     RepoManager repoManager( makeRepoManager( sysRoot ) );
515     ServiceInfoList services = repoManager.knownServices();
516
517     for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
518     {
519       ServiceInfo & nservice( *it );
520       SEC << nservice << endl;
521
522       if ( ! nservice.enabled() )
523         continue;
524
525       repoManager.refreshService( nservice );
526     }
527   }
528
529   if ( 1 )
530   {
531     RepoManager repoManager( makeRepoManager( sysRoot ) );
532     RepoInfoList repos = repoManager.knownRepositories();
533
534     // launch repos
535     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
536     {
537       RepoInfo & nrepo( *it );
538       SEC << nrepo << endl;
539
540       if ( ! nrepo.enabled() )
541         continue;
542
543       if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
544       {
545         if ( repoManager.isCached( nrepo ) )
546         {
547           SEC << "cleanCache" << endl;
548           repoManager.cleanCache( nrepo );
549         }
550         SEC << "refreshMetadata" << endl;
551         //repoManager.refreshMetadata( nrepo );
552         SEC << "buildCache" << endl;
553         repoManager.buildCache( nrepo );
554       }
555     }
556
557     // create from cache:
558     {
559       Measure x( "CREATE FROM CACHE" );
560       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
561       {
562         RepoInfo & nrepo( *it );
563         if ( ! nrepo.enabled() )
564           continue;
565
566         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
567         try
568         {
569           repoManager.loadFromCache( nrepo );
570         }
571         catch ( const Exception & exp )
572         {
573           MIL << "Try to rebuild cache..." << endl;
574           SEC << "cleanCache" << endl;
575           repoManager.cleanCache( nrepo );
576           SEC << "buildCache" << endl;
577           repoManager.buildCache( nrepo );
578           SEC << "Create from cache" << endl;
579           repoManager.loadFromCache( nrepo );
580         }
581
582         //USR << "pool: " << pool << endl;
583       }
584     }
585   }
586
587   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
588   USR << "pool: " << pool << endl;
589
590   ///////////////////////////////////////////////////////////////////
591
592   if ( 0 )
593   {
594     Measure x( "Upgrade" );
595     getZYpp()->resolver()->doUpgrade();
596   }
597
598   ///////////////////////////////////////////////////////////////////
599
600
601
602
603
604 #if 1
605   getZYpp()->resolver()->addRequire( Capability("amarok") );
606   pool.byKindBegin<Package>()->status().setTransact( true, ResStatus::USER );
607   solve();
608   vdumpPoolStats( USR << "Transacting:"<< endl,
609                   make_filter_begin<resfilter::ByTransact>(pool),
610                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
611 #endif
612
613   //////////////////////////////////////////////////////////////////
614   INT << "===[END]============================================" << endl << endl;
615   zypp::base::LogControl::instance().logNothing();
616   return 0;
617 }
618 catch ( const Exception & exp )
619 {
620   INT << exp << endl << exp.historyAsString();
621   throw;
622 }
623 catch (...)
624 {
625   throw;
626 }
627
628