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