Imported Upstream version 17.23.5
[platform/upstream/libzypp.git] / devel / devel.ma / Basic.cc
1 #include "Tools.h"
2
3 #include <iostream>
4
5 #include <zypp/base/LogControl.h>
6 #include <zypp/base/LogTools.h>
7 #include <zypp/base/String.h>
8 #include <zypp/base/SerialNumber.h>
9 #include <zypp/ExternalProgram.h>
10 #include <zypp/PathInfo.h>
11 #include <zypp/TmpPath.h>
12 #include <zypp/ResPoolProxy.h>
13 #include <zypp/repo/PackageProvider.h>
14 #include <zypp/media/MediaManager.h>
15 #include <zypp/MediaSetAccess.h>
16 #include <zypp/Fetcher.h>
17
18 static const Pathname sysRoot( "/" );
19
20 using namespace zypp;
21 using namespace zypp::ui;
22
23
24 #include <zypp/ZYppCallbacks.h>
25
26     struct DownloadProgressReceive :  public callback::ReceiveReport<media::DownloadProgressReport>
27     {
28       DownloadProgressReceive()
29       { connect(); }
30 #if 0
31         enum Action {
32           ABORT,  // abort and return error
33           RETRY,        // retry
34           IGNORE        // ignore the failure
35         };
36
37         enum Error {
38           NO_ERROR,
39           NOT_FOUND,    // the requested Url was not found
40           IO,           // IO error
41           ACCESS_DENIED, // user authent. failed while accessing restricted file
42           ERROR // other error
43         };
44 #endif
45         virtual void start( const Url & file, Pathname localfile )
46         {
47           USR << "DP +++ " << file  << endl;
48           lp = 0;
49         }
50
51         virtual bool progress(int value, const Url &file, double dbps_avg = -1, double dbps_current = -1)
52         {
53           if ( abs(value-lp) >= 20 || value == 100 && lp != 100  )
54           {
55             USR << "DP " << file << " " << value << "%" << endl;
56             lp = value;
57           }
58           return true;
59
60         }
61
62         virtual Action problem( const Url &file , Error error , const std::string &description )
63         {
64           USR << "DP !!! " << file << " (" << error << ")" << endl;
65           return ABORT;
66
67         }
68
69         virtual void finish( const Url &file , Error error , const std::string &reason )
70         {
71           USR << "DP --- " << file << " (" << error << ")" << endl;
72         }
73
74         int lp;
75     };
76
77     ////////////////////////////////////////////////////////////////////
78     //
79     //////////////////////////////////////////////////////////////////
80
81     struct DownloadResolvableReceive :  public callback::ReceiveReport<repo::DownloadResolvableReport>
82     {
83       DownloadResolvableReceive()
84       { connect(); }
85 #if 0
86       enum Action {
87         ABORT,  // abort and return error
88         RETRY,  // retry
89         IGNORE, // ignore this resolvable but continue
90       };
91
92       enum Error {
93         NO_ERROR,
94         NOT_FOUND,      // the requested Url was not found
95         IO,             // IO error
96         INVALID         // the downloaded file is invalid
97       };
98 #endif
99       virtual void start( Resolvable::constPtr resolvable_ptr, const Url &url )
100       {
101         USR << "+++ " << resolvable_ptr << endl;
102       }
103
104
105       // Dowmload delta rpm:
106       // - path below url reported on start()
107       // - expected download size (0 if unknown)
108       // - download is interruptable
109       // - problems are just informal
110       virtual void startDeltaDownload( const Pathname & /*filename*/, const ByteCount & /*downloadsize*/ )
111       {
112         USR << __PRETTY_FUNCTION__ << endl;
113       }
114
115       virtual bool progressDeltaDownload( int /*value*/ )
116       {
117         USR << __PRETTY_FUNCTION__ << endl;
118         return true;
119       }
120
121       virtual void problemDeltaDownload( const std::string &/*description*/ )
122       {
123         USR << __PRETTY_FUNCTION__ << endl;
124       }
125
126       virtual void finishDeltaDownload()
127       {
128         USR << __PRETTY_FUNCTION__ << endl;
129       }
130
131       // Apply delta rpm:
132       // - local path of downloaded delta
133       // - aplpy is not interruptable
134       // - problems are just informal
135       virtual void startDeltaApply( const Pathname & /*filename*/ )
136       {
137         USR << __PRETTY_FUNCTION__ << endl;
138       }
139
140       virtual void progressDeltaApply( int /*value*/ )
141       {
142         USR << __PRETTY_FUNCTION__ << endl;
143       }
144
145       virtual void problemDeltaApply( const std::string &/*description*/ )
146       {
147         USR << __PRETTY_FUNCTION__ << endl;
148       }
149
150       virtual void finishDeltaApply()
151       {
152         USR << __PRETTY_FUNCTION__ << endl;
153       }
154
155       // return false if the download should be aborted right now
156       virtual bool progress(int value, Resolvable::constPtr resolvable_ptr)
157       {
158         if ( 1 || abs(value-lp) >= 20 || value == 100 && lp != 100  )
159         {
160           USR << resolvable_ptr << " " << value << "%" << endl;
161           lp = value;
162         }
163         return true;
164       }
165
166       virtual Action problem( Resolvable::constPtr resolvable_ptr , Error error , const std::string &/*description*/ )
167       {
168         USR << "!!! " << resolvable_ptr << " (" << error << ")" << endl;
169         return ABORT;
170       }
171
172       virtual void finish(Resolvable::constPtr resolvable_ptr , Error error , const std::string &/*reason*/ )
173       {
174         USR << "--- " << resolvable_ptr << " (" << error << ")" << endl;
175       }
176
177       int lp;
178     };
179
180
181 bool queryInstalledEditionHelper( const std::string & name_r,
182                                   const Edition &     ed_r,
183                                   const Arch &        arch_r )
184 {
185   if ( ed_r == Edition::noedition )
186     return true;
187   if ( name_r == "kernel-default" && ed_r == Edition("2.6.22.5-10") )
188     return true;
189   if ( name_r == "update-test-affects-package-manager" && ed_r == Edition("1.1-6") )
190     return true;
191
192   return false;
193 }
194
195 ManagedFile repoProvidePackage( const PoolItem & pi )
196 {
197   ResPool _pool( getZYpp()->pool() );
198   repo::RepoMediaAccess _access;
199
200   // Redirect PackageProvider queries for installed editions
201   // (in case of patch/delta rpm processing) to rpmDb.
202   repo::PackageProviderPolicy packageProviderPolicy;
203   packageProviderPolicy.queryInstalledCB( queryInstalledEditionHelper );
204
205   Package::constPtr p = asKind<Package>( pi.resolvable() );
206
207   // Build a repository list for repos
208   // contributing to the pool
209   repo::DeltaCandidates deltas;//( repo::makeDeltaCandidates( _pool.knownRepositoriesBegin(), _pool.knownRepositoriesEnd() ) );
210
211   repo::PackageProvider pkgProvider( _access, p, deltas, packageProviderPolicy );
212
213   return pkgProvider.providePackage();
214 }
215
216 /******************************************************************
217 **
218 **      FUNCTION NAME : main
219 **      FUNCTION TYPE : int
220 */
221 int main( int argc, char * argv[] )
222 {
223   --argc;
224   ++argv;
225   zypp::base::LogControl::instance().logToStdErr();
226   INT << "===[START]==========================================" << endl;
227   ::unsetenv( "ZYPP_CONF" );
228   ZConfig::instance();
229
230   DownloadProgressReceive _dpr;
231   DownloadResolvableReceive _drr;
232
233   TestSetup::LoadSystemAt( sysRoot );
234   ///////////////////////////////////////////////////////////////////
235   ResPool   pool( ResPool::instance() );
236   sat::Pool satpool( sat::Pool::instance() );
237   ///////////////////////////////////////////////////////////////////
238   dumpRange( USR, satpool.reposBegin(), satpool.reposEnd() ) << endl;
239   USR << "pool: " << pool << endl;
240
241   if ( 0 ) {
242     PoolItem pi( getPi<Package>( "CDT", "amarok", Edition(), Arch_empty ) );
243     SEC << pi << endl;
244     ManagedFile f( repoProvidePackage( pi ) );
245     SEC << f << endl;
246   }
247   {
248     Url url("cd:///?devices=/dev/sr0");
249     Pathname path(url.getPathName());
250     url.setPathName ("/");
251     MediaSetAccess access(url);
252     Pathname local = access.provideFile(path);
253     SEC << local << endl;
254   }
255   if ( 0 ) {
256     Url url("http://download.opensuse.org/debug/distribution/11.4/repo/oss/content.asc");
257     url.setPathName ("/");
258     MediaSetAccess access(url);
259
260     zypp::Fetcher fch;
261     fch.reset();
262     fch.setOptions(zypp::Fetcher::AutoAddIndexes);
263
264     // path - add "/" to the beginning if it's missing there
265     std::string media_path("/debug/distribution/11.4/repo/oss/content.ascx");
266     zypp::OnMediaLocation mloc(media_path, 1);
267     mloc.setOptional(true);
268
269     zypp::filesystem::TmpDir tmpdir( zypp::filesystem::TmpDir::defaultLocation() );
270     fch.addIndex(mloc);
271     fch.start(tmpdir.path(), access);
272   }
273
274
275
276 //   f.resetDispose();
277 //   ExternalProgram("find /tmp/var") >> DBG;
278 //   DBG << endl;
279
280   INT << "===[END]============================================" << endl;
281   zypp::base::LogControl::instance().logNothing();
282   return 0;
283 }