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