- Add new product attributes (flavor,referencePackage).
[platform/upstream/libzypp.git] / devel / devel.ma / Tools.h
1 #ifndef Tools_h
2 #define Tools_h
3
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <list>
8 #include <map>
9 #include <set>
10
11 #include "Printing.h"
12 #include "FakePool.h"
13
14 #include <zypp/base/Easy.h>
15 #include <zypp/base/Counter.h>
16 #include <zypp/base/Measure.h>
17
18 #include <zypp/PathInfo.h>
19 #include <zypp/Date.h>
20 #include <zypp/ResObject.h>
21 #include <zypp/pool/PoolStats.h>
22
23 #include "zypp/ZYppFactory.h"
24 #include "zypp/ResPool.h"
25 #include "zypp/ResPoolProxy.h"
26 #include "zypp/ui/Selectable.h"
27 #include <zypp/Repository.h>
28 #include <zypp/RepoManager.h>
29
30 using namespace zypp;
31 using zypp::debug::Measure;
32 using std::endl;
33
34 ///////////////////////////////////////////////////////////////////
35
36 #define for_providers_(IT,CAP) for ( sat::WhatProvides::const_iterator IT = sat::WhatProvides( Capability CAP ).begin(), _for_end = sat::WhatProvides().end(); IT != _for_end; ++IT )
37
38 ///////////////////////////////////////////////////////////////////
39
40 void waitForInput()
41 {
42   int i;
43   USR << "WAITING FOR INPUT!" << endl;
44   std::cin >> i;
45 }
46
47 ///////////////////////////////////////////////////////////////////
48
49 void mksrc( const std::string & url, const std::string & alias, RepoManager & repoManager )
50 {
51   RepoInfo nrepo;
52   nrepo.setAlias( alias );
53   nrepo.setName( alias );
54   nrepo.setEnabled( true );
55   nrepo.setAutorefresh( false );
56   nrepo.addBaseUrl( Url(url) );
57
58   if ( ! repoManager.isCached( nrepo ) )
59   {
60     repoManager.buildCache( nrepo );
61   }
62
63   repoManager.loadFromCache( nrepo );
64 }
65
66 ///////////////////////////////////////////////////////////////////
67 //
68 template<class _Condition>
69   struct SetTrue
70   {
71     SetTrue( _Condition cond_r )
72     : _cond( cond_r )
73     {}
74
75     template<class _Tp>
76       bool operator()( _Tp t ) const
77       {
78         _cond( t );
79         return true;
80       }
81
82     _Condition _cond;
83   };
84
85 template<class _Condition>
86   inline SetTrue<_Condition> setTrue_c( _Condition cond_r )
87   {
88     return SetTrue<_Condition>( cond_r );
89   }
90
91 struct PrintPoolItem
92 {
93   void operator()( const PoolItem & pi ) const
94   { USR << pi << endl; }
95 };
96
97 template <class _Iterator>
98   std::ostream & vdumpPoolStats( std::ostream & str,
99                                  _Iterator begin_r, _Iterator end_r )
100   {
101     pool::PoolStats stats;
102     std::for_each( begin_r, end_r,
103
104                    functor::chain( setTrue_c(PrintPoolItem()),
105                                    setTrue_c(functor::functorRef<void,ResObject::constPtr>(stats)) )
106
107                  );
108     return str << stats;
109   }
110
111 ///////////////////////////////////////////////////////////////////
112 // rstats
113
114 typedef zypp::pool::PoolStats Rstats;
115
116 template<class _Iterator>
117   void rstats( _Iterator begin, _Iterator end )
118   {
119     DBG << __PRETTY_FUNCTION__ << endl;
120     Rstats stats;
121     for_each( begin, end, functor::functorRef<void,ResObject::constPtr>(stats) );
122     MIL << stats << endl;
123   }
124
125 template<class _Container>
126   void rstats( const _Container & c )
127   {
128     rstats( c.begin(), c.end() );
129   }
130
131 ///////////////////////////////////////////////////////////////////
132
133 inline RepoManager makeRepoManager( const Pathname & mgrdir_r )
134 {
135   return RepoManager();
136
137   RepoManagerOptions mgropt;
138   mgropt.repoCachePath    = mgrdir_r/"cache";
139   mgropt.repoRawCachePath = mgrdir_r/"raw_cache";
140   mgropt.knownReposPath   = mgrdir_r/"repos.d/";
141
142   return RepoManager( mgropt );
143 }
144
145 ///////////////////////////////////////////////////////////////////
146
147 template<class _Res>
148 ui::Selectable::Ptr getSel( const std::string & name_r )
149 {
150   ResPoolProxy uipool( getZYpp()->poolProxy() );
151   for_(it, uipool.byKindBegin<_Res>(), uipool.byKindEnd<_Res>() )
152   {
153     if ( (*it)->name() == name_r )
154       return (*it);
155   }
156   return 0;
157 }
158
159 template<class _Res>
160 PoolItem getPi( const std::string & name_r, const Edition & ed_r, const Arch & arch_r )
161 {
162   PoolItem ret;
163   ResPool pool( getZYpp()->pool() );
164   for_(it, pool.byNameBegin(name_r), pool.byNameEnd(name_r) )
165   {
166     if ( !ret && isKind<_Res>( (*it).resolvable() )
167          && ( ed_r == Edition() || ed_r == (*it)->edition() )
168          && ( arch_r == Arch()  || arch_r == (*it)->arch()  ) )
169     {
170       ret = (*it);
171       MIL << "    ->" << *it << endl;
172     }
173     else
174     {
175       DBG << "     ?" << *it << endl;
176     }
177   }
178   return ret;
179 }
180 template<class _Res>
181 PoolItem getPi( const std::string & name_r )
182 {
183   return getPi<_Res>( name_r, Edition(), Arch() );
184 }
185 template<class _Res>
186 PoolItem getPi( const std::string & name_r, const Edition & ed_r )
187 {
188   return getPi<_Res>( name_r, ed_r, Arch() );
189 }
190 template<class _Res>
191 PoolItem getPi( const std::string & name_r, const Arch & arch_r )
192 {
193   return getPi<_Res>( name_r, Edition(), arch_r );
194 }
195
196 ///////////////////////////////////////////////////////////////////
197 #endif // Tools_h