- Susetags:Selections: Allow parsing older .sel file formats. (#159851)
[platform/upstream/libzypp.git] / devel / devel.ma / Parse.cc
1 #include <ctime>
2
3 #include <iostream>
4 #include <list>
5 #include <map>
6 #include <set>
7
8 #include "Measure.h"
9 #include "Printing.h"
10 #include "Tools.h"
11
12 #include <zypp/base/Logger.h>
13 #include <zypp/base/LogControl.h>
14 #include <zypp/base/String.h>
15 #include <zypp/base/Exception.h>
16 #include <zypp/base/PtrTypes.h>
17 #include <zypp/base/Iterator.h>
18 #include <zypp/base/Algorithm.h>
19 #include <zypp/base/Functional.h>
20 #include <zypp/base/ProvideNumericId.h>
21 #include <zypp/base/ProvideNumericId.h>
22
23 #include "zypp/NVRAD.h"
24 #include "zypp/ResPool.h"
25 #include "zypp/ResFilters.h"
26 #include "zypp/CapFilters.h"
27 #include "zypp/Package.h"
28 #include "zypp/Language.h"
29 #include "zypp/NameKindProxy.h"
30
31 #include <zypp/SourceManager.h>
32 #include <zypp/SourceFactory.h>
33 #include <zypp/source/susetags/SuseTagsImpl.h>
34
35 #include "zypp/ZYppFactory.h"
36 #include "zypp/ResPoolProxy.h"
37 #include "zypp/ResPoolProxy.h"
38 #include "zypp/target/rpm/RpmDb.h"
39
40 using namespace std;
41 using namespace zypp;
42 using namespace zypp::ui;
43 using namespace zypp::functor;
44
45 ///////////////////////////////////////////////////////////////////
46
47 static const Pathname sysRoot( "/Local/ROOT" );
48 static const Url      instSrc( "dir:/Local/SLES10" );
49 //static const Url      instSrc( "dir:/Local/FACTORY" );
50
51 ///////////////////////////////////////////////////////////////////
52
53 namespace container
54 {
55   template<class _Tp>
56     bool isIn( const std::set<_Tp> & cont, const typename std::set<_Tp>::value_type & val )
57     { return cont.find( val ) != cont.end(); }
58 }
59
60 ///////////////////////////////////////////////////////////////////
61
62 template<class _Condition>
63   struct SetTrue
64   {
65     SetTrue( _Condition cond_r )
66     : _cond( cond_r )
67     {}
68
69     template<class _Tp>
70       bool operator()( _Tp t ) const
71       {
72         _cond( t );
73         return true;
74       }
75
76     _Condition _cond;
77   };
78
79 template<class _Condition>
80   inline SetTrue<_Condition> setTrue_c( _Condition cond_r )
81   {
82     return SetTrue<_Condition>( cond_r );
83   }
84
85 template <class _Iterator, class _Filter, class _Function>
86   inline _Function for_each_if( _Iterator begin_r, _Iterator end_r,
87                                 _Filter filter_r,
88                                 _Function fnc_r )
89   {
90     for ( _Iterator it = begin_r; it != end_r; ++it )
91       {
92         if ( filter_r( *it ) )
93           {
94             fnc_r( *it );
95           }
96       }
97     return fnc_r;
98   }
99
100 struct PrintPoolItem
101 {
102   void operator()( const PoolItem & pi ) const
103   {
104     USR << "S" << pi->source().numericId()
105         << "/M" << mediaId(pi)
106         << " " << pi << endl;
107   }
108   unsigned mediaId( const PoolItem & pi ) const
109   {
110     Package::constPtr pkg( asKind<Package>(pi.resolvable()) );
111     if ( pkg )
112       return pkg->mediaId();
113     return 0;
114   }
115 };
116
117 template <class _Iterator>
118   std::ostream & vdumpPoolStats( std::ostream & str,
119                                  _Iterator begin_r, _Iterator end_r )
120   {
121     pool::PoolStats stats;
122     std::for_each( begin_r, end_r,
123
124                    functor::chain( setTrue_c(PrintPoolItem()),
125                                    setTrue_c(functor::functorRef<void,ResObject::constPtr>(stats)) )
126
127                  );
128     return str << stats;
129   }
130
131 struct PoolItemSelect
132 {
133   void operator()( const PoolItem & pi ) const
134   {
135     if ( pi->source().numericId() == 2 )
136       pi.status().setTransact( true, ResStatus::USER );
137   }
138 };
139
140 ///////////////////////////////////////////////////////////////////
141 typedef std::list<PoolItem> PoolItemList;
142 typedef std::set<PoolItem>  PoolItemSet;
143 #include "zypp/solver/detail/InstallOrder.h"
144 using zypp::solver::detail::InstallOrder;
145 #include "Iorder.h"
146
147 ///////////////////////////////////////////////////////////////////
148 namespace zypp
149 {
150   struct CollectTransacting
151   {
152     typedef std::list<PoolItem> PoolItemList;
153
154     void operator()( const PoolItem & pi )
155     {
156       if ( pi.status().isToBeInstalled() )
157         {
158           _toInstall.insert( pi );
159         }
160       else if ( pi.status().isToBeUninstalled() )
161         {
162           if ( pi.status().isToBeUninstalledDueToObsolete()
163                || pi.status().isToBeUninstalledDueToUpgrade() )
164             _skipToDelete.insert( pi );
165           else
166             _toDelete.insert( pi );
167         }
168     }
169
170     PoolItemSet _toInstall;
171     PoolItemSet _toDelete;
172     PoolItemSet _skipToDelete;
173   };
174
175   std::ostream & operator<<( std::ostream & str, const CollectTransacting & obj )
176   {
177     str << "CollectTransacting:" << endl;
178     dumpPoolStats( str << " toInstall: ",
179                    obj._toInstall.begin(), obj._toInstall.end() ) << endl;
180     dumpPoolStats( str << " toDelete: ",
181                    obj._toDelete.begin(), obj._toDelete.end() ) << endl;
182     dumpPoolStats( str << " skipToDelete: ",
183                    obj._skipToDelete.begin(), obj._skipToDelete.end() ) << endl;
184     return str;
185   }
186 }
187
188 ///////////////////////////////////////////////////////////////////
189 #if 0
190 template<class _InstIterator, class _DelIterator, class _OutputIterator>
191 void strip_obsoleted_to_delete( _InstIterator instBegin_r, _InstIterator instEnd_r,
192                                 _DelIterator  delBegin_r,  _DelIterator  delEnd_r,
193                                 _OutputIterator skip_r )
194   {
195     if ( instBegin_r == instEnd_r
196          || delBegin_r == delEnd_r )
197     return; // ---> nothing to do
198
199     // build obsoletes from inst
200     CapSet obsoletes;
201     for ( /**/; instBegin_r != instEnd_r; ++instBegin_r )
202     {
203       //xxxxx
204       //PoolItem_Ref item( *it );
205       //obsoletes.insert( item->dep(Dep::OBSOLETES).begin(), item->dep(Dep::OBSOLETES).end() );
206     }
207   if ( obsoletes.size() == 0 )
208     return; // ---> nothing to do
209
210   // match them... ;(
211   PoolItemList undelayed;
212   // forall applDelete Packages...
213   for ( PoolItemList::iterator it = deleteList_r.begin();
214         it != deleteList_r.end(); ++it )
215     {
216       PoolItem_Ref ipkg( *it );
217       bool delayPkg = false;
218       // ...check whether an obsoletes....
219       for ( CapSet::iterator obs = obsoletes.begin();
220             ! delayPkg && obs != obsoletes.end(); ++obs )
221         {
222           // ...matches anything provided by the package?
223           for ( CapSet::const_iterator prov = ipkg->dep(Dep::PROVIDES).begin();
224                 prov != ipkg->dep(Dep::PROVIDES).end(); ++prov )
225             {
226               if ( obs->matches( *prov ) == CapMatch::yes )
227                 {
228                   // if so, delay package deletion
229                   DBG << "Ignore appl_delete (should be obsoleted): " << ipkg << endl;
230                   delayPkg = true;
231                   ipkg.status().setTransact( false, ResStatus::USER );
232                   break;
233                 }
234             }
235         }
236       if ( ! delayPkg ) {
237         DBG << "undelayed " << ipkg << endl;
238         undelayed.push_back( ipkg );
239       }
240     }
241   // Puhh...
242   deleteList_r.swap( undelayed );
243
244 }
245 #endif
246 ///////////////////////////////////////////////////////////////////
247
248 /******************************************************************
249 **
250 **      FUNCTION NAME : main
251 **      FUNCTION TYPE : int
252 */
253 int main( int argc, char * argv[] )
254 {
255   //zypp::base::LogControl::instance().logfile( "xxx" );
256   INT << "===[START]==========================================" << endl;
257   ResPool pool( getZYpp()->pool() );
258
259   if ( 0 )
260     {
261       Measure x( "initTarget " + sysRoot.asString() );
262       getZYpp()->initTarget( sysRoot );
263       getZYpp()->addResolvables( getZYpp()->target()->resolvables(), true );
264       INT << "Added target: " << pool << endl;
265     }
266
267   if ( 0 ) {
268     SourceManager::sourceManager()->restore( sysRoot );
269     if ( SourceManager::sourceManager()->allSources().empty() )
270       {
271         Source_Ref src( createSource( instSrc ) );
272         SourceManager::sourceManager()->addSource( src );
273         SourceManager::sourceManager()->store( sysRoot, true );
274       }
275
276     Source_Ref src( *SourceManager::sourceManager()->Source_begin() );
277     getZYpp()->addResolvables( src.resolvables() );
278     INT << "Added source: " << pool << endl;
279   }
280
281   Source_Ref src2( createSource( "dir:/Local/SUSE-Linux-10.1-Build_830-i386/CD1" ) );
282   return 0;
283   Source_Ref src1( createSource( "dir:/Local/SUSE-Linux-10.1-Build_830-Addon-BiArch/CD1" ) );
284   INT << "Pool: " << pool << endl;
285   getZYpp()->addResolvables( src1.resolvables() );
286   INT << "Added source1: " << pool << endl;
287   getZYpp()->addResolvables( src2.resolvables() );
288   INT << "Added source2: " << pool << endl;
289
290   NameKindProxy s( nameKindProxy<Selection>( pool, "default" ) );
291   MIL << s << endl;
292   if ( ! s.availableEmpty() )
293     {
294       PoolItem def( * s.availableBegin() );
295       def.status().setTransact( true, ResStatus::USER );
296     }
297
298   bool eres, rres;
299   {
300     zypp::base::LogControl::TmpLineWriter shutUp;
301     eres = getZYpp()->resolver()->establishPool();
302     rres = getZYpp()->resolver()->resolvePool();
303   }
304   MIL << "est " << eres << " slv " << rres << endl;
305
306
307   for_each( pool.byKindBegin<Package>(), pool.byKindEnd<Package>(),
308             PoolItemSelect() );
309   INT << "FIN: " << pool << endl;
310   vdumpPoolStats( INT,
311                   make_filter_begin<resfilter::ByTransact>(pool),
312                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
313
314   if ( 1 )
315     {
316       PoolItemList errors_r;
317       PoolItemList remaining_r;
318       PoolItemList srcremaining_r;
319       commit( pool, 0, errors_r, remaining_r, srcremaining_r, false );
320
321       dumpPoolStats( WAR << "remaining_r ", remaining_r.begin(), remaining_r.end() ) << endl;
322       dumpPoolStats( WAR << "srcremaining_r ", srcremaining_r.begin(), srcremaining_r.end() ) << endl;
323     }
324   else
325     {
326       CollectTransacting toTransact;
327       std::for_each( make_filter_begin<resfilter::ByTransact>(pool),
328                      make_filter_end<resfilter::ByTransact>(pool),
329                      functor::functorRef<void,PoolItem>(toTransact) );
330       MIL << toTransact;
331     }
332
333 #if 0
334   Source_Ref src( *SourceManager::sourceManager()->Source_begin() );
335   const std::list<Pathname> srcKeys( src.publicKeys() );
336   MIL << src << endl;
337   DBG << srcKeys << endl;
338
339   target::rpm::RpmDb rpm;
340   rpm.initDatabase( sysRoot );
341   std::set<Edition> rpmKeys( rpm.pubkeys() );
342   MIL << rpm << endl;
343   DBG << rpmKeys << endl;
344
345   ResPool pool( getZYpp()->pool() );
346   getZYpp()->addResolvables( src.resolvables() );
347   SEC << pool << endl;
348
349   rpm.closeDatabase();
350 #endif
351
352   INT << "===[END]============================================" << endl << endl;
353   return 0;
354 }
355