class sat::LocaleSupport: Convenience methods to manage support
[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/Debug.h>
8 #include <zypp/base/Functional.h>
9 #include <zypp/base/IOStream.h>
10 #include <zypp/base/ProvideNumericId.h>
11 #include <zypp/AutoDispose.h>
12
13 #include "zypp/ResPoolProxy.h"
14
15 #include "zypp/ZYppCallbacks.h"
16 #include "zypp/NVRAD.h"
17 #include "zypp/ResPool.h"
18 #include "zypp/ResFilters.h"
19 #include "zypp/ResObjects.h"
20 #include "zypp/Digest.h"
21 #include "zypp/PackageKeyword.h"
22 #include "zypp/TmpPath.h"
23 #include "zypp/ManagedFile.h"
24 #include "zypp/NameKindProxy.h"
25 #include "zypp/pool/GetResolvablesToInsDel.h"
26
27 #include "zypp/RepoManager.h"
28 #include "zypp/Repository.h"
29 #include "zypp/RepoInfo.h"
30
31 #include "zypp/repo/PackageProvider.h"
32
33 #include "zypp/ui/PatchContents.h"
34 #include "zypp/ResPoolProxy.h"
35
36 #include "zypp/sat/Pool.h"
37 #include "zypp/sat/LocaleSupport.h"
38
39 #include <zypp/base/GzStream.h>
40
41 #include <boost/mpl/int.hpp>
42
43 using namespace std;
44 using namespace zypp;
45 using namespace zypp::functor;
46 using namespace zypp::ui;
47
48 ///////////////////////////////////////////////////////////////////
49
50 static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
51
52 ///////////////////////////////////////////////////////////////////
53 ///////////////////////////////////////////////////////////////////
54 namespace zypp
55 { /////////////////////////////////////////////////////////////////
56
57 bool queryInstalledEditionHelper( const std::string & name_r,
58                                   const Edition &     ed_r,
59                                   const Arch &        arch_r )
60 {
61   if ( ed_r == Edition::noedition )
62     return true;
63   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
64     return true;
65   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
66     return true;
67
68   return false;
69 }
70
71
72 ManagedFile repoProvidePackage( const PoolItem & pi )
73 {
74   ResPool _pool( getZYpp()->pool() );
75   repo::RepoMediaAccess _access;
76
77   // Redirect PackageProvider queries for installed editions
78   // (in case of patch/delta rpm processing) to rpmDb.
79   repo::PackageProviderPolicy packageProviderPolicy;
80   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
81
82   Package::constPtr p = asKind<Package>(pi.resolvable());
83
84   // Build a repository list for repos
85   // contributing to the pool
86   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
87                                                            _pool.knownRepositoriesEnd() ) );
88   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
89   return pkgProvider.providePackage();
90 }
91
92   /////////////////////////////////////////////////////////////////
93 } // namespace zypp
94 ///////////////////////////////////////////////////////////////////
95 ///////////////////////////////////////////////////////////////////
96
97 namespace zypp
98 {
99   template <class _LIterator, class _RIterator, class _Function>
100       inline int invokeOnEach( _LIterator lbegin_r, _LIterator lend_r,
101                                _RIterator rbegin_r, _RIterator rend_r,
102                                _Function fnc_r )
103       {
104         int cnt = 0;
105         for ( _LIterator lit = lbegin_r; lit != lend_r; ++lit )
106         {
107           for ( _RIterator rit = rbegin_r; rit != rend_r; ++rit )
108           {
109             ++cnt;
110             if ( ! fnc_r( *lit, *rit ) )
111               return -cnt;
112           }
113         }
114         return cnt;
115       }
116 }
117
118
119 void dbgDu( Selectable::Ptr sel )
120 {
121   if ( sel->installedPoolItem() )
122   {
123     DBG << "i: " << sel->installedPoolItem() << endl
124         << sel->installedPoolItem()->diskusage() << endl;
125   }
126   if ( sel->candidatePoolItem() )
127   {
128     DBG << "c: " << sel->candidatePoolItem() << endl
129         << sel->candidatePoolItem()->diskusage() << endl;
130   }
131   INT << sel << endl
132       << getZYpp()->diskUsage() << endl;
133 }
134
135 ///////////////////////////////////////////////////////////////////
136
137 struct Xprint
138 {
139   bool operator()( const PoolItem & obj_r )
140   {
141     //MIL << obj_r << endl;
142     //DBG << " -> " << obj_r->satSolvable() << endl;
143
144     return true;
145   }
146
147   bool operator()( const sat::Solvable & obj_r )
148   {
149     //dumpOn( MIL, obj_r ) << endl;
150     return true;
151   }
152 };
153
154 ///////////////////////////////////////////////////////////////////
155 struct SetTransactValue
156 {
157   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
158   : _newVal( newVal_r )
159   , _causer( causer_r )
160   {}
161
162   ResStatus::TransactValue   _newVal;
163   ResStatus::TransactByValue _causer;
164
165   bool operator()( const PoolItem & pi ) const
166   {
167     bool ret = pi.status().setTransactValue( _newVal, _causer );
168     if ( ! ret )
169       ERR << _newVal <<  _causer << " " << pi << endl;
170     return ret;
171   }
172 };
173
174 struct StatusReset : public SetTransactValue
175 {
176   StatusReset()
177   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
178   {}
179 };
180
181 struct StatusInstall : public SetTransactValue
182 {
183   StatusInstall()
184   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
185   {}
186 };
187
188 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
189 {
190   if ( nkp.availableEmpty() )
191   {
192     ERR << "No Item to select: " << nkp << endl;
193     return false;
194     ZYPP_THROW( Exception("No Item to select") );
195   }
196
197   if ( arch != Arch() )
198   {
199     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
200     for ( ; it != nkp.availableEnd(); ++it )
201     {
202       if ( (*it)->arch() == arch )
203         return (*it).status().setTransact( true, ResStatus::USER );
204     }
205   }
206
207   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
208 }
209
210 ///////////////////////////////////////////////////////////////////
211
212 bool solve()
213 {
214   bool rres = false;
215   {
216     //zypp::base::LogControl::TmpLineWriter shutUp;
217     rres = getZYpp()->resolver()->resolvePool();
218   }
219   if ( ! rres )
220   {
221     ERR << "resolve " << rres << endl;
222     return false;
223   }
224   MIL << "resolve " << rres << endl;
225   return true;
226 }
227
228 bool install()
229 {
230   SEC << getZYpp()->commit( ZYppCommitPolicy() ) << endl;
231   return true;
232 }
233
234 ///////////////////////////////////////////////////////////////////
235
236 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
237 {
238   virtual void start( const Resolvable::constPtr & script_r,
239                       const Pathname & path_r,
240                       Task task_r )
241   {
242     SEC << __FUNCTION__ << endl
243     << "  " << script_r << endl
244     << "  " << path_r   << endl
245     << "  " << task_r   << endl;
246   }
247
248   virtual bool progress( Notify notify_r, const std::string & text_r )
249   {
250     SEC << __FUNCTION__ << endl
251     << "  " << notify_r << endl
252     << "  " << text_r   << endl;
253     return true;
254   }
255
256   virtual void problem( const std::string & description_r )
257   {
258     SEC << __FUNCTION__ << endl
259     << "  " << description_r << endl;
260   }
261
262   virtual void finish()
263   {
264     SEC << __FUNCTION__ << endl;
265   }
266
267 };
268 ///////////////////////////////////////////////////////////////////
269
270 struct DigestReceive : public callback::ReceiveReport<DigestReport>
271 {
272   DigestReceive()
273   {
274     connect();
275   }
276
277   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
278   {
279     USR << endl;
280     return false;
281   }
282   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
283   {
284     USR << endl;
285     return false;
286   }
287   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
288   {
289     USR << "fle " << PathInfo(file) << endl;
290     USR << "req " << requested << endl;
291     USR << "fnd " << found << endl;
292     return false;
293   }
294 };
295
296 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
297 {
298   KeyRingSignalsReceive()
299   {
300     connect();
301   }
302   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
303   {
304     USR << endl;
305   }
306   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
307   {
308     USR << endl;
309   }
310 };
311
312 ///////////////////////////////////////////////////////////////////
313
314 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
315 {
316   virtual Action requestMedia( Url & source
317                                , unsigned mediumNr
318                                , const std::string & label
319                                , Error error
320                                , const std::string & description
321                                , const std::vector<std::string> & devices
322                                , unsigned int & dev_current )
323   {
324     SEC << __FUNCTION__ << endl
325     << "  " << source << endl
326     << "  " << mediumNr << endl
327     << "  " << label << endl
328     << "  " << error << endl
329     << "  " << description << endl
330     << "  " << devices << endl
331     << "  " << dev_current << endl;
332     return IGNORE;
333   }
334 };
335
336 ///////////////////////////////////////////////////////////////////
337
338 namespace container
339 {
340   template<class _Tp>
341     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
342     { return cont.find( val ) != cont.end(); }
343 }
344 ///////////////////////////////////////////////////////////////////
345
346 void itCmp( const sat::Pool::SolvableIterator & l, const sat::Pool::SolvableIterator & r )
347 {
348   SEC << *l << " - " << *r << endl;
349   INT << "== " << (l==r) << endl;
350   INT << "!= " << (l!=r) << endl;
351 }
352
353 bool isTrue()  { return true; }
354 bool isFalse() { return false; }
355
356 void dumpIdStr()
357 {
358   for ( int i = -3; i < 30; ++i )
359   {
360     DBG << i << '\t' << IdString( i ) << endl;
361   }
362 }
363
364 void ttt( const char * lhs, const char * rhs )
365 {
366   DBG << lhs << " <=> " << rhs << " --> " << ::strcmp( lhs, rhs ) << endl;
367 }
368
369 namespace zypp
370 {
371 namespace filter
372 {
373   template <class _MemFun, class _Value>
374   class HasValue
375   {
376     public:
377       HasValue( _MemFun fun_r, _Value val_r )
378       : _fun( fun_r ), _val( val_r )
379       {}
380       template <class _Tp>
381       bool operator()( const _Tp & obj_r ) const
382       { return( _fun && (obj_r.*_fun)() == _val ); }
383     private:
384       _MemFun _fun;
385       _Value  _val;
386   };
387
388   template <class _MemFun, class _Value>
389   HasValue<_MemFun, _Value> byValue( _MemFun fun_r, _Value val_r )
390   { return HasValue<_MemFun, _Value>( fun_r, val_r ); }
391 }
392
393 }
394
395 template <class L>
396 struct _TestO { _TestO( const L & lhs ) : _lhs( lhs ) {} const L & _lhs; };
397
398 template <class L>
399 std::ostream & operator<<( std::ostream & str, const _TestO<L> & obj )
400 { const L & lhs( obj._lhs); return str << (lhs?'_':'*') << (lhs.empty()?'e':'_') << "'" << lhs << "'"; }
401
402 template <class L>
403 _TestO<L> testO( const L & lhs )
404 { return _TestO<L>( lhs ); }
405
406 template <class L, class R>
407 void testCMP( const L & lhs, const R & rhs )
408 {
409   MIL << "LHS " << testO(lhs) << endl;
410   MIL << "RHS " << rhs << endl;
411
412 #define OUTS(S) DBG << #S << ": " << (S) << endl
413   OUTS( lhs.compare(rhs) );
414   OUTS( lhs != rhs );
415   OUTS( lhs <  rhs );
416   OUTS( lhs <= rhs );
417   OUTS( lhs == rhs );
418   OUTS( lhs >= rhs );
419   OUTS( lhs >  rhs );
420 #undef OUTS
421 }
422
423 namespace zypp
424 {
425 //   poolItemIterator
426 }
427
428 void tt( const std::string & name_r, ResKind kind_r = ResKind::package )
429 {
430   Capability cap( name_r, kind_r );
431   sat::WhatProvides possibleProviders(cap);
432   (possibleProviders.empty()?WAR:MIL) << name_r << endl;
433   for_(iter, possibleProviders.begin(), possibleProviders.end())
434   {
435     MIL << name_r << endl;
436     PoolItem provider = ResPool::instance().find(*iter);
437   }
438 }
439
440 /******************************************************************
441 **
442 **      FUNCTION NAME : main
443 **      FUNCTION TYPE : int
444 */
445 int main( int argc, char * argv[] )
446 try {
447   zypp::base::LogControl::instance().logToStdErr();
448   INT << "===[START]==========================================" << endl;
449
450   sat::Pool satpool( sat::Pool::instance() );
451   ResPool   pool( ResPool::instance() );
452   USR << "pool: " << pool << endl;
453
454   if ( 1 )
455   {
456     RepoManager repoManager( makeRepoManager( sysRoot ) );
457     RepoInfoList repos = repoManager.knownRepositories();
458
459     // launch repos
460     for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
461     {
462       RepoInfo & nrepo( *it );
463       SEC << nrepo << endl;
464
465       if ( ! nrepo.enabled() )
466         continue;
467
468       if ( ! repoManager.isCached( nrepo ) || /*force*/false )
469       {
470         if ( repoManager.isCached( nrepo ) )
471         {
472           SEC << "cleanCache" << endl;
473           repoManager.cleanCache( nrepo );
474         }
475         SEC << "refreshMetadata" << endl;
476         repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
477         SEC << "buildCache" << endl;
478         repoManager.buildCache( nrepo );
479       }
480     }
481
482     // create from cache:
483     {
484       Measure x( "CREATE FROM CACHE" );
485       for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
486       {
487         RepoInfo & nrepo( *it );
488         if ( ! nrepo.enabled() )
489           continue;
490
491         Measure x( "CREATE FROM CACHE "+nrepo.alias() );
492         try
493         {
494           repoManager.loadFromCache( nrepo );
495         }
496         catch ( const Exception & exp )
497         {
498           MIL << "Try to rebuild cache..." << endl;
499           SEC << "cleanCache" << endl;
500           repoManager.cleanCache( nrepo );
501           SEC << "buildCache" << endl;
502           repoManager.buildCache( nrepo );
503           SEC << "Create from cache" << endl;
504           repoManager.loadFromCache( nrepo );
505         }
506
507         USR << "pool: " << pool << endl;
508       }
509     }
510   }
511
512   if ( 0 )
513   {
514     Measure x( "INIT TARGET" );
515     {
516       getZYpp()->initializeTarget( sysRoot );
517       getZYpp()->target()->load();
518     }
519   }
520
521   USR << "pool: " << pool << endl;
522
523   ///////////////////////////////////////////////////////////////////
524 // Dataiterator di;
525 // Id keyname = std2id (pool, "susetags:datadir");
526 // if (keyname)
527 //   {
528 //     Dataitertor di;
529 //     dataiterator_init(&di, repo, 0, keyname, 0, SEARCH_NO_STORAGE_SOLVABLE);
530 //     if (dataiterator_step(&di))
531 //       printf ("datadir: %s\n", di.kv.str);
532 //   }
533
534   sat::LocaleSupport myLocale( Locale("de") );
535
536   if ( myLocale.isAvailable() )
537   {
538     MIL << "Support for locale '" << myLocale.locale() << "' is available." << endl;
539   }
540   if ( ! myLocale.isRequested() )
541   {
542     MIL << "Will enable support for locale '" << myLocale.locale() << "'." << endl;
543     myLocale.setRequested( true );
544   }
545   MIL << "Packages supporting locale '" << myLocale.locale() << "':" << endl;
546   for_( it, myLocale.begin(), myLocale.end() )
547   {
548     // iterate over sat::Solvables
549     MIL << "  " << *it << endl;
550     // or get the PoolItems
551     DBG << "  " << PoolItem(*it) << endl;
552
553   }
554
555 //   for_( it, poolItemIterator(myLocale.begin()), poolItemIterator(myLocale.end()) )
556 //   {
557     // iterate over PoolItem
558 //     MIL << "  " << *it << endl;
559 //   }
560
561
562 #if 0
563   sat::SolvAttr flist( "solvable:filelist" );
564
565   for_( it, pool.byIdentBegin<Package>("zypper"), pool.byIdentEnd<Package>("zypper") )
566   {
567     INT << *it << endl;
568     sat::Solvable s( it->satSolvable() );
569     MIL << sat::SolvAttr::summary << endl;
570     MIL << s.lookupStrAttribute( sat::SolvAttr::summary ) << endl;
571     MIL << s.lookupStrAttribute( sat::SolvAttr::noAttr ) << endl;
572
573     ::Dataitertor di;
574     ::dataiterator_init( &di, 0, s.id(), flist.id(), 0, SEARCH_NO_STORAGE_SOLVABLE );
575     while ( ::dataiterator_step( &di ) )
576     {
577
578     }
579
580   }
581 #endif
582
583 #if 0
584   for_( it, pool.byKindBegin<SrcPackage>(), pool.byKindEnd<SrcPackage>() )
585   {
586     MIL << *it << endl;
587     //tt( (*it)->name() );
588   }
589
590   IdString id ("amarok");
591   sat::WhatProvides w( Capability(id.id()) );
592
593   for_( it, w.begin(), w.end() )
594   {
595     WAR << *it << endl;
596     MIL << PoolItem(*it) << endl;
597     Package_Ptr p( asKind<Package>(PoolItem(*it)) );
598     MIL << p << endl;
599     if ( p )
600     {
601       OnMediaLocation l( p->location() );
602       MIL << l << endl;
603     }
604     //OnMediaLocation
605   }
606
607   sat::Solvable a(65241);
608   PoolItem p( a );
609   USR << p << endl;
610   p.status().setTransact( true, ResStatus::USER );
611   USR << p << endl;
612   USR << PoolItem() << endl;
613 #endif
614
615   if ( 0 )
616   {
617     Measure x( "Upgrade" );
618     UpgradeStatistics u;
619     getZYpp()->resolver()->doUpgrade( u );
620   }
621
622
623   if ( 0 ) {
624   PoolItem pi ( getPi<Package>("amarok") );
625   MIL << pi << endl;
626   if ( pi )
627   {
628     pi.status().setTransact( true, ResStatus::USER );
629     solve();
630     vdumpPoolStats( USR << "Transacting:"<< endl,
631                     make_filter_begin<resfilter::ByTransact>(pool),
632                     make_filter_end<resfilter::ByTransact>(pool) ) << endl;
633
634   }
635   }
636   //vdumpPoolStats( USR << "Pool:"<< endl, pool.begin(), pool.end() ) << endl;
637   //waitForInput();
638
639   //std::for_each( pool.begin(), pool.end(), Xprint() );
640
641   ///////////////////////////////////////////////////////////////////
642   INT << "===[END]============================================" << endl << endl;
643   zypp::base::LogControl::instance().logNothing();
644   return 0;
645 }
646 catch ( const Exception & exp )
647 {
648   INT << exp << endl << exp.historyAsString();
649 }
650 catch (...)
651 {}
652
653