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