Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / devel / devel.ma / MaTest.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
28
29 #include "zypp/RepoManager.h"
30 #include "zypp/Repository.h"
31 #include "zypp/RepoInfo.h"
32
33 #include "zypp/repo/PackageProvider.h"
34
35 #include "zypp/ResPoolProxy.h"
36
37 #include "zypp/sat/Pool.h"
38 #include "zypp/sat/LocaleSupport.h"
39 #include "zypp/sat/LookupAttr.h"
40 #include "zypp/sat/SolvableSet.h"
41 #include "zypp/sat/SolvIterMixin.h"
42 #include "zypp/sat/detail/PoolImpl.h"
43 #include "zypp/sat/WhatObsoletes.h"
44 #include "zypp/PoolQuery.h"
45 #include "zypp/ServiceInfo.h"
46
47 #include <boost/mpl/int.hpp>
48
49 using namespace std;
50 using namespace zypp;
51 using namespace zypp::functor;
52 using namespace zypp::ui;
53
54 ///////////////////////////////////////////////////////////////////
55
56 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
57
58 ///////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////
60 namespace zypp
61 { /////////////////////////////////////////////////////////////////
62
63 bool queryInstalledEditionHelper( const std::string & name_r,
64                                   const Edition &     ed_r,
65                                   const Arch &        arch_r )
66 {
67   if ( ed_r == Edition::noedition )
68     return true;
69   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
70     return true;
71   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
72     return true;
73
74   return false;
75 }
76
77
78 ManagedFile repoProvidePackage( const PoolItem & pi )
79 {
80   ResPool _pool( getZYpp()->pool() );
81   repo::RepoMediaAccess _access;
82
83   // Redirect PackageProvider queries for installed editions
84   // (in case of patch/delta rpm processing) to rpmDb.
85   repo::PackageProviderPolicy packageProviderPolicy;
86   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
87
88   Package::constPtr p = asKind<Package>(pi.resolvable());
89
90   // Build a repository list for repos
91   // contributing to the pool
92   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
93                                                            _pool.knownRepositoriesEnd() ) );
94   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
95   return pkgProvider.providePackage();
96 }
97
98   /////////////////////////////////////////////////////////////////
99 } // namespace zypp
100 ///////////////////////////////////////////////////////////////////
101 ///////////////////////////////////////////////////////////////////
102
103 namespace zypp
104 {
105   template <class TLIterator, class TRIterator, class TFunction>
106       inline int invokeOnEach( TLIterator lbegin_r, TLIterator lend_r,
107                                TRIterator rbegin_r, TRIterator rend_r,
108                                TFunction fnc_r )
109       {
110         int cnt = 0;
111         for ( TLIterator lit = lbegin_r; lit != lend_r; ++lit )
112         {
113           for ( TRIterator rit = rbegin_r; rit != rend_r; ++rit )
114           {
115             ++cnt;
116             if ( ! fnc_r( *lit, *rit ) )
117               return -cnt;
118           }
119         }
120         return cnt;
121       }
122 }
123
124 ///////////////////////////////////////////////////////////////////
125
126 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
127 {
128   str << pi << endl;
129   Package::constPtr p( asKind<Package>(pi) );
130   if ( p )
131   {
132 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
133     Locale l( "de" );
134     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
135     l = Locale( "fr" );
136     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
137     l = Locale( "dsdf" );
138     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
139     OUTS( summary );
140     OUTS( installSize );
141     OUTS( downloadSize );
142     OUTS( sourcePkgName );
143     OUTS( sourcePkgEdition );
144     OUTS( checksum );
145     OUTS( location );
146 #undef OUTS
147
148
149   }
150   return str;
151 }
152
153 struct Xprint
154 {
155   bool operator()( const PoolItem & obj_r )
156   {
157     //MIL << obj_r << endl;
158     //DBG << " -> " << obj_r->satSolvable() << endl;
159
160     return true;
161   }
162
163   bool operator()( const sat::Solvable & obj_r )
164   {
165     //dumpOn( MIL, obj_r ) << endl;
166     return true;
167   }
168 };
169
170 ///////////////////////////////////////////////////////////////////
171 struct SetTransactValue
172 {
173   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
174   : _newVal( newVal_r )
175   , _causer( causer_r )
176   {}
177
178   ResStatus::TransactValue   _newVal;
179   ResStatus::TransactByValue _causer;
180
181   bool operator()( const PoolItem & pi ) const
182   {
183     bool ret = pi.status().setTransactValue( _newVal, _causer );
184     if ( ! ret )
185       ERR << _newVal <<  _causer << " " << pi << endl;
186     return ret;
187   }
188 };
189
190 struct StatusReset : public SetTransactValue
191 {
192   StatusReset()
193   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
194   {}
195 };
196
197 struct StatusInstall : public SetTransactValue
198 {
199   StatusInstall()
200   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
201   {}
202 };
203
204 ///////////////////////////////////////////////////////////////////
205
206 bool solve()
207 {
208   bool rres = false;
209   {
210     //zypp::base::LogControl::TmpLineWriter shutUp;
211     rres = getZYpp()->resolver()->resolvePool();
212   }
213   if ( ! rres )
214   {
215     ERR << "resolve " << rres << endl;
216     getZYpp()->resolver()->problems();
217     return false;
218   }
219   MIL << "resolve " << rres << endl;
220   return true;
221 }
222
223 bool install()
224 {
225   ZYppCommitPolicy pol;
226   pol.dryRun(true);
227   pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
228   SEC << getZYpp()->commit( pol ) << endl;
229   return true;
230 }
231
232 void testcase()
233 {
234   getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
235 }
236
237 ///////////////////////////////////////////////////////////////////
238
239 struct DigestReceive : public callback::ReceiveReport<DigestReport>
240 {
241   DigestReceive()
242   {
243     connect();
244   }
245
246   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
247   {
248     USR << endl;
249     return false;
250   }
251   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
252   {
253     USR << endl;
254     return false;
255   }
256   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
257   {
258     USR << "fle " << PathInfo(file) << endl;
259     USR << "req " << requested << endl;
260     USR << "fnd " << found << endl;
261     return false;
262   }
263 };
264
265 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
266 {
267   KeyRingSignalsReceive()
268   {
269     connect();
270   }
271   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
272   {
273     USR << endl;
274   }
275   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
276   {
277     USR << endl;
278   }
279 };
280
281 ///////////////////////////////////////////////////////////////////
282
283 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
284 {
285   virtual Action requestMedia( Url & source
286                                , unsigned mediumNr
287                                , const std::string & label
288                                , Error error
289                                , const std::string & description
290                                , const std::vector<std::string> & devices
291                                , unsigned int & dev_current )
292   {
293     SEC << __FUNCTION__ << endl
294     << "  " << source << endl
295     << "  " << mediumNr << endl
296     << "  " << label << endl
297     << "  " << error << endl
298     << "  " << description << endl
299     << "  " << devices << endl
300     << "  " << dev_current << endl;
301     return IGNORE;
302   }
303 };
304
305 ///////////////////////////////////////////////////////////////////
306
307 namespace container
308 {
309   template<class Tp>
310     bool isIn( const std::set<Tp> & cont, const typename std::set<Tp>::value_type & val )
311     { return cont.find( val ) != cont.end(); }
312 }
313 ///////////////////////////////////////////////////////////////////
314
315 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
316 {
317   SEC << *l << " - " << *r << endl;
318   INT << "== " << (l==r) << endl;
319   INT << "!= " << (l!=r) << endl;
320 }
321
322 bool isTrue()  { return true; }
323 bool isFalse() { return false; }
324
325 void dumpIdStr()
326 {
327   for ( int i = -3; i < 30; ++i )
328   {
329     DBG << i << '\t' << IdString( i ) << endl;
330   }
331 }
332
333 void ttt( const char * lhs, const char * rhs )
334 {
335   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
336 }
337
338 namespace zypp
339 {
340 namespace filter
341 {
342   template <class TMemFun, class TValue>
343   class HasValue
344   {
345     public:
346       HasValue( TMemFun fun_r, TValue val_r )
347       : _fun( fun_r ), _val( val_r )
348       {}
349       template <class Tp>
350       bool operator()( const Tp & obj_r ) const
351       { return( _fun && (obj_r.*_fun)() == _val ); }
352     private:
353       TMemFun _fun;
354       TValue  _val;
355   };
356
357   template <class TMemFun, class TValue>
358   HasValue<TMemFun, TValue> byValue( TMemFun fun_r, TValue val_r )
359   { return HasValue<TMemFun, TValue>( fun_r, val_r ); }
360 }
361
362 }
363
364 template <class L>
365 struct TestO { TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
366
367 template <class L>
368 std::ostream & operator<<( std::ostream & str, const TestO<L> & obj )
369 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
370
371 template <class L>
372 TestO<L> testO( const L & lhs )
373 { return TestO<L>( lhs ); }
374
375 template <class L, class R>
376 void testCMP( const L & lhs, const R & rhs )
377 {
378   MIL << "LHS " << testO(lhs) << endl;
379   MIL << "RHS " << rhs << endl;
380
381 #define OUTS(S) DBG << #S << ": " << (S) << endl
382   OUTS( lhs.compare(rhs) );
383   OUTS( lhs != rhs );
384   OUTS( lhs <  rhs );
385   OUTS( lhs <= rhs );
386   OUTS( lhs == rhs );
387   OUTS( lhs >= rhs );
388   OUTS( lhs >  rhs );
389 #undef OUTS
390 }
391
392 inline bool useRepo( RepoInfo & repo )
393 {
394   //return repo.alias()  == "matest";
395   return repo.enabled();
396 }
397
398 /******************************************************************
399 **
400 **      FUNCTION NAME : main
401 **      FUNCTION TYPE : int
402 */
403 int main( int argc, char * argv[] )
404 try {
405   --argc;
406   ++argv;
407   zypp::base::LogControl::instance().logToStdErr();
408   INT << "===[START]==========================================" << endl;
409   ZConfig::instance();
410
411   ResPool   pool( ResPool::instance() );
412   sat::Pool satpool( sat::Pool::instance() );
413
414   if ( 0 )
415   {
416     Measure x( "INIT TARGET" );
417     {
418       {
419         zypp::base::LogControl::TmpLineWriter shutUp;
420         getZYpp()->initializeTarget( sysRoot );
421       }
422       getZYpp()->target()->load();
423       USR << getZYpp()->target()->targetDistribution() << endl;
424       USR << getZYpp()->target()->targetDistributionRelease() << endl;
425     }
426   }
427
428   if ( 1 )
429   {
430     RepoManager repoManager( makeRepoManager( sysRoot ) );
431     RepoInfoList repos = repoManager.knownRepositories();
432
433     // launch repos
434     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
435     {
436       RepoInfo & nrepo( *it );
437       SEC << nrepo << endl;
438
439       if ( ! useRepo( nrepo ) )
440         continue;
441
442       if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
443       {
444         if ( repoManager.isCached( nrepo ) )
445         {
446           SEC << "cleanCache" << endl;
447           repoManager.cleanCache( nrepo );
448         }
449         SEC << "refreshMetadata" << endl;
450         //repoManager.refreshMetadata( nrepo );
451         SEC << "buildCache" << endl;
452         repoManager.buildCache( nrepo );
453       }
454     }
455
456     // create from cache:
457     {
458       Measure x( "CREATE FROM CACHE" );
459       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
460       {
461         RepoInfo & nrepo( *it );
462         if ( ! useRepo( nrepo ) )
463           continue;
464
465         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
466         try
467         {
468           repoManager.loadFromCache( nrepo );
469         }
470         catch ( const Exception & exp )
471         {
472           MIL << "Try to rebuild cache..." << endl;
473           SEC << "cleanCache" << endl;
474           repoManager.cleanCache( nrepo );
475           SEC << "buildCache" << endl;
476           repoManager.buildCache( nrepo );
477           SEC << "Create from cache" << endl;
478           repoManager.loadFromCache( nrepo );
479         }
480
481         USR << "pool: " << pool << endl;
482       }
483     }
484   }
485
486   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
487   USR << "pool: " << pool << endl;
488
489   ///////////////////////////////////////////////////////////////////
490
491   if ( 0 )
492   {
493     Measure x( "Upgrade" );
494     getZYpp()->resolver()->doUpgrade();
495   }
496
497   ///////////////////////////////////////////////////////////////////
498   ///////////////////////////////////////////////////////////////////
499 //  UNTh_(2)product:openSUSE-11.1.x86_64(openSUSE-dvd-11.1)
500 //  U_Th_(583)yast2-ntp-client-2.17.1-1.26.noarch(openSUSE-dvd-11.1)
501 //  U_Th_(1652)kernel-default-2.6.27-7.2.x86_64(openSUSE-dvd-11.1)
502 //  U_Th_(2490)ntp-4.2.4p5-1.6.x86_64(openSUSE-dvd-11.1)
503 //  UNTh_(2545)openSUSE-release-11.1-1.4.x86_64(openSUSE-dvd-11.1)
504 //  USTh_(3462)pattern:base-11.1-46.1.x86_64(openSUSE-dvd-11.1)
505 //  USTh_(3672)pattern:x11-11.1-46.1.x86_64(openSUSE-dvd-11.1)
506 //  USTu_(3680)pattern:xfce-11.1-46.1.x86_64(openSUSE-dvd-11.1)
507
508
509   getZYpp()->resolver()->addRequire( Capability("product:openSUSE") );
510   getZYpp()->resolver()->addRequire( Capability("yast2-ntp-client") );
511   getZYpp()->resolver()->addRequire( Capability("kernel-default") );
512   getZYpp()->resolver()->addRequire( Capability("ntp") );
513   getZYpp()->resolver()->addRequire( Capability("openSUSE-release") );
514   getZYpp()->resolver()->addRequire( Capability("pattern:base") );
515   getZYpp()->resolver()->addRequire( Capability("pattern:x11") );
516   getZYpp()->resolver()->addRequire( Capability("pattern:xfce") );
517
518   solve();
519
520   vdumpPoolStats( USR << "Transacting:"<< endl,
521                   make_filter_begin<resfilter::ByTransact>(pool),
522                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
523
524   ByteCount sze;
525   ByteCount dusze;
526   DiskUsageCounter ducounter( DiskUsageCounter::justRootPartition() );
527   for_( it, make_filter_begin<resfilter::ByTransact>(pool), make_filter_end<resfilter::ByTransact>(pool) )
528   {
529     USR << *it << endl;
530     ByteCount csze( ducounter.disk_usage( *it ).begin()->commitDiff() );
531     sze += (*it)->installSize();
532     dusze += csze;
533     DBG <<(*it)->installSize() << " vs. " << csze << " | " << ByteCount( dusze-sze ) << endl;
534
535   }
536   SEC << sze << endl;
537   SEC << dusze << endl;
538
539   SEC << zypp::getZYpp()->diskUsage() << endl;
540
541   ///////////////////////////////////////////////////////////////////
542   INT << "===[END]============================================" << endl << endl;
543   zypp::base::LogControl::instance().logNothing();
544   return 0;
545 }
546 catch ( const Exception & exp )
547 {
548   INT << exp << endl << exp.historyAsString();
549 }
550 catch (...)
551 {}
552
553