0ec1f1430c4d72c0f1bd54fc3487da7c5783fccf
[platform/upstream/libzypp.git] / devel / devel.dmacvicar / ScanSource.cc
1 #include <iostream>
2
3 #include <zypp/base/LogControl.h>
4 #include <zypp/base/LogTools.h>
5 #include <zypp/base/Measure.h>
6 #include <zypp/SourceFactory.h>
7 #include <zypp/Source.h>
8 #include <zypp/Product.h>
9 #include <zypp/ResStore.h>
10 #include <zypp/ResObject.h>
11 #include <zypp/pool/PoolStats.h>
12 #include <zypp/KeyRing.h>
13 #include <zypp/Date.h>
14 #include <zypp/SourceManager.h>
15
16 using namespace std;
17 using namespace zypp;
18
19 static bool verbose = false;
20 static bool debug_flag   = false;
21
22 #define LOG (debug_flag ? USR : cout)
23
24 struct KeyRingReceiver : public callback::ReceiveReport<KeyRingReport>
25 {
26   KeyRingReceiver()
27   {
28     connect();
29   }
30
31   virtual bool askUserToAcceptUnsignedFile( const std::string & file )
32   {
33     LOG << "===[UnsignedFile " << file << "]" << endl;
34     return true;
35   }
36   virtual bool askUserToAcceptUnknownKey( const std::string &file,
37                                           const std::string &id )
38   {
39     LOG << "===[UnknownKey " << id << "]" << endl;
40     return true;
41   }
42   virtual bool askUserToTrustKey( const PublicKey &key)
43   {
44     LOG << "===[TrustKey" << key << "]" << endl;
45     return true;
46   }
47   virtual bool askUserToImportKey( const PublicKey &key)
48   {
49     LOG << "===[ImportKey " << key << "]" << endl;
50     return true;
51   }
52   virtual bool askUserToAcceptVerificationFailed( const std::string &file,
53                                                   const PublicKey &key )
54   {
55     LOG << "===[VerificationFailed " << file << " " << key << "]" << endl;
56     return true;
57   }
58 };
59
60 struct ResStoreStats : public pool::PoolStats
61 {
62   void operator()( const ResObject::constPtr & obj )
63   {
64     if ( isKind<Product>( obj ) )
65       {
66         LOG << obj << endl;
67       }
68     pool::PoolStats::operator()( obj );
69   }
70 };
71
72 /******************************************************************
73 **
74 **      FUNCTION NAME : main
75 **      FUNCTION TYPE : int
76 */
77 int main( int argc, char * argv[] )
78 {
79   //zypp::base::LogControl::instance().logfile( "" );
80   INT << "===[START]==========================================" << endl;
81   --argc;
82   ++argv;
83
84   if ( ! argc )
85     {
86       LOG << "Usage: ScanSource [options] url [[options] url...]" << endl;
87       LOG << "  Display summary of Sources found at 'url'. " << endl;
88       LOG << "  " << endl;
89       LOG << "  " << endl;
90       LOG << "  options:" << endl;
91       LOG << "  +/-l    enable/disable detailed listing of Source content" << endl;
92       LOG << "  +/-d    enable/disable debug output" << endl;
93       return 0;
94     }
95
96   KeyRingReceiver accept;
97
98   for ( ; argc; --argc, ++argv )
99     {
100       if ( *argv == string("+l") )
101         {
102           verbose = true;
103           continue;
104         }
105       if ( *argv == string("-l") )
106         {
107           verbose = false;
108           continue;
109         }
110       if ( *argv == string("+d") )
111         {
112           zypp::base::LogControl::instance().logfile( "-" );
113           debug_flag = true;
114           continue;
115         }
116       if ( *argv == string("-d") )
117         {
118           zypp::base::LogControl::instance().logfile( "" );
119           debug_flag = false;
120           continue;
121         }
122
123       LOG << "====================================================" << endl;
124       LOG << "===Search Source at Url(" << *argv << ")..." << endl;
125       Source_Ref src;
126       try
127         {
128           debug::Measure m( "Create" );
129           Url url(*argv);
130           try
131             {
132               src = SourceFactory().createFrom( url, "/", Date::now().asSeconds() );
133             }
134           catch ( const source::SourceUnknownTypeException & )
135             {
136               src = SourceFactory().createFrom( "Plaindir", url, "/", Date::now().asSeconds(), "", false, true );
137             }
138             m.elapsed();
139             //LOG << m.asString() << endl;
140         }
141       catch ( const Exception & except_r )
142         {
143           LOG << "***Failed: " << except_r << endl;
144           continue;
145         }
146       LOG << "type:           " << src.type() << endl;
147       LOG << "numberOfMedia:  " << src.numberOfMedia() << endl;
148       LOG << "alias:          " << src.alias() << endl;
149       LOG << "vendor:         " << src.vendor() << endl;
150       LOG << "unique_id:      " << src.unique_id() << endl;
151       LOG << "baseSource:     " << src.baseSource() << endl;
152       LOG << "autorefresh:    " << src.autorefresh() << endl;
153       LOG << "publicKeys:     " << src.publicKeys() << endl;
154
155       LOG << "===Parse content..." << endl;
156       try
157       {
158         debug::Measure m( "Parse" );
159         src.resolvables();
160         m.elapsed();
161         //LOG << m.asString() << endl;
162       }
163       catch ( const Exception & except_r )
164       {
165         LOG << "***Failed: " << except_r << endl;
166         continue;
167       }
168       LOG << for_each( src.resolvables().begin(), src.resolvables().end(),
169                        ResStoreStats() ) << endl;
170       if ( verbose )
171       {
172         dumpRange( LOG, src.resolvables().begin(), src.resolvables().end() ) << endl;
173       }
174 #define TestKind Product
175
176       for (ResStore::const_iterator it = src.resolvables().begin(); it != src.resolvables().end(); ++it)
177       {
178         if ( isKind<TestKind>(*it) )
179         {
180           zypp::TestKind::constPtr res = asKind<TestKind>( *it );
181           cout << res->name() << " | " << res->edition() << std::endl;
182           cout << res->distributionName() << " | " << res->distributionEdition() << std::endl;
183         }
184       }
185       
186       //SourceManager::sourceManager()->addSource( src );
187       //SourceManager::sourceManager()->store( "/", true );
188     }
189
190   INT << "===[END]============================================" << endl << endl;
191   return 0;
192 }
193