Fixed zmart -v
[platform/upstream/libzypp.git] / tools / zmart / zmart.cc
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <streambuf>
5 #include <unistd.h>
6
7 #include <boost/program_options.hpp>
8
9 #include "zmart.h"
10 #include "zmart-sources.h"
11 #include "zmart-misc.h"
12
13 #include "zmart-rpm-callbacks.h"
14 #include "zmart-keyring-callbacks.h"
15 #include "zmart-source-callbacks.h"
16
17 using namespace zypp::detail;
18
19 using namespace std;
20 using namespace zypp;
21 using namespace boost;
22
23 namespace po = boost::program_options;
24
25 ZYpp::Ptr God;
26 RuntimeData gData;
27 Settings gSettings;
28
29 RpmCallbacks rpm_callbacks;
30 SourceCallbacks source_callbacks;
31
32 int main(int argc, char **argv)
33 {
34   po::positional_options_description pos_options;
35   pos_options.add("command", -1);
36   
37   po::options_description general_options("General options");
38   general_options.add_options()
39       ("help,h", "produce a help message")
40       ("version,v", "output the version number")
41       ;
42
43   po::options_description operation_options("Operations");
44   operation_options.add_options()
45       ("install,i", po::value< vector<string> >(), "install packages or resolvables")
46       ("list-system-sources,l", "Show available system sources")
47       ("add-source,a", po::value< string >(), "Add a new source from a URL")
48       ;
49   
50   po::options_description source_options("Source options");
51   source_options.add_options()
52       ("disable-system-sources,D", "Don't read the system sources.")
53       ("sources,S", po::value< vector<string> >(), "Read from additional sources")
54       ;
55   
56   po::options_description target_options("Target options");
57   target_options.add_options()
58       ("disable-system-resolvables,T", "Don't read system installed resolvables.")
59       ;
60
61   // Declare an options description instance which will include
62   // all the options
63   po::options_description all_options("Allowed options");
64   all_options.add(general_options).add(source_options).add(operation_options).add(target_options);
65
66   // Declare an options description instance which will be shown
67   // to the user
68   po::options_description visible_options("Allowed options");
69   visible_options.add(general_options).add(source_options).add(operation_options).add(target_options);
70
71   po::variables_map vm;
72   //po::store(po::parse_command_line(argc, argv, visible_options), vm);
73   po::store(po::command_line_parser(argc, argv).options(visible_options).positional(pos_options).run(), vm);
74   po::notify(vm);
75
76   
77   if (vm.count("list-system-sources"))
78   {
79     if ( geteuid() != 0 )
80     {
81       cout << "Sorry, you need root privileges to view system sources." << endl;
82       exit(-1);
83     }
84     
85     list_system_sources();
86     return 1;
87   }
88   
89   if (vm.count("help")) {
90     cout << visible_options << "\n";
91     return 1;
92   }
93
94   if (vm.count("version")) {
95     cout << "zmart 0.1" << endl;
96     return 1;
97   }
98
99   if (vm.count("disable-system-sources"))
100   {
101     MIL << "system sources disabled" << endl;
102     gSettings.disable_system_sources = true;
103   }
104   else
105   {
106     if ( geteuid() != 0 )
107     {
108       cout << "Sorry, you need root privileges to use system sources, disabling them..." << endl;
109       gSettings.disable_system_sources = true;
110       MIL << "system sources disabled" << endl;
111     }
112     else
113     {
114       MIL << "system sources enabled" << endl;
115     }
116   }
117
118   if (vm.count("add-source"))
119   {
120     string urlStr = vm["add-source"].as< string >();
121     Url url;
122     try {
123       url = Url( urlStr );
124     }
125     catch ( const Exception & excpt_r )
126     {
127       ZYPP_CAUGHT( excpt_r );
128       cerr << "URL is invalid." << endl;
129       cerr << excpt_r.asUserString() << endl;
130       exit( 1 );
131     }
132     
133     try {
134       add_source_by_url(url, "aliasfixme");
135     }
136     catch ( const Exception & excpt_r )
137     {
138       cerr << excpt_r.asUserString() << endl;
139       exit(1);
140     }
141     
142     exit(0);
143   }
144   
145   if (vm.count("disable-system-resolvables"))
146   {
147     MIL << "system resolvables disabled" << endl;
148     cout << "Ignoring installed resolvables..." << endl;
149     gSettings.disable_system_resolvables = true;
150   }
151   
152   if (vm.count("install"))
153   {
154     vector<string> to_install = vm["install"].as< vector<string> >();
155     //MIL << "Additional installs are: " << to_install << "\n";
156     for ( vector<string>::const_iterator it = to_install.begin(); it != to_install.end(); ++it )
157     {
158       gData.packages_to_install = to_install;
159     }
160   }
161   
162   if (vm.count("sources"))
163   {
164     vector<string> sources = vm["sources"].as< vector<string> >();
165     //MIL << "Additional sources are: " << sources << "\n";
166     for ( vector<string>::const_iterator it = sources.begin(); it != sources.end(); ++it )
167     {
168       try
169       {
170         gSettings.additional_sources.push_back(Url(*it)); 
171       }
172       catch ( const Exception &e )
173       {
174         cout << "wrong url " << *it << std::endl;
175         exit(-1);
176       }
177     }
178   }
179   
180   const char *logfile = getenv("ZYPP_LOGFILE");
181   if (logfile != NULL)
182     zypp::base::LogControl::instance().logfile( logfile );
183   else
184     zypp::base::LogControl::instance().logfile( ZYPP_CHECKPATCHES_LOG );
185   
186   std::string previous_token;
187   
188   God = NULL;
189   try
190   {
191     God = zypp::getZYpp();
192   }
193   catch (Exception & excpt_r)
194   {
195     ZYPP_CAUGHT (excpt_r);
196     ERR  << "a ZYpp transaction is already in progress." << endl;
197     cerr << "a ZYpp transaction is already in progress." << endl;
198     cout << RANDOM_TOKEN;
199     return -1;
200   }
201   
202   SourceManager_Ptr manager;
203   manager = SourceManager::sourceManager();
204   
205   KeyRingCallbacks keyring_callbacks;
206   DigestCallbacks digest_callbacks;
207   
208   if ( ! gSettings.disable_system_sources )
209     init_system_sources();
210   
211   for ( std::list<Url>::const_iterator it = gSettings.additional_sources.begin(); it != gSettings.additional_sources.end(); ++it )
212   {
213     include_source_by_url( *it );
214   }
215   
216   cout << endl;
217   
218   if ( gData.sources.empty() )
219   {
220     cout << "Warning! No sources. Operating only over the installed resolvables. You will not be able to install stuff" << endl;
221   } 
222   
223   // dont add rpms
224   God->initializeTarget("/");
225   
226   std::string token = calculate_token();
227   
228   if ( token != gSettings.previous_token )
229   {
230     // something changed
231     load_sources();
232     
233     if ( ! gSettings.disable_system_resolvables )
234       load_target();
235     
236     for ( vector<string>::const_iterator it = gData.packages_to_install.begin(); it != gData.packages_to_install.end(); ++it )
237     {
238       mark_package_for_install(*it);
239     }
240     
241     resolve();
242     
243     show_summary();
244   
245     if ( gData.security_patches_count > 0 )
246       return 2;
247   
248     if ( gData.patches_count > 0 )
249       return 1;
250   }
251   
252   return 0;
253 }
254
255