Imported Upstream version 14.48.2
[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
12 #include "zypp/ZYppCallbacks.h"
13 #include "zypp/NVRAD.h"
14 #include "zypp/ResPool.h"
15 #include "zypp/ResFilters.h"
16 #include "zypp/Package.h"
17 #include "zypp/Pattern.h"
18 #include "zypp/Language.h"
19 #include "zypp/Digest.h"
20 #include "zypp/PackageKeyword.h"
21 #include "zypp/ManagedFile.h"
22
23
24 #include "zypp/parser/TagParser.h"
25 #include "zypp/parser/susetags/PackagesFileReader.h"
26 #include "zypp/parser/susetags/PackagesLangFileReader.h"
27 #include "zypp/parser/susetags/PatternFileReader.h"
28 #include "zypp/parser/susetags/ContentFileReader.h"
29 #include "zypp/parser/susetags/RepoIndex.h"
30 #include "zypp/parser/susetags/RepoParser.h"
31 #include "zypp/cache/CacheStore.h"
32 #include "zypp/RepoManager.h"
33 #include "zypp/RepoInfo.h"
34
35 #include "zypp/repo/DeltaCandidates.h"
36 #include "zypp/repo/PackageProvider.h"
37 #include "zypp/repo/SrcPackageProvider.h"
38
39 #include "zypp/ui/PatchContents.h"
40 #include "zypp/ResPoolProxy.h"
41
42 using namespace std;
43 using namespace zypp;
44 using namespace zypp::functor;
45 using namespace zypp::ui;
46 using zypp::parser::TagParser;
47
48 ///////////////////////////////////////////////////////////////////
49
50 static const Pathname 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 void dbgDu( Selectable::Ptr sel )
97 {
98   if ( sel->installedPoolItem() )
99   {
100     DBG << "i: " << sel->installedPoolItem() << endl
101         << sel->installedPoolItem()->diskusage() << endl;
102   }
103   if ( sel->candidatePoolItem() )
104   {
105     DBG << "c: " << sel->candidatePoolItem() << endl
106         << sel->candidatePoolItem()->diskusage() << endl;
107   }
108   INT << sel << endl
109       << getZYpp()->diskUsage() << endl;
110 }
111
112 ///////////////////////////////////////////////////////////////////
113
114 struct Xprint
115 {
116   bool operator()( const PoolItem & obj_r )
117   {
118     if ( obj_r.status().isLocked() )
119       SEC << obj_r << endl;
120
121 //     handle( asKind<Package>( obj_r ) );
122 //     handle( asKind<Patch>( obj_r ) );
123 //     handle( asKind<Pattern>( obj_r ) );
124 //     handle( asKind<Product>( obj_r ) );
125     return true;
126   }
127
128   void handle( const Package_constPtr & p )
129   {
130     if ( !p )
131       return;
132
133     WAR << p->size() << endl;
134     MIL << p->diskusage() << endl;
135   }
136
137   void handle( const Patch_constPtr & p )
138   {
139     if ( !p )
140       return;
141   }
142
143   void handle( const Pattern_constPtr & p )
144   {
145     if ( !p )
146       return;
147
148     if ( p->vendor().empty() )
149       ERR << p << endl;
150     else if ( p->vendor() == "SUSE (assumed)" )
151       SEC << p << endl;
152   }
153
154   void handle( const Product_constPtr & p )
155   {
156     if ( !p )
157       return;
158
159     USR << p << endl;
160     USR << p->vendor() << endl;
161     USR << p->type() << endl;
162   }
163
164   template<class _C>
165   bool operator()( const _C & obj_r )
166   {
167     return true;
168   }
169 };
170
171 ///////////////////////////////////////////////////////////////////
172 struct SetTransactValue
173 {
174   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
175   : _newVal( newVal_r )
176   , _causer( causer_r )
177   {}
178
179   ResStatus::TransactValue   _newVal;
180   ResStatus::TransactByValue _causer;
181
182   bool operator()( const PoolItem & pi ) const
183   {
184     bool ret = pi.status().setTransactValue( _newVal, _causer );
185     if ( ! ret )
186       ERR << _newVal <<  _causer << " " << pi << endl;
187     return ret;
188   }
189 };
190
191 struct StatusReset : public SetTransactValue
192 {
193   StatusReset()
194   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
195   {}
196 };
197
198 struct StatusInstall : public SetTransactValue
199 {
200   StatusInstall()
201   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
202   {}
203 };
204
205 ///////////////////////////////////////////////////////////////////
206
207 bool solve( bool establish = false )
208 {
209   if ( establish )
210   {
211     bool eres = false;
212     {
213       zypp::base::LogControl::TmpLineWriter shutUp;
214       eres = getZYpp()->resolver()->establishPool();
215     }
216     if ( ! eres )
217     {
218       ERR << "establish " << eres << endl;
219       return false;
220     }
221     MIL << "establish " << eres << endl;
222   }
223
224   bool rres = false;
225   {
226     zypp::base::LogControl::TmpLineWriter shutUp;
227     rres = getZYpp()->resolver()->resolvePool();
228   }
229   if ( ! rres )
230   {
231     ERR << "resolve " << rres << endl;
232     return false;
233   }
234   MIL << "resolve " << rres << endl;
235   return true;
236 }
237
238 bool install()
239 {
240   SEC << getZYpp()->commit( ZYppCommitPolicy() ) << endl;
241   return true;
242 }
243
244 ///////////////////////////////////////////////////////////////////
245
246 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
247 {
248   virtual void start( const Resolvable::constPtr & script_r,
249                       const Pathname & path_r,
250                       Task task_r )
251   {
252     SEC << __FUNCTION__ << endl
253     << "  " << script_r << endl
254     << "  " << path_r   << endl
255     << "  " << task_r   << endl;
256   }
257
258   virtual bool progress( Notify notify_r, const std::string & text_r )
259   {
260     SEC << __FUNCTION__ << endl
261     << "  " << notify_r << endl
262     << "  " << text_r   << endl;
263     return true;
264   }
265
266   virtual void problem( const std::string & description_r )
267   {
268     SEC << __FUNCTION__ << endl
269     << "  " << description_r << endl;
270   }
271
272   virtual void finish()
273   {
274     SEC << __FUNCTION__ << endl;
275   }
276
277 };
278 ///////////////////////////////////////////////////////////////////
279
280 struct DigestReceive : public callback::ReceiveReport<DigestReport>
281 {
282   DigestReceive()
283   {
284     connect();
285   }
286
287   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
288   {
289     USR << endl;
290     return false;
291   }
292   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
293   {
294     USR << endl;
295     return false;
296   }
297   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
298   {
299     USR << "fle " << PathInfo(file) << endl;
300     USR << "req " << requested << endl;
301     USR << "fnd " << found << endl;
302
303     waitForInput();
304
305     return false;
306   }
307 };
308
309 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
310 {
311   KeyRingSignalsReceive()
312   {
313     connect();
314   }
315   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
316   {
317     USR << endl;
318   }
319   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
320   {
321     USR << endl;
322   }
323 };
324
325 ///////////////////////////////////////////////////////////////////
326
327 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
328 {
329   virtual Action requestMedia( Url & source
330                                , unsigned mediumNr
331                                , Error error
332                                , const std::string & description )
333   {
334     SEC << __FUNCTION__ << endl
335     << "  " << source << endl
336     << "  " << mediumNr << endl
337     << "  " << error << endl
338     << "  " << description << endl;
339     return IGNORE;
340   }
341 };
342
343 ///////////////////////////////////////////////////////////////////
344
345 namespace container
346 {
347   template<class _Tp>
348     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
349     { return cont.find( val ) != cont.end(); }
350 }
351
352 ///////////////////////////////////////////////////////////////////
353
354 struct AddResolvables
355 {
356   bool operator()( const Repository & src ) const
357   {
358     getZYpp()->addResolvables( src.resolvables() );
359     return true;
360   }
361 };
362
363 ///////////////////////////////////////////////////////////////////
364
365
366 std::ostream & operator<<( std::ostream & str, const iostr::EachLine & obj )
367 {
368   str << "(" << obj.valid() << ")[" << obj.lineNo() << "|" << obj.lineStart() << "]{" << *obj << "}";
369   return str;
370
371 }
372
373 ///////////////////////////////////////////////////////////////////
374
375 #define for_(IT,BEG,END) for ( typeof(BEG) IT = BEG; IT != END; ++IT )
376
377 ///////////////////////////////////////////////////////////////////
378 namespace zypp
379 { /////////////////////////////////////////////////////////////////
380
381
382   void Vtst( const std::string & lhs, const std::string & rhs )
383   {
384     (VendorAttr::instance().equivalent( lhs, rhs )?MIL:ERR) << lhs << " <==> "<< rhs << endl;
385
386   }
387
388   /////////////////////////////////////////////////////////////////
389 } // namespace zypp
390 ///////////////////////////////////////////////////////////////////
391
392 using namespace zypp;
393
394 void tt( std::string dd )
395 {
396   unsigned level = 3;
397   std::string::size_type pos = dd.find( "/" );
398   while ( --level && pos != std::string::npos )
399   {
400     pos = dd.find( "/", pos+1 );
401   }
402   if ( pos != std::string::npos )
403     dd.erase( pos+1 );
404   DBG << dd << "\t" << level << " " << pos << endl;
405 }
406
407 /******************************************************************
408 **
409 **      FUNCTION NAME : main
410 **      FUNCTION TYPE : int
411 */
412 int main( int argc, char * argv[] )
413 {
414   //zypp::base::LogControl::instance().logfile( "log.restrict" );
415   INT << "===[START]==========================================" << endl;
416   setenv( "ZYPP_CONF", "/Local/ROOT/zypp.conf", 1 );
417
418   DigestReceive foo;
419   KeyRingSignalsReceive baa;
420
421   RepoManager repoManager( makeRepoManager( "/Local/ROOT" ) );
422
423   RepoInfoList repos = repoManager.knownRepositories();
424   SEC << "/Local/ROOT " << repos << endl;
425
426   for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
427   {
428     RepoInfo & nrepo( *it );
429     if ( ! nrepo.enabled() )
430       continue;
431
432     if ( ! repoManager.isCached( nrepo ) || 0 )
433     {
434       if ( repoManager.isCached( nrepo ) )
435       {
436         SEC << "cleanCache" << endl;
437         repoManager.cleanCache( nrepo );
438       }
439       SEC << "refreshMetadata" << endl;
440       //repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
441       repoManager.refreshMetadata( nrepo );
442       SEC << "buildCache" << endl;
443       repoManager.buildCache( nrepo );
444     }
445
446     SEC << nrepo << endl;
447     Repository nrep( repoManager.createFromCache( nrepo ) );
448     const zypp::ResStore & store( nrep.resolvables() );
449
450     dumpPoolStats( SEC << "Store: " << endl,
451                    store.begin(), store.end() ) << endl;
452     getZYpp()->addResolvables( store );
453   }
454
455   ResPool pool( getZYpp()->pool() );
456   USR << "pool: " << pool << endl;
457   SEC << pool.knownRepositoriesSize() << endl;
458
459   if ( 0 )
460   {
461     {
462       zypp::base::LogControl::TmpLineWriter shutUp;
463       //getZYpp()->initTarget( sysRoot );
464       getZYpp()->initTarget( "/" );
465     }
466     MIL << "Added target: " << pool << endl;
467   }
468
469
470   //std::for_each( pool.begin(), pool.end(), Xprint() );
471
472   PoolItem pi = getPi<Patch>( "fetchmsttfonts.sh" );
473   USR << pi << endl;
474
475   //pi.status().setTransact( true, ResStatus::USER );
476   //install();
477
478  ///////////////////////////////////////////////////////////////////
479   INT << "===[END]============================================" << endl << endl;
480   zypp::base::LogControl::instance().logNothing();
481   return 0;
482 }
483