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