f67a8a42b2bec28e56bf9ec6cd83a31fe2d11fe1
[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 #define INCLUDE_TESTSETUP_WITHOUT_BOOST
15 #include "zypp/../tests/lib/TestSetup.h"
16 #undef  INCLUDE_TESTSETUP_WITHOUT_BOOST
17
18 #include <zypp/base/Easy.h>
19 #include <zypp/base/Counter.h>
20 #include <zypp/base/Measure.h>
21
22 #include <zypp/PathInfo.h>
23 #include <zypp/Date.h>
24 #include <zypp/ResObject.h>
25 #include <zypp/pool/PoolStats.h>
26
27 #include "zypp/ZYppFactory.h"
28 #include "zypp/ResPool.h"
29 #include "zypp/ResPoolProxy.h"
30 #include "zypp/ui/Selectable.h"
31 #include <zypp/Repository.h>
32 #include <zypp/RepoManager.h>
33
34
35 using namespace zypp;
36 using zypp::debug::Measure;
37 using std::endl;
38
39 ///////////////////////////////////////////////////////////////////
40
41 #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 )
42
43 ///////////////////////////////////////////////////////////////////
44
45 template<typename T>
46 void whichType( T )
47 { INT << __PRETTY_FUNCTION__ << endl; }
48 template<typename T>
49 void whichType()
50 { INT << __PRETTY_FUNCTION__ << endl; }
51
52 void waitForInput()
53 {
54   std::string i;
55   USR << "WAITING FOR INPUT!" << endl;
56   std::cin >> i;
57 }
58
59 ///////////////////////////////////////////////////////////////////
60
61 void mksrc( const std::string & url, const std::string & alias, RepoManager & repoManager )
62 {
63   RepoInfo nrepo;
64   nrepo.setAlias( alias );
65   nrepo.setName( alias );
66   nrepo.setEnabled( true );
67   nrepo.setAutorefresh( false );
68   nrepo.addBaseUrl( Url(url) );
69
70   if ( ! repoManager.isCached( nrepo ) )
71   {
72     repoManager.buildCache( nrepo );
73   }
74
75   repoManager.loadFromCache( nrepo );
76 }
77
78 ///////////////////////////////////////////////////////////////////
79 //
80 template<class _Condition>
81   struct SetTrue
82   {
83     SetTrue( _Condition cond_r )
84     : _cond( cond_r )
85     {}
86
87     template<class _Tp>
88       bool operator()( _Tp t ) const
89       {
90         _cond( t );
91         return true;
92       }
93
94     _Condition _cond;
95   };
96
97 template<class _Condition>
98   inline SetTrue<_Condition> setTrue_c( _Condition cond_r )
99   {
100     return SetTrue<_Condition>( cond_r );
101   }
102
103 struct PrintPoolItem
104 {
105   void operator()( const PoolItem & pi ) const
106   { USR << pi << endl; }
107 };
108
109 template <class _Iterator>
110   std::ostream & vdumpPoolStats( std::ostream & str,
111                                  _Iterator begin_r, _Iterator end_r )
112   {
113     pool::PoolStats stats;
114     std::for_each( begin_r, end_r,
115
116                    functor::chain( setTrue_c(PrintPoolItem()),
117                                    setTrue_c(functor::functorRef<void,ResObject::constPtr>(stats)) )
118
119                  );
120     return str << stats;
121   }
122
123 ///////////////////////////////////////////////////////////////////
124 // rstats
125
126 typedef zypp::pool::PoolStats Rstats;
127
128 template<class _Iterator>
129   void rstats( _Iterator begin, _Iterator end )
130   {
131     DBG << __PRETTY_FUNCTION__ << endl;
132     Rstats stats;
133     for_each( begin, end, functor::functorRef<void,ResObject::constPtr>(stats) );
134     MIL << stats << endl;
135   }
136
137 template<class _Container>
138   void rstats( const _Container & c )
139   {
140     rstats( c.begin(), c.end() );
141   }
142
143 ///////////////////////////////////////////////////////////////////
144
145 inline RepoManager makeRepoManager( const Pathname & mgrdir_r )
146 {
147   // set via zypp.conf
148   return RepoManager();
149 }
150
151 ///////////////////////////////////////////////////////////////////
152
153 template<class _Res>
154 ui::Selectable::Ptr getSel( const std::string & name_r )
155 {
156   ResPoolProxy uipool( getZYpp()->poolProxy() );
157   for_(it, uipool.byKindBegin<_Res>(), uipool.byKindEnd<_Res>() )
158   {
159     if ( (*it)->name() == name_r )
160       return (*it);
161   }
162   return 0;
163 }
164
165
166
167 template<class _Res>
168 PoolItem getPi( const std::string & alias_r, const std::string & name_r, const Edition & ed_r, const Arch & arch_r )
169 {
170   PoolItem ret;
171   ResPool pool( getZYpp()->pool() );
172   for_(it, pool.byIdentBegin<_Res>(name_r), pool.byIdentEnd<_Res>(name_r) )
173   {
174     if (    ( ed_r.empty()    || ed_r.match((*it)->edition()) == 0 )
175          && ( arch_r.empty()  || arch_r == (*it)->arch()  )
176          && ( alias_r.empty() || alias_r == (*it)->repository().alias() ) )
177     {
178       if ( !ret || ret->repository().alias() == sat::Pool::systemRepoAlias() )
179       {
180         ret = (*it);
181         MIL << "    ->" << *it << endl;
182       }
183       else
184       {
185         DBG << "    - " << *it << endl;
186       }
187     }
188     else
189     {
190       DBG << "     ?" << *it << endl;
191     }
192   }
193   return ret;
194 }
195 template<class _Res>
196 PoolItem getPi( const std::string & name_r, const Edition & ed_r, const Arch & arch_r )
197 {
198   return getPi<_Res>( "", name_r, ed_r, arch_r );
199 }
200 template<class _Res>
201 PoolItem getPi( const std::string & name_r )
202 {
203   return getPi<_Res>( name_r, Edition(), Arch_empty );
204 }
205 template<class _Res>
206 PoolItem getPi( const std::string & name_r, const Edition & ed_r )
207 {
208   return getPi<_Res>( name_r, ed_r, Arch_empty );
209 }
210 template<class _Res>
211 PoolItem getPi( const std::string & name_r, const Arch & arch_r )
212 {
213   return getPi<_Res>( name_r, Edition(), arch_r );
214 }
215
216 ///////////////////////////////////////////////////////////////////
217 #endif // Tools_h