backup
[platform/upstream/libzypp.git] / devel / devel.ma / Parse.cc
1 #include "Tools.h"
2
3 #include <zypp/base/PtrTypes.h>
4 #include <zypp/base/Exception.h>
5 #include <zypp/base/LogTools.h>
6 #include <zypp/base/ProvideNumericId.h>
7 #include <zypp/AutoDispose.h>
8
9 #include "zypp/ZYppFactory.h"
10 #include "zypp/ResPoolProxy.h"
11 #include <zypp/CapMatchHelper.h>
12
13 #include "zypp/ZYppCallbacks.h"
14 #include "zypp/NVRAD.h"
15 #include "zypp/ResPool.h"
16 #include "zypp/ResFilters.h"
17 #include "zypp/CapFilters.h"
18 #include "zypp/Package.h"
19 #include "zypp/Pattern.h"
20 #include "zypp/Language.h"
21 #include "zypp/Digest.h"
22 #include "zypp/PackageKeyword.h"
23 #include "zypp/ManagedFile.h"
24 #include "zypp/NameKindProxy.h"
25 #include "zypp/pool/GetResolvablesToInsDel.h"
26
27 #include "zypp/parser/TagParser.h"
28 #include "zypp/parser/susetags/PackagesFileReader.h"
29 #include "zypp/parser/susetags/PackagesLangFileReader.h"
30 #include "zypp/parser/susetags/PatternFileReader.h"
31 #include "zypp/parser/susetags/ContentFileReader.h"
32 #include "zypp/parser/susetags/RepoIndex.h"
33 #include "zypp/parser/susetags/RepoParser.h"
34 #include "zypp/cache/CacheStore.h"
35 #include "zypp/RepoManager.h"
36 #include "zypp/RepoInfo.h"
37
38 #include "zypp/repo/PackageProvider.h"
39
40 #include "zypp/ui/PatchContents.h"
41 #include "zypp/ResPoolProxy.h"
42
43 using namespace std;
44 using namespace zypp;
45 using namespace zypp::functor;
46 using namespace zypp::ui;
47 using zypp::parser::TagParser;
48
49 ///////////////////////////////////////////////////////////////////
50
51 static const Pathname sysRoot( "/Local/ROOT" );
52
53 ///////////////////////////////////////////////////////////////////
54 ///////////////////////////////////////////////////////////////////
55 namespace zypp
56 { /////////////////////////////////////////////////////////////////
57
58 bool queryInstalledEditionHelper( const std::string & name_r,
59                                   const Edition &     ed_r,
60                                   const Arch &        arch_r )
61 {
62   INT << name_r << "-" << ed_r << "." << arch_r << endl;
63   return false;
64 }
65
66 ManagedFile repoProvidePackage( const PoolItem & pi )
67 {
68   ResPool _pool( getZYpp()->pool() );
69   repo::RepoMediaAccess _access;
70
71   // Redirect PackageProvider queries for installed editions
72   // (in case of patch/delta rpm processing) to rpmDb.
73   repo::PackageProviderPolicy packageProviderPolicy;
74   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
75
76   Package::constPtr p = asKind<Package>(pi.resolvable());
77
78   // Build a repository list for repos
79   // contributing to the pool
80   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(),
81                                                            _pool.knownRepositoriesEnd() ) );
82   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
83   return pkgProvider.providePackage();
84 }
85
86   /////////////////////////////////////////////////////////////////
87 } // namespace zypp
88 ///////////////////////////////////////////////////////////////////
89 ///////////////////////////////////////////////////////////////////
90
91 template<class _Res>
92 Selectable::Ptr getSel( const std::string & name_r )
93 {
94   ResPoolProxy uipool( getZYpp()->poolProxy() );
95   for_(it, uipool.byKindBegin<_Res>(), uipool.byKindEnd<_Res>() )
96   {
97     if ( (*it)->name() == name_r )
98       return (*it);
99   }
100   return 0;
101 }
102
103 template<class _Res>
104 PoolItem getPi( const std::string & name_r, const Edition & ed_r, const Arch & arch_r )
105 {
106   PoolItem ret;
107   ResPool pool( getZYpp()->pool() );
108   for_(it, pool.byNameBegin(name_r), pool.byNameEnd(name_r) )
109   {
110     if ( !ret && isKind<_Res>( (*it).resolvable() )
111          && ( ed_r == Edition() || ed_r == (*it)->edition() )
112          && ( arch_r == Arch()  || arch_r == (*it)->arch()  ) )
113     {
114       ret = (*it);
115       MIL << "    ->" << *it << endl;
116     }
117     else
118     {
119       DBG << "     ?" << *it << endl;
120     }
121   }
122   return ret;
123 }
124 template<class _Res>
125 PoolItem getPi( const std::string & name_r )
126 {
127   return getPi<_Res>( name_r, Edition(), Arch() );
128 }
129 template<class _Res>
130 PoolItem getPi( const std::string & name_r, const Edition & ed_r )
131 {
132   return getPi<_Res>( name_r, ed_r, Arch() );
133 }
134 template<class _Res>
135 PoolItem getPi( const std::string & name_r, const Arch & arch_r )
136 {
137   return getPi<_Res>( name_r, Edition(), arch_r );
138 }
139
140
141
142
143 void dbgDu( Selectable::Ptr sel )
144 {
145   if ( sel->installedPoolItem() )
146   {
147     DBG << "i: " << sel->installedPoolItem() << endl
148         << sel->installedPoolItem()->diskusage() << endl;
149   }
150   if ( sel->candidatePoolItem() )
151   {
152     DBG << "c: " << sel->candidatePoolItem() << endl
153         << sel->candidatePoolItem()->diskusage() << endl;
154   }
155   INT << sel << endl
156       << getZYpp()->diskUsage() << endl;
157 }
158
159 ///////////////////////////////////////////////////////////////////
160
161 struct Xprint
162 {
163   bool operator()( const PoolItem & obj_r )
164   {
165 //     handle( asKind<Package>( obj_r ) );
166 //     handle( asKind<Patch>( obj_r ) );
167 //     handle( asKind<Pattern>( obj_r ) );
168 //     handle( asKind<Product>( obj_r ) );
169     return true;
170   }
171
172   void handle( const Package_constPtr & p )
173   {
174     if ( !p )
175       return;
176
177     WAR << p->size() << endl;
178     MIL << p->diskusage() << endl;
179   }
180
181   void handle( const Patch_constPtr & p )
182   {
183     if ( !p )
184       return;
185   }
186
187   void handle( const Pattern_constPtr & p )
188   {
189     if ( !p )
190       return;
191
192     if ( p->vendor().empty() )
193       ERR << p << endl;
194     else if ( p->vendor() == "SUSE (assumed)" )
195       SEC << p << endl;
196   }
197
198   void handle( const Product_constPtr & p )
199   {
200     if ( !p )
201       return;
202
203     USR << p << endl;
204     USR << p->vendor() << endl;
205     USR << p->type() << endl;
206   }
207
208   template<class _C>
209   bool operator()( const _C & obj_r )
210   {
211     return true;
212   }
213 };
214
215 ///////////////////////////////////////////////////////////////////
216 struct SetTransactValue
217 {
218   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
219   : _newVal( newVal_r )
220   , _causer( causer_r )
221   {}
222
223   ResStatus::TransactValue   _newVal;
224   ResStatus::TransactByValue _causer;
225
226   bool operator()( const PoolItem & pi ) const
227   {
228     bool ret = pi.status().setTransactValue( _newVal, _causer );
229     if ( ! ret )
230       ERR << _newVal <<  _causer << " " << pi << endl;
231     return ret;
232   }
233 };
234
235 struct StatusReset : public SetTransactValue
236 {
237   StatusReset()
238   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
239   {}
240 };
241
242 struct StatusInstall : public SetTransactValue
243 {
244   StatusInstall()
245   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
246   {}
247 };
248
249 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
250 {
251   if ( nkp.availableEmpty() )
252   {
253     ERR << "No Item to select: " << nkp << endl;
254     return false;
255     ZYPP_THROW( Exception("No Item to select") );
256   }
257
258   if ( arch != Arch() )
259   {
260     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
261     for ( ; it != nkp.availableEnd(); ++it )
262     {
263       if ( (*it)->arch() == arch )
264         return (*it).status().setTransact( true, ResStatus::USER );
265     }
266   }
267
268   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
269 }
270
271 ///////////////////////////////////////////////////////////////////
272
273 bool solve( bool establish = false )
274 {
275   if ( establish )
276   {
277     bool eres = false;
278     {
279       zypp::base::LogControl::TmpLineWriter shutUp;
280       eres = getZYpp()->resolver()->establishPool();
281     }
282     if ( ! eres )
283     {
284       ERR << "establish " << eres << endl;
285       return false;
286     }
287     MIL << "establish " << eres << endl;
288   }
289
290   bool rres = false;
291   {
292     zypp::base::LogControl::TmpLineWriter shutUp;
293     rres = getZYpp()->resolver()->resolvePool();
294   }
295   if ( ! rres )
296   {
297     ERR << "resolve " << rres << endl;
298     return false;
299   }
300   MIL << "resolve " << rres << endl;
301   return true;
302 }
303
304 ///////////////////////////////////////////////////////////////////
305
306 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
307 {
308   virtual void start( const Resolvable::constPtr & script_r,
309                       const Pathname & path_r,
310                       Task task_r )
311   {
312     SEC << __FUNCTION__ << endl
313     << "  " << script_r << endl
314     << "  " << path_r   << endl
315     << "  " << task_r   << endl;
316   }
317
318   virtual bool progress( Notify notify_r, const std::string & text_r )
319   {
320     SEC << __FUNCTION__ << endl
321     << "  " << notify_r << endl
322     << "  " << text_r   << endl;
323     return true;
324   }
325
326   virtual void problem( const std::string & description_r )
327   {
328     SEC << __FUNCTION__ << endl
329     << "  " << description_r << endl;
330   }
331
332   virtual void finish()
333   {
334     SEC << __FUNCTION__ << endl;
335   }
336
337 };
338 ///////////////////////////////////////////////////////////////////
339
340 struct DigestReceive : public callback::ReceiveReport<DigestReport>
341 {
342   DigestReceive()
343   {
344     connect();
345   }
346
347   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
348   {
349     USR << endl;
350     return false;
351   }
352   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
353   {
354     USR << endl;
355     return false;
356   }
357   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
358   {
359     USR << "fle " << PathInfo(file) << endl;
360     USR << "req " << requested << endl;
361     USR << "fnd " << found << endl;
362
363     waitForInput();
364
365     return false;
366   }
367 };
368
369 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
370 {
371   KeyRingSignalsReceive()
372   {
373     connect();
374   }
375   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
376   {
377     USR << endl;
378   }
379   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
380   {
381     USR << endl;
382   }
383 };
384
385 ///////////////////////////////////////////////////////////////////
386
387 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
388 {
389   virtual Action requestMedia( Url & source
390                                , unsigned mediumNr
391                                , Error error
392                                , const std::string & description )
393   {
394     SEC << __FUNCTION__ << endl
395     << "  " << source << endl
396     << "  " << mediumNr << endl
397     << "  " << error << endl
398     << "  " << description << endl;
399     return IGNORE;
400   }
401 };
402
403 ///////////////////////////////////////////////////////////////////
404
405 namespace container
406 {
407   template<class _Tp>
408     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
409     { return cont.find( val ) != cont.end(); }
410 }
411
412 ///////////////////////////////////////////////////////////////////
413
414 struct AddResolvables
415 {
416   bool operator()( const Repository & src ) const
417   {
418     getZYpp()->addResolvables( src.resolvables() );
419     return true;
420   }
421 };
422
423 ///////////////////////////////////////////////////////////////////
424
425
426 std::ostream & operator<<( std::ostream & str, const iostr::EachLine & obj )
427 {
428   str << "(" << obj.valid() << ")[" << obj.lineNo() << "|" << obj.lineStart() << "]{" << *obj << "}";
429   return str;
430
431 }
432
433 ///////////////////////////////////////////////////////////////////
434
435 #define for_(IT,BEG,END) for ( typeof(BEG) IT = BEG; IT != END; ++IT )
436
437 ///////////////////////////////////////////////////////////////////
438 namespace zypp
439 { /////////////////////////////////////////////////////////////////
440
441
442   void Vtst( const std::string & lhs, const std::string & rhs )
443   {
444     (VendorAttr::instance().equivalent( lhs, rhs )?MIL:ERR) << lhs << " <==> "<< rhs << endl;
445
446   }
447
448   /////////////////////////////////////////////////////////////////
449 } // namespace zypp
450 ///////////////////////////////////////////////////////////////////
451
452 using namespace zypp;
453
454 void tt( std::string dd )
455 {
456   unsigned level = 3;
457   std::string::size_type pos = dd.find( "/" );
458   while ( --level && pos != std::string::npos )
459   {
460     pos = dd.find( "/", pos+1 );
461   }
462   if ( pos != std::string::npos )
463     dd.erase( pos+1 );
464   DBG << dd << "\t" << level << " " << pos << endl;
465 }
466
467 /******************************************************************
468 **
469 **      FUNCTION NAME : main
470 **      FUNCTION TYPE : int
471 */
472 int main( int argc, char * argv[] )
473 {
474   //zypp::base::LogControl::instance().logfile( "log.restrict" );
475   INT << "===[START]==========================================" << endl;
476   setenv( "ZYPP_CONF", "/Local/ROOT/zypp.conf", 1 );
477
478   DigestReceive foo;
479   KeyRingSignalsReceive baa;
480
481   DiskUsageCounter::MountPointSet fakePart;
482   fakePart.insert( DiskUsageCounter::MountPoint( "/",        1024, 10240, 5120, 0LL, false ) );
483 //   fakePart.insert( DiskUsageCounter::MountPoint( "/usr",     1024, 10240, 5120, 0LL, false ) );
484   fakePart.insert( DiskUsageCounter::MountPoint( "/usr/lib", 1024, 10240, 5120, 0LL, false ) );
485   fakePart.insert( DiskUsageCounter::MountPoint( "/usr/bin", 1024, 10240, 5120, 0LL, false ) );
486   getZYpp()->setPartitions( fakePart );
487
488   ResPool pool( getZYpp()->pool() );
489   vdumpPoolStats( USR << "Initial pool:" << endl,
490                   pool.begin(),
491                   pool.end() ) << endl;
492
493
494   RepoManager repoManager( makeRepoManager( "/Local/ROOT" ) );
495   RepoInfoList repos = repoManager.knownRepositories();
496   SEC << repos << endl;
497
498   if ( repos.empty() )
499   {
500     RepoInfo nrepo;
501     nrepo
502         .setAlias( "factorytest" )
503         .setName( "Test Repo for factory." )
504         .setEnabled( true )
505         .setAutorefresh( false )
506         .addBaseUrl( Url("http://dist.suse.de/install/stable-x86/") );
507
508     repoManager.addRepository( nrepo );
509     SEC << "refreshMetadat" << endl;
510     repoManager.refreshMetadata( nrepo );
511     SEC << "buildCache" << endl;
512     repoManager.buildCache( nrepo );
513     SEC << "------" << endl;
514     repos = repoManager.knownRepositories();
515   }
516
517   for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
518   {
519     RepoInfo & nrepo( *it );
520     if ( ! nrepo.enabled() )
521       continue;
522
523     if ( ! repoManager.isCached( nrepo ) || 0 )
524     {
525       if ( repoManager.isCached( nrepo ) )
526       {
527         SEC << "cleanCache" << endl;
528         repoManager.cleanCache( nrepo );
529       }
530       SEC << "refreshMetadata" << endl;
531       repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
532       SEC << "buildCache" << endl;
533       repoManager.buildCache( nrepo );
534     }
535
536     SEC << nrepo << endl;
537     Repository nrep( repoManager.createFromCache( nrepo ) );
538     const zypp::ResStore & store( nrep.resolvables() );
539
540     dumpPoolStats( SEC << "Store: " << endl,
541                    store.begin(), store.end() ) << endl;
542     getZYpp()->addResolvables( store );
543   }
544
545   USR << "pool: " << pool << endl;
546   SEC << pool.knownRepositoriesSize() << endl;
547
548   if ( 1 )
549   {
550     {
551        zypp::base::LogControl::TmpLineWriter shutUp;
552       //getZYpp()->initTarget( sysRoot );
553       getZYpp()->initTarget( "/" );
554     }
555     MIL << "Added target: " << pool << endl;
556   }
557
558   //std::for_each( pool.begin(), pool.end(), Xprint() );
559
560   repo::DeltaCandidates deltas( repo::makeDeltaCandidates( pool.knownRepositoriesBegin(),
561                                                            pool.knownRepositoriesEnd() ) );
562
563   DBG << "patch: " << deltas.patchRpms(0).size() << " " << deltas.patchRpms(0) << endl;
564   DBG << "delta: " << deltas.deltaRpms(0).size() << " " << deltas.deltaRpms(0) << endl;
565
566
567   PoolItem pi( getPi<Package>( "kernel-default" ) );
568   USR << pi << endl;
569
570  ///////////////////////////////////////////////////////////////////
571   INT << "===[END]============================================" << endl << endl;
572   zypp::base::LogControl::instance().logNothing();
573   return 0;
574 }
575