- Removed deprecated oldstyle commit methods.
[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/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/Language.h"
20 #include "zypp/NameKindProxy.h"
21
22
23 using namespace std;
24 using namespace zypp;
25 using namespace zypp::ui;
26 using namespace zypp::functor;
27
28 ///////////////////////////////////////////////////////////////////
29
30 static const Pathname sysRoot( "/Local/ROOT" );
31
32 ///////////////////////////////////////////////////////////////////
33
34 namespace container
35 {
36   template<class _Tp>
37     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
38     { return cont.find( val ) != cont.end(); }
39 }
40
41 ///////////////////////////////////////////////////////////////////
42
43 template<class _Condition>
44   struct SetTrue
45   {
46     SetTrue( _Condition cond_r )
47     : _cond( cond_r )
48     {}
49
50     template<class _Tp>
51       bool operator()( _Tp t ) const
52       {
53         _cond( t );
54         return true;
55       }
56
57     _Condition _cond;
58   };
59
60 template<class _Condition>
61   inline SetTrue<_Condition> setTrue_c( _Condition cond_r )
62   {
63     return SetTrue<_Condition>( cond_r );
64   }
65
66 template <class _Iterator, class _Filter, class _Function>
67   inline _Function for_each_if( _Iterator begin_r, _Iterator end_r,
68                                 _Filter filter_r,
69                                 _Function fnc_r )
70   {
71     for ( _Iterator it = begin_r; it != end_r; ++it )
72       {
73         if ( filter_r( *it ) )
74           {
75             fnc_r( *it );
76           }
77       }
78     return fnc_r;
79   }
80
81 struct PrintPoolItem
82 {
83   void operator()( const PoolItem & pi ) const
84   {
85     USR << "S" << pi->source().numericId()
86         << "/M" << mediaId(pi)
87         << " " << pi << endl;
88   }
89   unsigned mediaId( const PoolItem & pi ) const
90   {
91     Package::constPtr pkg( asKind<Package>(pi.resolvable()) );
92     if ( pkg )
93       return pkg->mediaId();
94     return 0;
95   }
96 };
97
98 template <class _Iterator>
99   std::ostream & vdumpPoolStats( std::ostream & str,
100                                  _Iterator begin_r, _Iterator end_r )
101   {
102     pool::PoolStats stats;
103     std::for_each( begin_r, end_r,
104
105                    functor::chain( setTrue_c(PrintPoolItem()),
106                                    setTrue_c(functor::functorRef<void,ResObject::constPtr>(stats)) )
107
108                  );
109     return str << stats;
110   }
111
112 struct PoolItemSelect
113 {
114   void operator()( const PoolItem & pi ) const
115   {
116     if ( pi->source().numericId() == 2 )
117       pi.status().setTransact( true, ResStatus::USER );
118   }
119 };
120
121 ///////////////////////////////////////////////////////////////////
122 typedef std::list<PoolItem> PoolItemList;
123 typedef std::set<PoolItem>  PoolItemSet;
124 #include "zypp/solver/detail/InstallOrder.h"
125 using zypp::solver::detail::InstallOrder;
126 #include "Iorder.h"
127
128 ///////////////////////////////////////////////////////////////////
129 namespace zypp
130 {
131   struct CollectTransacting
132   {
133     typedef std::list<PoolItem> PoolItemList;
134
135     void operator()( const PoolItem & pi )
136     {
137       if ( pi.status().isToBeInstalled() )
138         {
139           _toInstall.insert( pi );
140         }
141       else if ( pi.status().isToBeUninstalled() )
142         {
143           if ( pi.status().isToBeUninstalledDueToObsolete()
144                || pi.status().isToBeUninstalledDueToUpgrade() )
145             _skipToDelete.insert( pi );
146           else
147             _toDelete.insert( pi );
148         }
149     }
150
151     PoolItemSet _toInstall;
152     PoolItemSet _toDelete;
153     PoolItemSet _skipToDelete;
154   };
155
156   std::ostream & operator<<( std::ostream & str, const CollectTransacting & obj )
157   {
158     str << "CollectTransacting:" << endl;
159     dumpPoolStats( str << " toInstall: ",
160                    obj._toInstall.begin(), obj._toInstall.end() ) << endl;
161     dumpPoolStats( str << " toDelete: ",
162                    obj._toDelete.begin(), obj._toDelete.end() ) << endl;
163     dumpPoolStats( str << " skipToDelete: ",
164                    obj._skipToDelete.begin(), obj._skipToDelete.end() ) << endl;
165     return str;
166   }
167 }
168
169 ///////////////////////////////////////////////////////////////////
170 #if 0
171 template<class _InstIterator, class _DelIterator, class _OutputIterator>
172 void strip_obsoleted_to_delete( _InstIterator instBegin_r, _InstIterator instEnd_r,
173                                 _DelIterator  delBegin_r,  _DelIterator  delEnd_r,
174                                 _OutputIterator skip_r )
175   {
176     if ( instBegin_r == instEnd_r
177          || delBegin_r == delEnd_r )
178     return; // ---> nothing to do
179
180     // build obsoletes from inst
181     CapSet obsoletes;
182     for ( /**/; instBegin_r != instEnd_r; ++instBegin_r )
183     {
184       //xxxxx
185       //PoolItem_Ref item( *it );
186       //obsoletes.insert( item->dep(Dep::OBSOLETES).begin(), item->dep(Dep::OBSOLETES).end() );
187     }
188   if ( obsoletes.size() == 0 )
189     return; // ---> nothing to do
190
191   // match them... ;(
192   PoolItemList undelayed;
193   // forall applDelete Packages...
194   for ( PoolItemList::iterator it = deleteList_r.begin();
195         it != deleteList_r.end(); ++it )
196     {
197       PoolItem_Ref ipkg( *it );
198       bool delayPkg = false;
199       // ...check whether an obsoletes....
200       for ( CapSet::iterator obs = obsoletes.begin();
201             ! delayPkg && obs != obsoletes.end(); ++obs )
202         {
203           // ...matches anything provided by the package?
204           for ( CapSet::const_iterator prov = ipkg->dep(Dep::PROVIDES).begin();
205                 prov != ipkg->dep(Dep::PROVIDES).end(); ++prov )
206             {
207               if ( obs->matches( *prov ) == CapMatch::yes )
208                 {
209                   // if so, delay package deletion
210                   DBG << "Ignore appl_delete (should be obsoleted): " << ipkg << endl;
211                   delayPkg = true;
212                   ipkg.status().setTransact( false, ResStatus::USER );
213                   break;
214                 }
215             }
216         }
217       if ( ! delayPkg ) {
218         DBG << "undelayed " << ipkg << endl;
219         undelayed.push_back( ipkg );
220       }
221     }
222   // Puhh...
223   deleteList_r.swap( undelayed );
224
225 }
226 #endif
227 ///////////////////////////////////////////////////////////////////
228
229 struct SetTransactValue
230 {
231   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
232   : _newVal( newVal_r )
233   , _causer( causer_r )
234   {}
235
236   ResStatus::TransactValue   _newVal;
237   ResStatus::TransactByValue _causer;
238
239   bool operator()( const PoolItem & pi ) const
240   { return pi.status().setTransactValue( _newVal, _causer ); }
241 };
242
243 struct StatusReset : public SetTransactValue
244 {
245   StatusReset()
246   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
247   {}
248 };
249
250
251 inline bool selectForTransact( const NameKindProxy & nkp )
252 {
253   if ( nkp.availableEmpty() )
254     return false;
255
256   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
257 }
258
259 /******************************************************************
260 **
261 **      FUNCTION NAME : main
262 **      FUNCTION TYPE : int
263 */
264 int main( int argc, char * argv[] )
265 {
266   //zypp::base::LogControl::instance().logfile( "xxx" );
267   INT << "===[START]==========================================" << endl;
268
269   ResPool pool( getZYpp()->pool() );
270
271   getZYpp()->initTarget( sysRoot );
272   getZYpp()->addResolvables( getZYpp()->target()->resolvables(), true );
273   INT << "Added target: " << pool << endl;
274
275   Source_Ref src1( createSource( "dir:/mounts/machcd2/CDs/SLES-10-CD-i386-Beta10/CD1" ) );
276   Source_Ref src2( createSource( "dir:/mounts/machcd2/kukuk/sles10-sp-i386/CD1" ) );
277   getZYpp()->addResolvables( src1.resolvables() );
278   getZYpp()->addResolvables( src2.resolvables() );
279   INT << "Pool: " << pool << endl;
280
281   selectForTransact( nameKindProxy<Pattern>( pool, "default" ) );
282   selectForTransact( nameKindProxy<Pattern>( pool, "x11" ) );
283   selectForTransact( nameKindProxy<Pattern>( pool, "kde" ) );
284   selectForTransact( nameKindProxy<Pattern>( pool, "OOo" ) );
285
286   vdumpPoolStats( INT << "Transacting: ",
287                   make_filter_begin<resfilter::ByTransact>(pool),
288                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
289
290   bool eres, rres;
291   {
292     zypp::base::LogControl::TmpLineWriter shutUp;
293     eres = getZYpp()->resolver()->establishPool();
294     rres = getZYpp()->resolver()->resolvePool();
295   }
296   MIL << "est " << eres << " slv " << rres << endl;
297
298   vdumpPoolStats( INT << "Transacting: ",
299                   make_filter_begin<resfilter::ByTransact>(pool),
300                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
301
302
303   ZYppCommitPolicy policy;
304   policy.rpmNoSignature();
305   if ( 0 )
306     {
307       ZYppCommitResult res( getZYpp()->commit( policy ) );
308     }
309   else
310     {
311       for ( unsigned mnr = 1; mnr < 6; ++mnr )
312         {
313           policy.restrictToMedia( mnr );
314           ZYppCommitResult res( getZYpp()->commit( policy ) );
315         }
316     }
317
318   INT << "===[END]============================================" << endl << endl;
319   return 0;
320 }
321