Imported Upstream version 14.48.2
[platform/upstream/libzypp.git] / devel / devel.ma / FakePool.h
1 #if 0
2 #define FakePool_h
3
4 #include <iostream>
5 #include <vector>
6 #include <string>
7
8 #include "zypp/base/LogTools.h"
9 #include "zypp/base/PtrTypes.h"
10 #include "zypp/base/Algorithm.h"
11 #include "zypp/base/Function.h"
12 #include "zypp/base/Functional.h"
13 #include "zypp/base/IOStream.h"
14 #include "zypp/base/String.h"
15
16 #include "zypp/ZYppFactory.h"
17 #include "zypp/ResPool.h"
18 #include "zypp/ResPoolProxy.h"
19 #include "zypp/CapFactory.h"
20
21 #include "zypp/Atom.h"
22 #include "zypp/Package.h"
23 #include "zypp/SrcPackage.h"
24 #include "zypp/Selection.h"
25 #include "zypp/Pattern.h"
26 #include "zypp/Product.h"
27 #include "zypp/Patch.h"
28 #include "zypp/Script.h"
29 #include "zypp/Message.h"
30 #include "zypp/Language.h"
31 #include "zypp/VendorAttr.h"
32
33 ///////////////////////////////////////////////////////////////////
34 namespace zypp
35 { /////////////////////////////////////////////////////////////////
36   ///////////////////////////////////////////////////////////////////
37   namespace debug
38   { /////////////////////////////////////////////////////////////////
39
40     /**
41      * \code
42      * const char * data[] = {
43      * "@ product"
44      * ,"@ installed"
45      * ,"- prodold 1 1 x86_64"
46      * ,"@ available"
47      * ,"- prodnew 1 1 x86_64"
48      * ,"@ obsoletes"
49      * ,"prodold"
50      * ,"@ fin"
51      * };
52      * DataCollect dataCollect;
53      * for_each( data, data + ( sizeof(data) / sizeof(const char *) ),
54      * function<void(const string &)>( ref( dataCollect ) ) );
55      * \endcode
56     */
57     class DataCollect
58     {
59     public:
60       DataCollect( bool verbose_r = true )
61       : _definst( false )
62       , _defkind( ResKind::package )
63       , _defdep( Dep::PROVIDES )
64       , _defdepref( _defkind )
65       , _verbose( verbose_r )
66       {
67         VendorAttr::disableAutoProtect();
68       }
69
70       bool operator()( const std::string & line_r )
71       {
72         parseLine( str::trim( line_r ) );
73         return true;
74       }
75
76       const ResStore & installed() const
77       { return _installed; }
78
79       const ResStore & available() const
80       { return _available; }
81
82       template<class _Iterator>
83         void collect( _Iterator begin_r, _Iterator end_r )
84         {
85           for_each( begin_r, end_r,
86                     function<void(const std::string &)>( ref(*this) ) );
87         }
88
89     private:
90       struct Data
91       {
92         Data( bool inst_r, Resolvable::Kind kind_r, const std::vector<std::string> & words_r )
93         : _inst( inst_r )
94         , _kind( kind_r )
95         , _data( words_r[1], Edition( words_r[2], words_r[3] ), Arch( words_r[4] ) )
96         {}
97
98         bool             _inst;
99         Resolvable::Kind _kind;
100         NVRAD            _data;
101       };
102
103     private:
104       void parseLine( const std::string & line_r )
105       {
106         if ( line_r.empty() || line_r[0] == '#' )
107           return;
108
109         std::vector<std::string> words;
110         str::split( line_r, std::back_inserter( words ) );
111         if ( words.empty() )
112           return;
113
114         if ( words[0] == "@" )
115           {
116             if ( words.size() < 2 )
117               throw line_r;
118             if ( words[1] == "installed" )
119               _definst = true;
120             else if ( words[1] == "available" )
121               _definst = false;
122             else if ( words[1] == "fin" )
123               finalize();
124             else
125               {
126                 try
127                   {
128                     _defdep = Dep( words[1] );
129                     if ( words.size() > 2 )
130                       _defdepref = Resolvable::Kind( words[2] );
131                   }
132                 catch ( ... )
133                   {
134                     _defkind = _defdepref = Resolvable::Kind( words[1] );
135                   }
136                 return;
137               }
138           }
139         else if ( words[0] == "-" )
140           {
141             if ( words.size() == 5 )
142               {
143                 finalize();
144                 _d.reset( new Data( _definst, _defkind, words ) );
145               }
146             else
147               {
148                 throw words;
149               }
150           }
151         else
152           {
153             _d->_data[_defdep].insert( CapFactory().parse( _defdepref, line_r ) );
154           }
155       }
156
157       void finalize()
158       {
159         if ( _d )
160           {
161             ResObject::Ptr p;
162             if ( _d->_kind == ResKind::package )
163               p = make<Package>();
164             else if ( _d->_kind == ResKind::srcpackage )
165               p = make<SrcPackage>();
166             else if ( _d->_kind == ResTraits<Selection>::kind )
167               p = make<Selection>();
168             else if ( _d->_kind == ResKind::pattern )
169               p = make<Pattern>();
170             else if ( _d->_kind == ResKind::product )
171               p = make<Product>();
172             else if ( _d->_kind == ResKind::patch )
173               p = make<Patch>();
174             else if ( _d->_kind == ResTraits<Script>::kind )
175               p = make<Script>();
176             else if ( _d->_kind == ResTraits<Message>::kind )
177               p = make<Message>();
178             else if ( _d->_kind == ResTraits<Language>::kind )
179               p = make<Language>();
180             else if ( _d->_kind == ResTraits<Atom>::kind )
181               p = make<Atom>();
182             else
183               throw _d->_kind;
184
185             if ( _verbose )
186               {
187                 _MIL("FakePool") << (_d->_inst?"i":"a") << " " << p << std::endl;
188                 _DBG("FakePool") << p->deps() << std::endl;
189               }
190
191             (_d->_inst?_installed:_available).insert( p );
192             _d.reset();
193           }
194       }
195
196       template<class _Res>
197         ResObject::Ptr make()
198         {
199           typename detail::ResImplTraits<typename _Res::Impl>::Ptr impl;
200           return zypp::detail::makeResolvableAndImpl( _d->_data, impl );
201         }
202
203     private:
204       bool             _definst;
205       Resolvable::Kind _defkind;
206       Dep              _defdep;
207       Resolvable::Kind _defdepref;
208
209       bool             _verbose;
210
211       shared_ptr<Data> _d;
212
213       ResStore         _installed;
214       ResStore         _available;
215     };
216     ///////////////////////////////////////////////////////////////////
217
218     /** \relates DataCollect Stream output. */
219     inline std::ostream & operator<<( std::ostream & str, const DataCollect & obj )
220     {
221       dumpRange( str << "Installed" << endl,
222                  obj.installed().begin(),
223                  obj.installed().end() ) << endl;
224       dumpRange( str << "Available:" << endl,
225                  obj.available().begin(),
226                  obj.available().end() ) << endl;
227       return str;
228     }
229
230     ///////////////////////////////////////////////////////////////////
231
232     template<class _Iterator>
233         inline void addPool( _Iterator begin_r, _Iterator end_r )
234     {
235       DataCollect dataCollect;
236       dataCollect.collect( begin_r, end_r );
237       getZYpp()->addResolvables( dataCollect.installed(), true );
238       getZYpp()->addResolvables( dataCollect.available() );
239     }
240
241     inline void addPool( const Pathname & file_r )
242     {
243       std::ifstream in( file_r.c_str() );
244       DataCollect dataCollect;
245       function<bool(const std::string &)> fnc( ref(dataCollect) );
246       iostr::forEachLine( in, fnc );
247       getZYpp()->addResolvables( dataCollect.installed(), true );
248       getZYpp()->addResolvables( dataCollect.available() );
249     }
250
251     /////////////////////////////////////////////////////////////////
252   } // namespace debug
253   ///////////////////////////////////////////////////////////////////
254   /////////////////////////////////////////////////////////////////
255 } // namespace zypp
256 ///////////////////////////////////////////////////////////////////
257 #endif // FakePool_h