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