1f3ccced66b7318101887102747d45600e43f4c4
[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("disable-system-sources"))
95   {
96     MIL << "system sources disabled" << endl;
97     gSettings.disable_system_sources = true;
98   }
99   else
100   {
101     if ( geteuid() != 0 )
102     {
103       cout << "Sorry, you need root privileges to use system sources, disabling them..." << endl;
104       gSettings.disable_system_sources = true;
105       MIL << "system sources disabled" << endl;
106     }
107     else
108     {
109       MIL << "system sources enabled" << endl;
110     }
111   }
112
113   if (vm.count("add-source"))
114   {
115     string urlStr = vm["add-source"].as< string >();
116     Url url;
117     try {
118       url = Url( urlStr );
119     }
120     catch ( const Exception & excpt_r )
121     {
122       ZYPP_CAUGHT( excpt_r );
123       cerr << "URL is invalid." << endl;
124       cerr << excpt_r.asUserString() << endl;
125       exit( 1 );
126     }
127     
128     try {
129       add_source_by_url(url, "aliasfixme");
130     }
131     catch ( const Exception & excpt_r )
132     {
133       cerr << excpt_r.asUserString() << endl;
134       exit(1);
135     }
136     
137     exit(0);
138   }
139   
140   if (vm.count("disable-system-resolvables"))
141   {
142     MIL << "system resolvables disabled" << endl;
143     cout << "Ignoring installed resolvables..." << endl;
144     gSettings.disable_system_resolvables = true;
145   }
146   
147   if (vm.count("install"))
148   {
149     vector<string> to_install = vm["install"].as< vector<string> >();
150     //MIL << "Additional installs are: " << to_install << "\n";
151     for ( vector<string>::const_iterator it = to_install.begin(); it != to_install.end(); ++it )
152     {
153       gData.packages_to_install = to_install;
154     }
155   }
156   
157   if (vm.count("sources"))
158   {
159     vector<string> sources = vm["sources"].as< vector<string> >();
160     //MIL << "Additional sources are: " << sources << "\n";
161     for ( vector<string>::const_iterator it = sources.begin(); it != sources.end(); ++it )
162     {
163       try
164       {
165         gSettings.additional_sources.push_back(Url(*it)); 
166       }
167       catch ( const Exception &e )
168       {
169         cout << "wrong url " << *it << std::endl;
170         exit(-1);
171       }
172     }
173   }
174   
175   const char *logfile = getenv("ZYPP_LOGFILE");
176   if (logfile != NULL)
177     zypp::base::LogControl::instance().logfile( logfile );
178   else
179     zypp::base::LogControl::instance().logfile( ZYPP_CHECKPATCHES_LOG );
180   
181   std::string previous_token;
182   
183   God = NULL;
184   try
185   {
186     God = zypp::getZYpp();
187   }
188   catch (Exception & excpt_r)
189   {
190     ZYPP_CAUGHT (excpt_r);
191     ERR  << "a ZYpp transaction is already in progress." << endl;
192     cerr << "a ZYpp transaction is already in progress." << endl;
193     cout << RANDOM_TOKEN;
194     return -1;
195   }
196   
197   SourceManager_Ptr manager;
198   manager = SourceManager::sourceManager();
199   
200   KeyRingCallbacks keyring_callbacks;
201   DigestCallbacks digest_callbacks;
202   
203   if ( ! gSettings.disable_system_sources )
204     init_system_sources();
205   
206   for ( std::list<Url>::const_iterator it = gSettings.additional_sources.begin(); it != gSettings.additional_sources.end(); ++it )
207   {
208     include_source_by_url( *it );
209   }
210   
211   cout << endl;
212   
213   if ( gData.sources.empty() )
214   {
215     cout << "Warning! No sources. Operating only over the installed resolvables. You will not be able to install stuff" << endl;
216   } 
217   
218   // dont add rpms
219   God->initializeTarget("/");
220   
221   std::string token = calculate_token();
222   
223   if ( token != gSettings.previous_token )
224   {
225     // something changed
226     load_sources();
227     
228     if ( ! gSettings.disable_system_resolvables )
229       load_target();
230     
231     for ( vector<string>::const_iterator it = gData.packages_to_install.begin(); it != gData.packages_to_install.end(); ++it )
232     {
233       mark_package_for_install(*it);
234     }
235     
236     resolve();
237     
238     show_summary();
239   
240     if ( gData.security_patches_count > 0 )
241       return 2;
242   
243     if ( gData.patches_count > 0 )
244       return 1;
245   }
246   
247   return 0;
248 }
249
250