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/NameKindProxy.h"
24 #include "zypp/pool/GetResolvablesToInsDel.h"
25
26 #include "zypp/parser/TagParser.h"
27 #include "zypp/parser/susetags/PackagesFileReader.h"
28 #include "zypp/parser/susetags/PackagesLangFileReader.h"
29 #include "zypp/parser/susetags/PatternFileReader.h"
30 #include "zypp/parser/susetags/ContentFileReader.h"
31 #include "zypp/parser/susetags/RepoIndex.h"
32 #include "zypp/parser/susetags/RepoParser.h"
33 #include "zypp/cache/CacheStore.h"
34 #include "zypp/RepoManager.h"
35 #include "zypp/RepoInfo.h"
36
37 #include "zypp/ui/PatchContents.h"
38
39 using namespace std;
40 using namespace zypp;
41 using namespace zypp::functor;
42
43 using zypp::parser::TagParser;
44
45 ///////////////////////////////////////////////////////////////////
46
47 static const Pathname sysRoot( "/Local/ROOT" );
48
49 ///////////////////////////////////////////////////////////////////
50
51 struct Xprint
52 {
53   bool operator()( const PoolItem & obj_r )
54   {
55     //handle( asKind<Package>( obj_r ) );
56     handle( asKind<Patch>( obj_r ) );
57     //handle( asKind<Pattern>( obj_r ) );
58     //handle( asKind<Product>( obj_r ) );
59     return true;
60   }
61
62   void handle( const Package_constPtr & p )
63   {
64     if ( !p )
65       return;
66
67     MIL << p->diskusage() << endl;
68   }
69
70   void handle( const Patch_constPtr & p )
71   {
72     if ( !p )
73       return;
74
75     ui::PatchContents pc( p );
76     MIL << p << endl;
77     dumpRange( DBG << pc.size() << endl,
78                pc.begin(), pc.end() );
79   }
80
81   void handle( const Pattern_constPtr & p )
82   {
83     if ( !p )
84       return;
85
86     MIL << p << endl;
87   }
88
89   void handle( const Product_constPtr & p )
90   {
91     if ( !p )
92       return;
93
94     ERR << p << endl;
95     ERR << p->type() << endl;
96   }
97
98   template<class _C>
99   bool operator()( const _C & obj_r )
100   {
101     return true;
102   }
103 };
104
105 ///////////////////////////////////////////////////////////////////
106 struct SetTransactValue
107 {
108   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
109   : _newVal( newVal_r )
110   , _causer( causer_r )
111   {}
112
113   ResStatus::TransactValue   _newVal;
114   ResStatus::TransactByValue _causer;
115
116   bool operator()( const PoolItem & pi ) const
117   {
118     bool ret = pi.status().setTransactValue( _newVal, _causer );
119     if ( ! ret )
120       ERR << _newVal <<  _causer << " " << pi << endl;
121     return ret;
122   }
123 };
124
125 struct StatusReset : public SetTransactValue
126 {
127   StatusReset()
128   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
129   {}
130 };
131
132 struct StatusInstall : public SetTransactValue
133 {
134   StatusInstall()
135   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
136   {}
137 };
138
139 inline bool g( const NameKindProxy & nkp, Arch arch = Arch() )
140 {
141   if ( nkp.availableEmpty() )
142   {
143     ERR << "No Item to select: " << nkp << endl;
144     return false;
145     ZYPP_THROW( Exception("No Item to select") );
146   }
147
148   if ( arch != Arch() )
149   {
150     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
151     for ( ; it != nkp.availableEnd(); ++it )
152     {
153       if ( (*it)->arch() == arch )
154         return (*it).status().setTransact( true, ResStatus::USER );
155     }
156   }
157
158   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
159 }
160
161 ///////////////////////////////////////////////////////////////////
162
163 bool solve( bool establish = false )
164 {
165   if ( establish )
166   {
167     bool eres = false;
168     {
169       zypp::base::LogControl::TmpLineWriter shutUp;
170       eres = getZYpp()->resolver()->establishPool();
171     }
172     if ( ! eres )
173     {
174       ERR << "establish " << eres << endl;
175       return false;
176     }
177     MIL << "establish " << eres << endl;
178   }
179
180   bool rres = false;
181   {
182     zypp::base::LogControl::TmpLineWriter shutUp;
183     rres = getZYpp()->resolver()->resolvePool();
184   }
185   if ( ! rres )
186   {
187     ERR << "resolve " << rres << endl;
188     return false;
189   }
190   MIL << "resolve " << rres << endl;
191   return true;
192 }
193
194 ///////////////////////////////////////////////////////////////////
195
196 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
197 {
198   virtual void start( const Resolvable::constPtr & script_r,
199                       const Pathname & path_r,
200                       Task task_r )
201   {
202     SEC << __FUNCTION__ << endl
203     << "  " << script_r << endl
204     << "  " << path_r   << endl
205     << "  " << task_r   << endl;
206   }
207
208   virtual bool progress( Notify notify_r, const std::string & text_r )
209   {
210     SEC << __FUNCTION__ << endl
211     << "  " << notify_r << endl
212     << "  " << text_r   << endl;
213     return true;
214   }
215
216   virtual void problem( const std::string & description_r )
217   {
218     SEC << __FUNCTION__ << endl
219     << "  " << description_r << endl;
220   }
221
222   virtual void finish()
223   {
224     SEC << __FUNCTION__ << endl;
225   }
226
227 };
228 ///////////////////////////////////////////////////////////////////
229
230 struct DigestReceive : public callback::ReceiveReport<DigestReport>
231 {
232   DigestReceive()
233   {
234     connect();
235   }
236
237   virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file )
238   {
239     USR << endl;
240     return false;
241   }
242   virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
243   {
244     USR << endl;
245     return false;
246   }
247   virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
248   {
249     USR << "fle " << PathInfo(file) << endl;
250     USR << "req " << requested << endl;
251     USR << "fnd " << found << endl;
252
253     waitForInput();
254
255     return false;
256   }
257 };
258
259 struct KeyRingSignalsReceive : public callback::ReceiveReport<KeyRingSignals>
260 {
261   KeyRingSignalsReceive()
262   {
263     connect();
264   }
265   virtual void trustedKeyAdded( const PublicKey &/*key*/ )
266   {
267     USR << endl;
268   }
269   virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
270   {
271     USR << endl;
272   }
273 };
274
275 ///////////////////////////////////////////////////////////////////
276
277 struct MediaChangeReceive : public callback::ReceiveReport<media::MediaChangeReport>
278 {
279   virtual Action requestMedia( Url & source
280                                , unsigned mediumNr
281                                , Error error
282                                , const std::string & description )
283   {
284     SEC << __FUNCTION__ << endl
285     << "  " << source << endl
286     << "  " << mediumNr << endl
287     << "  " << error << endl
288     << "  " << description << endl;
289     return IGNORE;
290   }
291 };
292
293 ///////////////////////////////////////////////////////////////////
294
295 namespace container
296 {
297   template<class _Tp>
298     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
299     { return cont.find( val ) != cont.end(); }
300 }
301
302 ///////////////////////////////////////////////////////////////////
303
304 struct AddResolvables
305 {
306   bool operator()( const Repository & src ) const
307   {
308     getZYpp()->addResolvables( src.resolvables() );
309     return true;
310   }
311 };
312
313 ///////////////////////////////////////////////////////////////////
314
315
316 std::ostream & operator<<( std::ostream & str, const iostr::EachLine & obj )
317 {
318   str << "(" << obj.valid() << ")[" << obj.lineNo() << "|" << obj.lineStart() << "]{" << *obj << "}";
319   return str;
320
321 }
322
323 ///////////////////////////////////////////////////////////////////
324
325 #define for_(IT,BEG,END) for ( typeof(BEG) IT = BEG; IT != END; ++IT )
326
327 ///////////////////////////////////////////////////////////////////
328 namespace zypp
329 { /////////////////////////////////////////////////////////////////
330
331
332
333   /////////////////////////////////////////////////////////////////
334 } // namespace zypp
335 ///////////////////////////////////////////////////////////////////
336
337 using namespace zypp;
338
339 /******************************************************************
340 **
341 **      FUNCTION NAME : main
342 **      FUNCTION TYPE : int
343 */
344 int main( int argc, char * argv[] )
345 {
346   //zypp::base::LogControl::instance().logfile( "log.restrict" );
347   INT << "===[START]==========================================" << endl;
348
349   DigestReceive foo;
350   KeyRingSignalsReceive baa;
351
352   RepoManager repoManager( makeRepoManager( "/Local/ROOT" ) );
353   RepoInfoList repos = repoManager.knownRepositories();
354   SEC << repos << endl;
355
356   if ( repos.empty() )
357   {
358     RepoInfo nrepo;
359     nrepo
360         .setAlias( "factorytest" )
361         .setName( "Test Repo for factory." )
362         .setEnabled( true )
363         .setAutorefresh( false )
364         .addBaseUrl( Url("http://dist.suse.de/install/stable-x86/") );
365
366     repoManager.addRepository( nrepo );
367     SEC << "refreshMetadat" << endl;
368     repoManager.refreshMetadata( nrepo );
369     SEC << "buildCache" << endl;
370     repoManager.buildCache( nrepo );
371     SEC << "------" << endl;
372     repos = repoManager.knownRepositories();
373   }
374
375   ResPool pool( getZYpp()->pool() );
376   vdumpPoolStats( USR << "Initial pool:" << endl,
377                   pool.begin(),
378                   pool.end() ) << endl;
379
380   for ( RepoInfoList::iterator it = repos.begin(); it != repos.end(); ++it )
381   {
382     RepoInfo & nrepo( *it );
383     if ( ! nrepo.enabled() )
384       continue;
385
386     if ( ! repoManager.isCached( nrepo ) || 0 )
387     {
388       if ( repoManager.isCached( nrepo ) )
389       {
390         SEC << "cleanCache" << endl;
391         repoManager.cleanCache( nrepo );
392       }
393       SEC << "refreshMetadata" << endl;
394       repoManager.refreshMetadata( nrepo, RepoManager::RefreshForced );
395       SEC << "buildCache" << endl;
396       repoManager.buildCache( nrepo );
397     }
398
399     SEC << nrepo << endl;
400     Repository nrep( repoManager.createFromCache( nrepo ) );
401     const zypp::ResStore & store( nrep.resolvables() );
402
403     dumpPoolStats( SEC << "Store: " << endl,
404                    store.begin(), store.end() ) << endl;
405     getZYpp()->addResolvables( store );
406   }
407
408   USR << "pool: " << pool << endl;
409   SEC << pool.knownRepositoriesSize() << endl;
410
411   if ( 0 )
412   {
413     {
414       zypp::base::LogControl::TmpLineWriter shutUp;
415       getZYpp()->initTarget( sysRoot );
416     }
417     MIL << "Added target: " << pool << endl;
418   }
419
420   INT << getZYpp()->diskUsage() << endl;
421
422   std::for_each( pool.begin(), pool.end(), Xprint() );
423
424   ///////////////////////////////////////////////////////////////////
425   INT << "===[END]============================================" << endl << endl;
426   zypp::base::LogControl::instance().logNothing();
427   return 0;
428 }
429