backup
[platform/upstream/libzypp.git] / devel / devel.ma / Parse.cc
1 #include <ctime>
2 #include <iostream>
3 #include "Tools.h"
4
5 #include <zypp/base/PtrTypes.h>
6 #include <zypp/base/Exception.h>
7 #include <zypp/base/ProvideNumericId.h>
8
9 #include "zypp/ZYppFactory.h"
10 #include "zypp/ResPoolProxy.h"
11 #include <zypp/SourceManager.h>
12 #include <zypp/SourceFactory.h>
13
14 #include "zypp/ZYppCallbacks.h"
15 #include "zypp/NVRAD.h"
16 #include "zypp/ResPool.h"
17 #include "zypp/ResFilters.h"
18 #include "zypp/CapFilters.h"
19 #include "zypp/Package.h"
20 #include "zypp/Pattern.h"
21 #include "zypp/Language.h"
22 #include "zypp/NameKindProxy.h"
23 #include "zypp/pool/GetResolvablesToInsDel.h"
24
25
26 using namespace std;
27 using namespace zypp;
28 using namespace zypp::ui;
29 using namespace zypp::functor;
30
31 ///////////////////////////////////////////////////////////////////
32
33 static const Pathname sysRoot( "/Local/ROOT" );
34
35 ///////////////////////////////////////////////////////////////////
36
37 struct ConvertDbReceive : public callback::ReceiveReport<target::ScriptResolvableReport>
38 {
39   virtual void start( const Resolvable::constPtr & script_r,
40                       const Pathname & path_r,
41                       Task task_r )
42   {
43     SEC << __FUNCTION__ << endl
44     << "  " << script_r << endl
45     << "  " << path_r   << endl
46     << "  " << task_r   << endl;
47   }
48
49   virtual bool progress( Notify notify_r, const std::string & text_r )
50   {
51     SEC << __FUNCTION__ << endl
52     << "  " << notify_r << endl
53     << "  " << text_r   << endl;
54     return true;
55   }
56
57   virtual void problem( const std::string & description_r )
58   {
59     SEC << __FUNCTION__ << endl
60     << "  " << description_r << endl;
61   }
62
63   virtual void finish()
64   {
65     SEC << __FUNCTION__ << endl;
66   }
67
68 };
69
70 ///////////////////////////////////////////////////////////////////
71
72 namespace container
73 {
74   template<class _Tp>
75     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
76     { return cont.find( val ) != cont.end(); }
77 }
78
79 ///////////////////////////////////////////////////////////////////
80
81 template<class _Condition>
82   struct SetTrue
83   {
84     SetTrue( _Condition cond_r )
85     : _cond( cond_r )
86     {}
87
88     template<class _Tp>
89       bool operator()( _Tp t ) const
90       {
91         _cond( t );
92         return true;
93       }
94
95     _Condition _cond;
96   };
97
98 template<class _Condition>
99   inline SetTrue<_Condition> setTrue_c( _Condition cond_r )
100   {
101     return SetTrue<_Condition>( cond_r );
102   }
103
104 template <class _Iterator, class _Filter, class _Function>
105   inline _Function for_each_if( _Iterator begin_r, _Iterator end_r,
106                                 _Filter filter_r,
107                                 _Function fnc_r )
108   {
109     for ( _Iterator it = begin_r; it != end_r; ++it )
110       {
111         if ( filter_r( *it ) )
112           {
113             fnc_r( *it );
114           }
115       }
116     return fnc_r;
117   }
118
119 struct PrintPoolItem
120 {
121   void operator()( const PoolItem & pi ) const
122   { USR << pi << " (" << pi.resolvable().get() << ")" <<endl; }
123 };
124
125 template <class _Iterator>
126   std::ostream & vdumpPoolStats( std::ostream & str,
127                                  _Iterator begin_r, _Iterator end_r )
128   {
129     pool::PoolStats stats;
130     std::for_each( begin_r, end_r,
131
132                    functor::chain( setTrue_c(PrintPoolItem()),
133                                    setTrue_c(functor::functorRef<void,ResObject::constPtr>(stats)) )
134
135                  );
136     return str << stats;
137   }
138
139 struct PoolItemSelect
140 {
141   void operator()( const PoolItem & pi ) const
142   {
143     if ( pi->source().numericId() == 2 )
144       pi.status().setTransact( true, ResStatus::USER );
145   }
146 };
147
148 ///////////////////////////////////////////////////////////////////
149 ///////////////////////////////////////////////////////////////////
150
151 struct AddResolvables
152 {
153   bool operator()( const Source_Ref & src ) const
154   {
155     getZYpp()->addResolvables( src.resolvables() );
156     return true;
157   }
158 };
159
160 ///////////////////////////////////////////////////////////////////
161
162 struct SetTransactValue
163 {
164   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
165   : _newVal( newVal_r )
166   , _causer( causer_r )
167   {}
168
169   ResStatus::TransactValue   _newVal;
170   ResStatus::TransactByValue _causer;
171
172   bool operator()( const PoolItem & pi ) const
173   { return pi.status().setTransactValue( _newVal, _causer ); }
174 };
175
176 struct StatusReset : public SetTransactValue
177 {
178   StatusReset()
179   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
180   {}
181 };
182
183
184 inline bool selectForTransact( const NameKindProxy & nkp, Arch arch = Arch() )
185 {
186   if ( nkp.availableEmpty() ) {
187     ERR << "No Item to select: " << nkp << endl;
188     return false;
189     ZYPP_THROW( Exception("No Item to select") );
190   }
191
192   if ( arch != Arch() )
193     {
194       typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
195       for ( ; it != nkp.availableEnd(); ++it )
196       {
197         if ( (*it)->arch() == arch )
198           return (*it).status().setTransact( true, ResStatus::USER );
199       }
200     }
201
202   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
203 }
204
205 void seltest( const NameKindProxy & nks )
206 {
207   SEC << nks << endl;
208   PoolItem av( *nks.availableBegin() );
209   SEC << av << endl;
210   Pattern::constPtr pat( asKind<Pattern>(av.resolvable()) );
211   SEC << pat << endl;
212   WAR << pat->install_packages() << endl;
213   MIL << pat->deps() << endl;
214   MIL << pat->includes() << endl;
215   MIL << pat->extends() << endl;
216 }
217
218 void showProd( const PoolItem & prod )
219 {
220   Product::constPtr p( asKind<Product>(prod) );
221   DBG << prod << endl;
222   MIL << p << endl;
223   MIL << p->distributionName() << endl;
224   MIL << p->distributionEdition() << endl;
225   MIL << p->installtime() << endl;
226 }
227
228 ///////////////////////////////////////////////////////////////////
229 /******************************************************************
230 **
231 **      FUNCTION NAME : main
232 **      FUNCTION TYPE : int
233 */
234 int main( int argc, char * argv[] )
235 {
236   //zypp::base::LogControl::instance().logfile( "log.restrict" );
237   INT << "===[START]==========================================" << endl;
238
239   ConvertDbReceive r;
240   r.connect();
241
242   ResPool pool( getZYpp()->pool() );
243
244   if ( 1 )
245     {
246       zypp::base::LogControl::TmpLineWriter shutUp;
247       Source_Ref src( createSource( "dir:/Local/SLES10" ) );
248       getZYpp()->addResolvables( src.resolvables() );
249     }
250   MIL << pool << endl;
251
252   PoolItem prod( *pool.byKindBegin<Product>() );
253   showProd( prod );
254
255   if ( 1 )
256     {
257       zypp::base::LogControl::TmpLineWriter shutUp;
258       getZYpp()->initTarget( sysRoot );
259       USR << "Added target: " << pool << endl;
260     }
261
262   prod.status().setTransact( true, ResStatus::USER );
263   ZYppCommitPolicy policy;
264   policy.rpmNoSignature();
265   ZYppCommitResult res( getZYpp()->commit( policy ) );
266
267   for_each( pool.byKindBegin<Product>(),
268             pool.byKindEnd<Product>(),
269             showProd );
270
271   dumpPoolStats( USR << "Products:"<< endl,
272                  pool.byKindBegin<Product>(),
273                  pool.byKindEnd<Product>() ) << endl;
274
275
276   zypp::base::LogControl::instance().logNothing();
277   return 0;
278
279
280
281   if ( 1 )
282     {
283 #define selt(K,N) selectForTransact( nameKindProxy<K>( pool, #N ) )
284       selt( Script, fetchmsttfonts.sh-patch-fetchmsttfonts.sh-2 );
285 #undef selt
286     }
287
288   vdumpPoolStats( USR << "Transacting:"<< endl,
289                   make_filter_begin<resfilter::ByTransact>(pool),
290                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
291
292   if ( 1 ) {
293     bool eres, rres;
294     {
295       //zypp::base::LogControl::TmpLineWriter shutUp;
296       //zypp::base::LogControl::instance().logfile( "SOLVER" );
297       eres = getZYpp()->resolver()->establishPool();
298       rres = getZYpp()->resolver()->resolvePool();
299     }
300     MIL << "est " << eres << " slv " << rres << endl;
301   }
302
303   dumpPoolStats( USR << "Transacting:"<< endl,
304                   make_filter_begin<resfilter::ByTransact>(pool),
305                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
306
307
308   if ( 1 )
309     {
310        ZYppCommitPolicy policy;
311        policy.rpmNoSignature();
312        ZYppCommitResult res( getZYpp()->commit( policy ) );
313     }
314
315   INT << "===[END]============================================" << endl << endl;
316   zypp::base::LogControl::instance().logNothing();
317   return 0;
318 }
319