(no commit message)
[platform/upstream/libzypp.git] / devel / devel.ma / Main.cc
1 #include "Tools.h"
2 #include "FakePool.h"
3
4 #include "zypp/base/Exception.h"
5 #include "zypp/base/InputStream.h"
6 #include "zypp/base/DefaultIntegral.h"
7 #include <zypp/base/Function.h>
8 #include <zypp/base/Iterator.h>
9
10 #include <zypp/SourceManager.h>
11 #include <zypp/SourceFactory.h>
12 #include "zypp/ZYppCallbacks.h"
13
14 #include "zypp/NVRAD.h"
15 #include "zypp/ResFilters.h"
16 #include "zypp/CapFilters.h"
17 #include "zypp/PackageKeyword.h"
18 #include "zypp/pool/GetResolvablesToInsDel.h"
19
20 using namespace std;
21 using namespace zypp;
22
23 ///////////////////////////////////////////////////////////////////
24
25 struct SetTransactValue
26 {
27   SetTransactValue( ResStatus::TransactValue newVal_r, ResStatus::TransactByValue causer_r )
28   : _newVal( newVal_r )
29   , _causer( causer_r )
30   {}
31
32   ResStatus::TransactValue   _newVal;
33   ResStatus::TransactByValue _causer;
34
35   bool operator()( const PoolItem & pi ) const
36   {
37     bool ret = pi.status().setTransactValue( _newVal, _causer );
38     if ( ! ret )
39       ERR << _newVal <<  _causer << " " << pi << endl;
40     return ret;
41   }
42 };
43
44 struct StatusReset : public SetTransactValue
45 {
46   StatusReset()
47   : SetTransactValue( ResStatus::KEEP_STATE, ResStatus::USER )
48   {}
49 };
50
51 struct StatusInstall : public SetTransactValue
52 {
53   StatusInstall()
54   : SetTransactValue( ResStatus::TRANSACT, ResStatus::USER )
55   {}
56 };
57
58 inline bool selectForTransact( const NameKindProxy & nkp, Arch arch = Arch() )
59 {
60   if ( nkp.availableEmpty() )
61   {
62     ERR << "No Item to select: " << nkp << endl;
63     return false;
64     ZYPP_THROW( Exception("No Item to select") );
65   }
66
67   if ( arch != Arch() )
68   {
69     typeof( nkp.availableBegin() ) it =  nkp.availableBegin();
70     for ( ; it != nkp.availableEnd(); ++it )
71     {
72       if ( (*it)->arch() == arch )
73         return (*it).status().setTransact( true, ResStatus::USER );
74     }
75   }
76
77   return nkp.availableBegin()->status().setTransact( true, ResStatus::USER );
78 }
79
80 ///////////////////////////////////////////////////////////////////
81
82 bool solve( bool establish = false )
83 {
84   if ( establish )
85   {
86     bool eres = getZYpp()->resolver()->establishPool();
87     if ( ! eres )
88     {
89       ERR << "establish " << eres << endl;
90       return false;
91     }
92     MIL << "establish " << eres << endl;
93   }
94
95   bool rres = getZYpp()->resolver()->resolvePool();
96   if ( ! rres )
97   {
98     ERR << "resolve " << rres << endl;
99     return false;
100   }
101   MIL << "resolve " << rres << endl;
102   return true;
103 }
104
105 ///////////////////////////////////////////////////////////////////
106
107 /******************************************************************
108 **
109 **      FUNCTION NAME : main
110 **      FUNCTION TYPE : int
111 */
112 int main( int argc, char * argv[] )
113 {
114   INT << "===[START]==========================================" << endl;
115   ///////////////////////////////////////////////////////////////////
116   // define pool
117   ///////////////////////////////////////////////////////////////////
118   if ( 0 )
119   {
120     const char * data[] = {
121       "@ product",
122       "@ installed",
123       "- prodold 1 1 x86_64",
124       "@ available",
125       "- prodnew 1 1 x86_64",
126       "@ obsoletes",
127       "prodold",
128       "@ fin",
129     };
130     debug::addPool( data, data + ( sizeof(data) / sizeof(const char *) ) );
131   }
132   else
133   {
134     debug::addPool( "SRC/iorderbug.pool" );
135   }
136   ResPool pool( getZYpp()->pool() );
137   vdumpPoolStats( USR << "Initial pool:" << endl,
138                   pool.begin(),
139                   pool.end() ) << endl;
140
141   ///////////////////////////////////////////////////////////////////
142   // define transaction
143   ///////////////////////////////////////////////////////////////////
144   if ( 0 )
145     for_each( pool.byKindBegin<Product>(), pool.byKindEnd<Product>(), StatusInstall() );
146
147 #define selt(K,N) selectForTransact( nameKindProxy<K>( pool, #N ) )
148   selt( Package, bash );
149   selt( Package, readline );
150   selt( Package, fontcfg );
151 #undef selt
152
153   ///////////////////////////////////////////////////////////////////
154   // solve
155   ///////////////////////////////////////////////////////////////////
156   if ( 0 )
157   {
158     solve();
159   }
160
161   vdumpPoolStats( USR << "Transacting:"<< endl,
162                   make_filter_begin<resfilter::ByTransact>(pool),
163                   make_filter_end<resfilter::ByTransact>(pool) ) << endl;
164
165   ///////////////////////////////////////////////////////////////////
166   // install order
167   ///////////////////////////////////////////////////////////////////
168   pool::GetResolvablesToInsDel collect( pool, pool::GetResolvablesToInsDel::ORDER_BY_MEDIANR );
169   MIL << "GetResolvablesToInsDel:" << endl << collect << endl;
170
171   if ( 1 )
172   {
173     // Collect until the 1st package from an unwanted media occurs.
174     // Further collection could violate install order.
175     bool hitUnwantedMedia = false;
176     typedef pool::GetResolvablesToInsDel::PoolItemList PoolItemList;
177     PoolItemList::iterator fst=collect._toInstall.end();
178     for ( PoolItemList::iterator it = collect._toInstall.begin(); it != collect._toInstall.end(); ++it)
179     {
180       ResObject::constPtr res( it->resolvable() );
181
182       if ( hitUnwantedMedia
183            || ( res->sourceMediaNr() && res->sourceMediaNr() != 1 ) )
184       {
185         if ( !hitUnwantedMedia )
186           fst=it;
187         hitUnwantedMedia = true;
188       }
189       else
190       {
191       }
192     }
193     dumpRange( WAR << "toInstall1: " << endl,
194                collect._toInstall.begin(), fst ) << endl;
195     dumpRange( WAR << "toInstall2: " << endl,
196                fst, collect._toInstall.end() ) << endl;
197     dumpRange( ERR << "toDelete: " << endl,
198                collect._toDelete.begin(), collect._toDelete.end() ) << endl;
199   }
200   else
201   {
202     dumpRange( WAR << "toInstall: " << endl,
203                collect._toInstall.begin(), collect._toInstall.end() ) << endl;
204     dumpRange( ERR << "toDelete: " << endl,
205                collect._toDelete.begin(), collect._toDelete.end() ) << endl;
206   }
207
208   INT << "===[END]============================================" << endl << endl;
209   zypp::base::LogControl::instance().logNothing();
210   return 0;
211 }
212