Imported Upstream version 16.3.2
[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/base/StrMatcher.h>
16 #include <zypp/AutoDispose.h>
17
18 #include "zypp/ResPoolProxy.h"
19
20 #include "zypp/ZYppCallbacks.h"
21 #include "zypp/ResPool.h"
22 #include "zypp/ResFilters.h"
23 #include "zypp/ResObjects.h"
24 #include "zypp/Digest.h"
25 #include "zypp/PackageKeyword.h"
26 #include "zypp/TmpPath.h"
27 #include "zypp/ManagedFile.h"
28 #include "zypp/MediaSetAccess.h"
29
30
31 #include "zypp/RepoManager.h"
32 #include "zypp/Repository.h"
33 #include "zypp/RepoInfo.h"
34 #include "zypp/TriBool.h"
35 #include "zypp/repo/PackageProvider.h"
36
37 #include "zypp/ResPoolProxy.h"
38
39 #include "zypp/sat/Pool.h"
40 #include "zypp/sat/LocaleSupport.h"
41 #include "zypp/sat/LookupAttr.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 TLIterator, class TRIterator, class TFunction>
113       inline int invokeOnEach( TLIterator lbegin_r, TLIterator lend_r,
114                                TRIterator rbegin_r, TRIterator rend_r,
115                                TFunction fnc_r )
116       {
117         int cnt = 0;
118         for ( TLIterator lit = lbegin_r; lit != lend_r; ++lit )
119         {
120           for ( TRIterator 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 ///////////////////////////////////////////////////////////////////
133
134 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
135 {
136   str << pi << endl;
137   Package::constPtr p( asKind<Package>(pi) );
138   if ( p )
139   {
140 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
141     Locale l( "de" );
142     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
143     l = Locale( "fr" );
144     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
145     l = Locale( "dsdf" );
146     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
147     OUTS( summary );
148     OUTS( installSize );
149     OUTS( downloadSize );
150     OUTS( sourcePkgName );
151     OUTS( sourcePkgEdition );
152     OUTS( checksum );
153     OUTS( location );
154 #undef OUTS
155
156
157   }
158   return str;
159 }
160
161 struct Xprint
162 {
163   bool operator()( const PoolItem & obj_r )
164   {
165     //MIL << obj_r << endl;
166     //DBG << " -> " << obj_r->satSolvable() << endl;
167
168     return true;
169   }
170
171   bool operator()( const sat::Solvable & obj_r )
172   {
173     //dumpOn( MIL, obj_r ) << endl;
174     return true;
175   }
176 };
177
178 ///////////////////////////////////////////////////////////////////
179 struct SetTransactValue
180 {
181   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
182   : _newVal( newVal_r )
183   , _causer( causer_r )
184   {}
185
186   ResStatus::TransactValue   _newVal;
187   ResStatus::TransactByValue _causer;
188
189   bool operator()( const PoolItem & pi ) const
190   {
191     bool ret = pi.status().setTransactValue( _newVal, _causer );
192     if ( ! ret )
193       ERR << _newVal <<  _causer << " " << pi << endl;
194     return ret;
195   }
196 };
197
198 struct StatusReset : public SetTransactValue
199 {
200   StatusReset()
201   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
202   {}
203 };
204
205 struct StatusInstall : public SetTransactValue
206 {
207   StatusInstall()
208   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
209   {}
210 };
211
212 ///////////////////////////////////////////////////////////////////
213
214 bool solve()
215 {
216   bool rres = false;
217   {
218     //zypp::base::LogControl::TmpLineWriter shutUp;
219     rres = getZYpp()->resolver()->resolvePool();
220   }
221   if ( ! rres )
222   {
223     ERR << "resolve " << rres << endl;
224     return false;
225   }
226   MIL << "resolve " << rres << endl;
227   return true;
228 }
229
230 bool install()
231 {
232   ZYppCommitPolicy pol;
233   pol.dryRun(true);
234   pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
235   SEC << getZYpp()->commit( pol ) << endl;
236   return true;
237 }
238
239 void testcase()
240 {
241   getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
242 }
243
244 ///////////////////////////////////////////////////////////////////
245
246 struct DigestReceive : public callback::ReceiveReport<DigestReport>
247 {
248   DigestReceive()
249   {
250     connect();
251   }
252
253   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
254   {
255     USR << endl;
256     return false;
257   }
258   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
259   {
260     USR << endl;
261     return false;
262   }
263   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
264   {
265     USR << "fle " << PathInfo(file) << endl;
266     USR << "req " << requested << endl;
267     USR << "fnd " << found << endl;
268     return false;
269   }
270 };
271
272 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
273 {
274   KeyRingSignalsReceive()
275   {
276     connect();
277   }
278   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
279   {
280     USR << endl;
281   }
282   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
283   {
284     USR << endl;
285   }
286 };
287
288 ///////////////////////////////////////////////////////////////////
289
290 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
291 {
292   virtual Action requestMedia( Url & source
293                                , unsigned mediumNr
294                                , const std::string & label
295                                , Error error
296                                , const std::string & description
297                                , const std::vector<std::string> & devices
298                                , unsigned int & dev_current )
299   {
300     SEC << __FUNCTION__ << endl
301     << "  " << source << endl
302     << "  " << mediumNr << endl
303     << "  " << label << endl
304     << "  " << error << endl
305     << "  " << description << endl
306     << "  " << devices << endl
307     << "  " << dev_current << endl;
308     return IGNORE;
309   }
310 };
311
312 ///////////////////////////////////////////////////////////////////
313
314 namespace container
315 {
316   template<class Tp>
317     bool isIn( const std::set<Tp> & cont, const typename std::set<Tp>::value_type & val )
318     { return cont.find( val ) != cont.end(); }
319 }
320 ///////////////////////////////////////////////////////////////////
321
322 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
323 {
324   SEC << *l << " - " << *r << endl;
325   INT << "== " << (l==r) << endl;
326   INT << "!= " << (l!=r) << endl;
327 }
328
329 bool isTrue()  { return true; }
330 bool isFalse() { return false; }
331
332 void dumpIdStr()
333 {
334   for ( int i = -3; i < 30; ++i )
335   {
336     DBG << i << '\t' << IdString( i ) << endl;
337   }
338 }
339
340 void ttt( const char * lhs, const char * rhs )
341 {
342   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
343 }
344
345 namespace zypp
346 {
347 namespace filter
348 {
349   template <class TMemFun, class TValue>
350   class HasValue
351   {
352     public:
353       HasValue( TMemFun fun_r, TValue val_r )
354       : _fun( fun_r ), _val( val_r )
355       {}
356       template <class Tp>
357       bool operator()( const Tp & obj_r ) const
358       { return( _fun && (obj_r.*_fun)() == _val ); }
359     private:
360       TMemFun _fun;
361       TValue  _val;
362   };
363
364   template <class TMemFun, class TValue>
365   HasValue<TMemFun, TValue> byValue( TMemFun fun_r, TValue val_r )
366   { return HasValue<TMemFun, TValue>( fun_r, val_r ); }
367 }
368
369 }
370
371 template <class L>
372 struct TestO { TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
373
374 template <class L>
375 std::ostream & operator<<( std::ostream & str, const TestO<L> & obj )
376 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
377
378 template <class L>
379 TestO<L> testO( const L & lhs )
380 { return TestO<L>( lhs ); }
381
382 template <class L, class R>
383 void testCMP( const L & lhs, const R & rhs )
384 {
385   MIL << "LHS " << testO(lhs) << endl;
386   MIL << "RHS " << rhs << endl;
387
388 #define OUTS(S) DBG << #S << ": " << (S) << endl
389   OUTS( lhs.compare(rhs) );
390   OUTS( lhs != rhs );
391   OUTS( lhs <  rhs );
392   OUTS( lhs <= rhs );
393   OUTS( lhs == rhs );
394   OUTS( lhs >= rhs );
395   OUTS( lhs >  rhs );
396 #undef OUTS
397 }
398
399 #include "zypp/Locks.h"
400 #include "zypp/target/HardLocksFile.h"
401 inline PoolQuery makeTrivialQuery( IdString ident_r )
402 {
403   sat::Solvable::SplitIdent ident( ident_r );
404
405   PoolQuery q;
406   q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
407   q.addKind( ident.kind() );
408   q.setMatchExact();
409   q.setCaseSensitive(true);
410   return q;
411 }
412 inline PoolQuery makeTrivialQuery( const char * ch )
413 { return makeTrivialQuery( IdString(ch) ); }
414 void lktest()
415 {
416   static unsigned i = 0;
417   ResPool pool( ResPool::instance() );
418   target::HardLocksFile::Data newdata;
419   pool.getHardLockQueries( newdata );
420   SEC << '[' << i++ << ']' << newdata << endl;
421 }
422
423
424 Capability guessPackageSpec( const std::string & str_r )
425 {
426   return Capability::guessPackageSpec( str_r );
427 }
428
429
430
431
432 void cut( const Capability & cap )
433 {
434   CapDetail detail( cap.detail() );
435   if ( detail.isSimple() )
436   {
437     MIL << detail.kind() << ": " << detail.name();
438     if ( detail.hasArch() )
439       MIL << " (" << detail.arch() << ")";
440     if ( detail.isVersioned() )
441       MIL << " " << detail.op() << " " << detail.ed();
442     MIL << endl;
443   }
444   else
445   {
446     MIL << "--- ??? ---" << endl;
447   }
448 }
449
450 namespace zypp { namespace target {
451   void XRunUpdateMessages( const Pathname & root_r,
452                            const Pathname & messagesPath_r,
453                            const std::vector<sat::Solvable> & checkPackages_r,
454                            ZYppCommitResult & result_r );
455
456 }}
457 using zypp::target::XRunUpdateMessages;
458
459 /******************************************************************
460 **
461 **      FUNCTION NAME : main
462 **      FUNCTION TYPE : int
463 */
464 int main( int argc, char * argv[] )
465 try {
466   --argc,++argv;
467   if (0) {
468     // download the repo index file
469     media::MediaManager mediamanager;
470     media::MediaAccessId mid = mediamanager.open( Url("http://download.opensuse.org") );
471     mediamanager.attach( mid );
472   }
473   zypp::base::LogControl::instance().logToStdErr();
474   INT << "===[START]==========================================" << endl;
475   ZConfig::instance();
476
477   ResPool   pool( ResPool::instance() );
478   sat::Pool satpool( sat::Pool::instance() );
479
480   if ( 0 )
481   {
482     Measure x( "INIT TARGET" );
483     {
484       {
485         //zypp::base::LogControl::TmpLineWriter shutUp;
486         getZYpp()->initializeTarget( sysRoot );
487       }
488       getZYpp()->target()->load();
489       USR << "baseproduct:               " << getZYpp()->target()->baseProduct() << endl;
490       USR << "targetDistribution:        " << getZYpp()->target()->targetDistribution() << endl;
491       USR << "targetDistributionRelease: " << getZYpp()->target()->targetDistributionRelease() << endl;
492       dumpRange( USR << "Product ", pool.byKindBegin<Product>(), pool.byKindEnd<Product>() ) << endl;
493     }
494   }
495
496   if ( 0 )
497   {
498     RepoManager repoManager( makeRepoManager( sysRoot ) );
499     ServiceInfoList services = repoManager.knownServices();
500
501     for ( ServiceInfoList::iterator it = services.begin(); it != services.end(); ++it )
502     {
503       ServiceInfo & nservice( *it );
504       SEC << nservice << endl;
505
506       if ( ! nservice.enabled() )
507         continue;
508
509       repoManager.refreshService( nservice );
510     }
511   }
512
513   if ( 1 )
514   {
515     RepoManager repoManager( makeRepoManager( sysRoot ) );
516     RepoInfoList repos = repoManager.knownRepositories();
517
518     // launch repos
519     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
520     {
521       RepoInfo & nrepo( *it );
522       SEC << nrepo << endl;
523
524       if ( ! nrepo.enabled() )
525         continue;
526
527       if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
528       {
529         if ( repoManager.isCached( nrepo ) )
530         {
531           SEC << "cleanCache" << endl;
532           repoManager.cleanCache( nrepo );
533         }
534         SEC << "refreshMetadata" << endl;
535         //repoManager.refreshMetadata( nrepo );
536         SEC << "buildCache" << endl;
537         repoManager.buildCache( nrepo );
538       }
539     }
540
541     // create from cache:
542     {
543       Measure x( "CREATE FROM CACHE" );
544       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
545       {
546         RepoInfo & nrepo( *it );
547         if ( ! nrepo.enabled() )
548           continue;
549
550         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
551         try
552         {
553           repoManager.loadFromCache( nrepo );
554         }
555         catch ( const Exception & exp )
556         {
557           MIL << "Try to rebuild cache..." << endl;
558           SEC << "cleanCache" << endl;
559           repoManager.cleanCache( nrepo );
560           SEC << "buildCache" << endl;
561           repoManager.buildCache( nrepo );
562           SEC << "Create from cache" << endl;
563           repoManager.loadFromCache( nrepo );
564         }
565
566         //USR << "pool: " << pool << endl;
567       }
568     }
569   }
570
571   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
572   USR << "pool: " << pool << endl;
573
574   ///////////////////////////////////////////////////////////////////
575
576   if ( 0 )
577   {
578     Measure x( "Upgrade" );
579     getZYpp()->resolver()->doUpgrade();
580   }
581
582   ///////////////////////////////////////////////////////////////////
583
584
585
586
587
588 #if 1
589   getZYpp()->resolver()->addRequire( Capability("amarok") );
590   pool.byKindBegin<Package>()->status().setTransact( true, ResStatus::USER );
591   solve();
592   vdumpPoolStats( USR << "Transacting:"<< endl,
593                   make_filter_begin<resfilter::ByTransact>(pool),
594                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
595 #endif
596
597   //////////////////////////////////////////////////////////////////
598   INT << "===[END]============================================" << endl << endl;
599   zypp::base::LogControl::instance().logNothing();
600   return 0;
601 }
602 catch ( const Exception & exp )
603 {
604   INT << exp << endl << exp.historyAsString();
605   throw;
606 }
607 catch (...)
608 {
609   throw;
610 }
611
612