24bd198a241c9959edebc1ea9b384d0dc673e4c7
[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 _LIterator, class _RIterator, class _Function>
106       inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
107                                _RIterator rbegin_r, _RIterator rend_r,
108                                _Function fnc_r )
109       {
110         int cnt = 0;
111         for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
112         {
113           for ( _RIterator 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 void dbgDu( Selectable::Ptr sel )
126 {
127   if ( sel->installedObj() )
128   {
129     DBG << "i: " << sel->installedObj() << endl
130         << sel->installedObj()->diskusage() << endl;
131   }
132   if ( sel->candidateObj() )
133   {
134     DBG << "c: " << sel->candidateObj() << endl
135         << sel->candidateObj()->diskusage() << endl;
136   }
137   INT << sel << endl
138       << getZYpp()->diskUsage() << endl;
139 }
140
141 ///////////////////////////////////////////////////////////////////
142
143 std::ostream & testDump( std::ostream & str, const PoolItem & pi )
144 {
145   str << pi << endl;
146   Package::constPtr p( asKind<Package>(pi) );
147   if ( p )
148   {
149 #define OUTS(V) str << str::form("%-25s: ",#V) << p->V() << endl
150     Locale l( "de" );
151     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
152     l = Locale( "fr" );
153     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
154     l = Locale( "dsdf" );
155     str << str::form("%-25s: ",l.code().c_str()) << p->summary(l) << endl;
156     OUTS( summary );
157     OUTS( installSize );
158     OUTS( downloadSize );
159     OUTS( sourcePkgName );
160     OUTS( sourcePkgEdition );
161     OUTS( checksum );
162     OUTS( location );
163 #undef OUTS
164
165
166   }
167   return str;
168 }
169
170 struct Xprint
171 {
172   bool operator()( const PoolItem & obj_r )
173   {
174     //MIL << obj_r << endl;
175     //DBG << " -> " << obj_r->satSolvable() << endl;
176
177     return true;
178   }
179
180   bool operator()( const sat::Solvable & obj_r )
181   {
182     //dumpOn( MIL, obj_r ) << endl;
183     return true;
184   }
185 };
186
187 ///////////////////////////////////////////////////////////////////
188 struct SetTransactValue
189 {
190   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
191   : _newVal( newVal_r )
192   , _causer( causer_r )
193   {}
194
195   ResStatus::TransactValue   _newVal;
196   ResStatus::TransactByValue _causer;
197
198   bool operator()( const PoolItem & pi ) const
199   {
200     bool ret = pi.status().setTransactValue( _newVal, _causer );
201     if ( ! ret )
202       ERR << _newVal <<  _causer << " " << pi << endl;
203     return ret;
204   }
205 };
206
207 struct StatusReset : public SetTransactValue
208 {
209   StatusReset()
210   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
211   {}
212 };
213
214 struct StatusInstall : public SetTransactValue
215 {
216   StatusInstall()
217   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
218   {}
219 };
220
221 ///////////////////////////////////////////////////////////////////
222
223 bool solve()
224 {
225   bool rres = false;
226   {
227     //zypp::base::LogControl::TmpLineWriter shutUp;
228     rres = getZYpp()->resolver()->resolvePool();
229   }
230   if ( ! rres )
231   {
232     ERR << "resolve " << rres << endl;
233     getZYpp()->resolver()->problems();
234     return false;
235   }
236   MIL << "resolve " << rres << endl;
237   return true;
238 }
239
240 bool install()
241 {
242   ZYppCommitPolicy pol;
243   pol.dryRun(true);
244   pol.rpmInstFlags( pol.rpmInstFlags().setFlag( target::rpm::RPMINST_JUSTDB ) );
245   SEC << getZYpp()->commit( pol ) << endl;
246   return true;
247 }
248
249 void testcase()
250 {
251   getZYpp()->resolver()->createSolverTestcase( "./solverTestcase" );
252 }
253
254 ///////////////////////////////////////////////////////////////////
255
256 struct DigestReceive : public callback::ReceiveReport<DigestReport>
257 {
258   DigestReceive()
259   {
260     connect();
261   }
262
263   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
264   {
265     USR << endl;
266     return false;
267   }
268   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
269   {
270     USR << endl;
271     return false;
272   }
273   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
274   {
275     USR << "fle " << PathInfo(file) << endl;
276     USR << "req " << requested << endl;
277     USR << "fnd " << found << endl;
278     return false;
279   }
280 };
281
282 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
283 {
284   KeyRingSignalsReceive()
285   {
286     connect();
287   }
288   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
289   {
290     USR << endl;
291   }
292   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
293   {
294     USR << endl;
295   }
296 };
297
298 ///////////////////////////////////////////////////////////////////
299
300 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
301 {
302   virtual Action requestMedia( Url & source
303                                , unsigned mediumNr
304                                , const std::string & label
305                                , Error error
306                                , const std::string & description
307                                , const std::vector<std::string> & devices
308                                , unsigned int & dev_current )
309   {
310     SEC << __FUNCTION__ << endl
311     << "  " << source << endl
312     << "  " << mediumNr << endl
313     << "  " << label << endl
314     << "  " << error << endl
315     << "  " << description << endl
316     << "  " << devices << endl
317     << "  " << dev_current << endl;
318     return IGNORE;
319   }
320 };
321
322 ///////////////////////////////////////////////////////////////////
323
324 namespace container
325 {
326   template<class _Tp>
327     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
328     { return cont.find( val ) != cont.end(); }
329 }
330 ///////////////////////////////////////////////////////////////////
331
332 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
333 {
334   SEC << *l << " - " << *r << endl;
335   INT << "== " << (l==r) << endl;
336   INT << "!= " << (l!=r) << endl;
337 }
338
339 bool isTrue()  { return true; }
340 bool isFalse() { return false; }
341
342 void dumpIdStr()
343 {
344   for ( int i = -3; i < 30; ++i )
345   {
346     DBG << i << '\t' << IdString( i ) << endl;
347   }
348 }
349
350 void ttt( const char * lhs, const char * rhs )
351 {
352   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
353 }
354
355 namespace zypp
356 {
357 namespace filter
358 {
359   template <class _MemFun, class _Value>
360   class HasValue
361   {
362     public:
363       HasValue( _MemFun fun_r, _Value val_r )
364       : _fun( fun_r ), _val( val_r )
365       {}
366       template <class _Tp>
367       bool operator()( const _Tp & obj_r ) const
368       { return( _fun && (obj_r.*_fun)() == _val ); }
369     private:
370       _MemFun _fun;
371       _Value  _val;
372   };
373
374   template <class _MemFun, class _Value>
375   HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
376   { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
377 }
378
379 }
380
381 template <class L>
382 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
383
384 template <class L>
385 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
386 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
387
388 template <class L>
389 _TestO<L> testO( const L & lhs )
390 { return _TestO<L>( lhs ); }
391
392 template <class L, class R>
393 void testCMP( const L & lhs, const R & rhs )
394 {
395   MIL << "LHS " << testO(lhs) << endl;
396   MIL << "RHS " << rhs << endl;
397
398 #define OUTS(S) DBG << #S << ": " << (S) << endl
399   OUTS( lhs.compare(rhs) );
400   OUTS( lhs != rhs );
401   OUTS( lhs <  rhs );
402   OUTS( lhs <= rhs );
403   OUTS( lhs == rhs );
404   OUTS( lhs >= rhs );
405   OUTS( lhs >  rhs );
406 #undef OUTS
407 }
408
409 inline bool useRepo( RepoInfo & repo )
410 {
411   return repo.alias()  == "matest";
412   return repo.enabled();
413 }
414
415 /******************************************************************
416 **
417 **      FUNCTION NAME : main
418 **      FUNCTION TYPE : int
419 */
420 int main( int argc, char * argv[] )
421 try {
422   --argc;
423   ++argv;
424   zypp::base::LogControl::instance().logToStdErr();
425   INT << "===[START]==========================================" << endl;
426   ZConfig::instance();
427
428   ResPool   pool( ResPool::instance() );
429   sat::Pool satpool( sat::Pool::instance() );
430
431   if ( 0 )
432   {
433     Measure x( "INIT TARGET" );
434     {
435       {
436         zypp::base::LogControl::TmpLineWriter shutUp;
437         getZYpp()->initializeTarget( sysRoot );
438       }
439       getZYpp()->target()->load();
440       USR << getZYpp()->target()->targetDistribution() << endl;
441       USR << getZYpp()->target()->targetDistributionRelease() << endl;
442     }
443   }
444
445   if ( 1 )
446   {
447     RepoManager repoManager( makeRepoManager( sysRoot ) );
448     RepoInfoList repos = repoManager.knownRepositories();
449
450     // launch repos
451     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
452     {
453       RepoInfo & nrepo( *it );
454       SEC << nrepo << endl;
455
456       if ( ! useRepo( nrepo ) )
457         continue;
458
459       if ( ! repoManager.isCached( nrepo ) || nrepo.type() == repo::RepoType::RPMPLAINDIR )
460       {
461         if ( repoManager.isCached( nrepo ) )
462         {
463           SEC << "cleanCache" << endl;
464           repoManager.cleanCache( nrepo );
465         }
466         SEC << "refreshMetadata" << endl;
467         //repoManager.refreshMetadata( nrepo );
468         SEC << "buildCache" << endl;
469         repoManager.buildCache( nrepo );
470       }
471     }
472
473     // create from cache:
474     {
475       Measure x( "CREATE FROM CACHE" );
476       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
477       {
478         RepoInfo & nrepo( *it );
479         if ( ! useRepo( nrepo ) )
480           continue;
481
482         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
483         try
484         {
485           repoManager.loadFromCache( nrepo );
486         }
487         catch ( const Exception & exp )
488         {
489           MIL << "Try to rebuild cache..." << endl;
490           SEC << "cleanCache" << endl;
491           repoManager.cleanCache( nrepo );
492           SEC << "buildCache" << endl;
493           repoManager.buildCache( nrepo );
494           SEC << "Create from cache" << endl;
495           repoManager.loadFromCache( nrepo );
496         }
497
498         USR << "pool: " << pool << endl;
499       }
500     }
501   }
502
503   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() );
504   USR << "pool: " << pool << endl;
505
506   ///////////////////////////////////////////////////////////////////
507
508   if ( 0 )
509   {
510     Measure x( "Upgrade" );
511     getZYpp()->resolver()->doUpgrade();
512   }
513
514   ///////////////////////////////////////////////////////////////////
515   ///////////////////////////////////////////////////////////////////
516 //  UNTh_(2)product:openSUSE-11.1.x86_64(openSUSE-dvd-11.1)
517 //  U_Th_(583)yast2-ntp-client-2.17.1-1.26.noarch(openSUSE-dvd-11.1)
518 //  U_Th_(1652)kernel-default-2.6.27-7.2.x86_64(openSUSE-dvd-11.1)
519 //  U_Th_(2490)ntp-4.2.4p5-1.6.x86_64(openSUSE-dvd-11.1)
520 //  UNTh_(2545)openSUSE-release-11.1-1.4.x86_64(openSUSE-dvd-11.1)
521 //  USTh_(3462)pattern:base-11.1-46.1.x86_64(openSUSE-dvd-11.1)
522 //  USTh_(3672)pattern:x11-11.1-46.1.x86_64(openSUSE-dvd-11.1)
523 //  USTu_(3680)pattern:xfce-11.1-46.1.x86_64(openSUSE-dvd-11.1)
524
525
526   getZYpp()->resolver()->addRequire( Capability("product:openSUSE") );
527   getZYpp()->resolver()->addRequire( Capability("yast2-ntp-client") );
528   getZYpp()->resolver()->addRequire( Capability("kernel-default") );
529   getZYpp()->resolver()->addRequire( Capability("ntp") );
530   getZYpp()->resolver()->addRequire( Capability("openSUSE-release") );
531   getZYpp()->resolver()->addRequire( Capability("pattern:base") );
532   getZYpp()->resolver()->addRequire( Capability("pattern:x11") );
533   getZYpp()->resolver()->addRequire( Capability("pattern:xfce") );
534
535   solve();
536
537   vdumpPoolStats( USR << "Transacting:"<< endl,
538                   make_filter_begin<resfilter::ByTransact>(pool),
539                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
540
541   ByteCount sze;
542   ByteCount dusze;
543   DiskUsageCounter ducounter( DiskUsageCounter::justRootPartition() );
544   for_( it, make_filter_begin<resfilter::ByTransact>(pool), make_filter_end<resfilter::ByTransact>(pool) )
545   {
546     USR << *it << endl;
547     ByteCount csze( ducounter.disk_usage( *it ).begin()->commitDiff() );
548     sze += (*it)->installSize();
549     dusze += csze;
550     DBG <<(*it)->installSize() << " vs. " << csze << " | " << ByteCount( dusze-sze ) << endl;
551
552   }
553   SEC << sze << endl;
554   SEC << dusze << endl;
555
556   SEC << zypp::getZYpp()->diskUsage() << endl;
557
558   ///////////////////////////////////////////////////////////////////
559   INT << "===[END]============================================" << endl << endl;
560   zypp::base::LogControl::instance().logNothing();
561   return 0;
562 }
563 catch ( const Exception & exp )
564 {
565   INT << exp << endl << exp.historyAsString();
566 }
567 catch (...)
568 {}
569
570