logging additional dependencies; corrected language and system dependencies
[platform/upstream/libzypp.git] / zypp / solver / detail / Testcase.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/solver/detail/Testcase.cc
10  *
11 */
12 #include <iostream>
13 #include <fstream>
14 #include <sstream>
15 #include <streambuf>
16
17 #include "zypp/solver/detail/Testcase.h"
18 #include "zypp/base/Logger.h"
19 #include "zypp/base/LogControl.h"
20 #include "zypp/PathInfo.h"
21 #include "zypp/Product.h"
22 #include "zypp/Package.h"
23 #include "zypp/Edition.h"
24 #include "zypp/target/store/xml_escape_parser.hpp"
25 #include "zypp/capability/VersionedCap.h"
26 #include "zypp/base/String.h"
27 #include "zypp/base/PtrTypes.h"
28
29
30 /////////////////////////////////////////////////////////////////////////
31 namespace zypp
32 { ///////////////////////////////////////////////////////////////////////
33   ///////////////////////////////////////////////////////////////////////
34   namespace solver
35   { /////////////////////////////////////////////////////////////////////
36     /////////////////////////////////////////////////////////////////////
37     namespace detail
38     { ///////////////////////////////////////////////////////////////////
39         
40 #define TAB "\t"
41 #define TAB2 "\t\t"
42         
43 using namespace std;
44 using namespace zypp::capability;
45 using namespace zypp::str;
46
47 IMPL_PTR_TYPE(HelixResolvable); 
48
49 static std::string xml_escape( const std::string &text )
50 {
51   iobind::parser::xml_escape_parser parser;
52   return parser.escape(text);
53 }
54
55 static std::string xml_tag_enclose( const std::string &text, const std::string &tag, bool escape = false )
56 {
57   string result;
58   result += "<" + tag + ">";
59
60   if ( escape)
61    result += xml_escape(text);
62   else
63    result += text;
64
65   result += "</" + tag + ">";
66   return result;
67 }
68
69         
70 template<class T>
71 std::string helixXML( const T &obj ); //undefined
72
73 template<> 
74 std::string helixXML( const Edition &edition )
75 {
76     stringstream str;
77     str << xml_tag_enclose(edition.version(), "version");
78     if (!edition.release().empty())
79         str << xml_tag_enclose(edition.release(), "release");
80     if (edition.epoch() != Edition::noepoch)
81         str << xml_tag_enclose(numstring(edition.epoch()), "epoch");    
82     return str.str();
83 }
84
85 template<> 
86 std::string helixXML( const Arch &arch )
87 {
88     stringstream str;
89     str << xml_tag_enclose(arch.asString(), "arch");        
90     return str.str();    
91 }
92
93 template<> 
94 std::string helixXML( const Capability &cap )
95 {
96     stringstream str;
97     VersionedCap::constPtr vercap = asKind<VersionedCap>(cap);
98     if (vercap
99         && vercap->op() != Rel::NONE
100         && vercap->op() != Rel::ANY
101         && !vercap->edition().version().empty() )
102     {
103         // version capability
104         str << "<dep name='" << xml_escape(vercap->index()) << "' op='" << xml_escape(vercap->op().asString()) <<
105             "' version='" << vercap->edition().version() << "'";
106         if (!vercap->edition().release().empty())
107             str << " release='" << vercap->edition().release() << "'";
108         if (vercap->edition().epoch() != Edition::noepoch)
109             str << " epoch='" << numstring(vercap->edition().epoch()) << "'";
110     }
111     else
112     {
113       str << "<dep name='" << xml_escape(cap.asString()) << "'";
114     }
115     
116     str << " kind=\"" << toLower (cap.refers().asString()) << "\""
117         << " />" << endl;
118         
119     return str.str();    
120 }
121
122 template<> 
123 std::string helixXML( const CapSet &caps )
124 {
125     stringstream str;
126     CapSet::iterator it = caps.begin();
127     str << endl;
128     for ( ; it != caps.end(); ++it)
129     {
130         str << TAB2 << helixXML((*it));
131     }
132     str << TAB;
133     return str.str();
134 }
135
136 template<> 
137 std::string helixXML( const Dependencies &dep )
138 {
139     stringstream str;
140     if ( dep[Dep::PROVIDES].size() > 0 )
141         str << TAB << xml_tag_enclose(helixXML(dep[Dep::PROVIDES]), "provides") << endl;
142     if ( dep[Dep::CONFLICTS].size() > 0 )
143         str << TAB << xml_tag_enclose(helixXML(dep[Dep::CONFLICTS]), "conflicts") << endl;
144     if ( dep[Dep::OBSOLETES].size() > 0 )
145         str << TAB << xml_tag_enclose(helixXML(dep[Dep::OBSOLETES]), "obsoletes") << endl;
146     if ( dep[Dep::FRESHENS].size() > 0 )
147         str << TAB << xml_tag_enclose(helixXML(dep[Dep::FRESHENS]), "freshens") << endl;
148     if ( dep[Dep::REQUIRES].size() > 0 )
149         str << TAB << xml_tag_enclose(helixXML(dep[Dep::REQUIRES]), "requires") << endl;  
150     if ( dep[Dep::RECOMMENDS].size() > 0 )
151         str << TAB << xml_tag_enclose(helixXML(dep[Dep::RECOMMENDS]), "recommends") << endl;
152     if ( dep[Dep::ENHANCES].size() > 0 )
153         str << TAB << xml_tag_enclose(helixXML(dep[Dep::ENHANCES]), "enhances") << endl;
154     if ( dep[Dep::SUPPLEMENTS].size() > 0 )
155         str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUPPLEMENTS]), "supplements") << endl;
156     if ( dep[Dep::SUGGESTS].size() > 0 )
157         str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUGGESTS]), "suggests") << endl;
158     return str.str();    
159 }
160
161 std::string helixXML( const Resolvable::constPtr &resolvable )
162 {
163   stringstream str;
164   if ( isKind<SystemResObject>(resolvable)
165        || isKind<Language>(resolvable) ) {
166       // system resolvable will be generated by the resolver
167       // language dependencies will be written i another part
168       return str.str();
169   }
170   
171   str << "<" << toLower (resolvable->kind().asString()) << ">" << endl;
172   str << TAB << xml_tag_enclose (resolvable->name(), "name", true) << endl;  
173   if ( isKind<Package>(resolvable) ) {
174       str << TAB << "<history>" << endl << TAB << "<update>" << endl;
175       str << TAB2 << helixXML (resolvable->arch()) << endl;
176       str << TAB2 << helixXML (resolvable->edition()) << endl;      
177       str << TAB << "</update>" << endl << TAB << "</history>" << endl;
178   } else {
179       str << TAB << helixXML (resolvable->arch()) << endl;      
180       str << TAB << helixXML (resolvable->edition()) << endl;            
181   }
182   str << helixXML (resolvable->deps());              
183
184   str << "</" << toLower (resolvable->kind().asString()) << ">" << endl;  
185   return str.str();
186 }
187
188 //---------------------------------------------------------------------------
189
190 Testcase::Testcase()
191     :dumpPath("/var/log/YaST2/solverTestcase")    
192 {
193 }
194
195 Testcase::Testcase(const std::string & path)
196     :dumpPath(path)
197 {
198 }
199         
200
201 Testcase::~Testcase()
202 {
203 }
204
205 bool Testcase::createTestcase(Resolver & resolver)
206 {
207     PathInfo path (dumpPath);
208
209     if ( !path.isExist() ) {
210         if (zypp::filesystem::mkdir (dumpPath)!=0) {
211             ERR << "Cannot create directory " << dumpPath << endl;
212             return false;
213         }
214     } else {
215         if (!path.isDir()) {
216             ERR << dumpPath << " is not a directory." << endl;
217             return false;
218         }
219         // remove old stuff
220         zypp::filesystem::clean_dir (dumpPath);
221     }
222     
223     zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
224     zypp::base::LogControl::TmpExcessive excessive;
225
226     resolver.reset(true); // true = resetting all valid solverresults
227     resolver.resolvePool();
228
229     zypp::base::LogControl::instance().logfile( "/var/log/YaST2/y2log" );    
230
231     ResPool pool        = resolver.pool();
232     RepositoryTable             repoTable;
233     PoolItemList        items_to_install;
234     PoolItemList        items_to_remove;
235     PoolItemList        language;
236     HelixResolvable     system (dumpPath + "/solver-system.xml");    
237
238     for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
239     {
240         Resolvable::constPtr res = it->resolvable();
241
242         if (isKind<Language>(res)) {
243             if ( it->status().isInstalled()
244                  || it->status().isToBeInstalled()) {
245                 language.push_back (*it);               
246             }
247         } else {
248             if ( it->status().isInstalled() ) {
249                 // system channel
250                 system.addResolvable (res);
251             } else {
252                 // repo channels
253                 ResObject::constPtr repoItem = it->resolvable();
254                 Repository repo  = repoItem->repository();
255                 if (repoTable.find (repo) == repoTable.end()) {
256                     repoTable[repo] = new HelixResolvable(dumpPath + "/"
257                                                           + numstring(repo.numericId())
258                                                           + "-package.xml");
259                 }
260                 repoTable[repo]->addResolvable (res);
261             }
262         
263             if ( it->status().isToBeInstalled()
264                  && !(it->status().isBySolver())) {
265                 items_to_install.push_back (*it);
266             }
267             if ( it->status().isToBeUninstalled()
268                  && !(it->status().isBySolver())) {
269                 items_to_remove.push_back (*it);
270             }
271         }
272     }
273
274     // writing control file "*-test.xml"
275
276     HelixControl control (dumpPath + "/solver-test.xml",
277                           repoTable,
278                           resolver.architecture(),
279                           language);
280
281     for (PoolItemList::const_iterator iter = items_to_install.begin(); iter != items_to_install.end(); iter++) {
282         control.installResolvable (iter->resolvable()); 
283     }
284
285     for (PoolItemList::const_iterator iter = items_to_remove.begin(); iter != items_to_remove.end(); iter++) {
286         control.deleteResolvable (iter->resolvable());  
287     }
288
289     control.addDependencies (resolver.extraCapability(), resolver.extraConflicts());
290
291     return true;
292 }
293
294 //---------------------------------------------------------------------------
295
296 HelixResolvable::HelixResolvable(const std::string & path)
297     :dumpFile (path)    
298 {
299     file = new ofstream(path.c_str());
300     if (!file) {
301         ZYPP_THROW (Exception( "Can't open " + path ) );
302     }
303
304     *file << "<channel><subchannel>" << endl;
305 }
306
307 HelixResolvable::~HelixResolvable()
308 {
309     *file << "</subchannel></channel>" << endl;
310 }
311     
312
313 void HelixResolvable::addResolvable(const Resolvable::constPtr &resolvable)
314 {
315     *file << helixXML (resolvable);
316 }
317
318 //---------------------------------------------------------------------------
319
320 HelixControl::HelixControl(const std::string & controlPath,
321                            const RepositoryTable & repoTable,
322                            const Arch & systemArchitecture,
323                            const PoolItemList &languages,
324                            const std::string & systemPath)
325     :dumpFile (controlPath) 
326 {
327     file = new ofstream(controlPath.c_str());
328     if (!file) {
329         ZYPP_THROW (Exception( "Can't open " + controlPath ) );
330     }
331
332     *file << "<?xml version=\"1.0\"?>" << endl
333           << "<!-- testcase generated by YaST -->" << endl
334           << "<test>" << endl
335           << "<setup arch=\"" << systemArchitecture << "\">" << endl
336           << TAB << "<system file=\"" << systemPath << "\"/>" << endl;
337     for ( RepositoryTable::const_iterator it = repoTable.begin();
338           it != repoTable.end(); ++it ) {
339         Repository repo = it->first;
340         *file << TAB << "<channel file=\"" << numstring(repo.numericId())
341               << "-package.xml\" name=\"" << numstring(repo.numericId())
342               << "\" />" << endl;
343     }
344     for (PoolItemList::const_iterator iter = languages.begin(); iter != languages.end(); iter++) {
345         *file << TAB << "<locale name=\"" <<  iter->resolvable()->name()
346               << "\" />" << endl;
347     }    
348     *file << "</setup>" << endl
349           << "<trial>" << endl
350           << "<showpool all=\"yes\"/>" << endl
351           << "<establish/>" << endl
352           << "<showpool all=\"true\" prefix=\">!> ESTABLISHED:\"/>" << endl;
353 }
354
355 HelixControl::HelixControl()
356     :dumpFile ("/var/log/YaST2/solverTestcase/solver-test.xml")
357 {
358     HelixControl (dumpFile);
359 }
360
361 HelixControl::~HelixControl()
362 {
363     *file << "</trial>" << endl
364           << "</test>" << endl;
365 }
366
367 void HelixControl::installResolvable(const ResObject::constPtr &resObject)
368 {
369     Repository repo  = resObject->repository();
370     *file << "<install channel=\"" << numstring(repo.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
371           << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
372           << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\"" 
373           << " edition=\"" << resObject->edition().asString() << "\"" << "/>" << endl;
374 }
375     
376 void HelixControl::deleteResolvable(const ResObject::constPtr &resObject)
377 {
378     Repository repo  = resObject->repository();    
379     *file << "<uninstall " << " kind=\"" << toLower (resObject->kind().asString()) << "\""
380           << " name=\"" << resObject->name() << "\"" << "/>" << endl;    
381 }
382
383 void HelixControl::addDependencies (const CapSet & capRequire, const CapSet & capConflict)
384 {
385     for (CapSet::const_iterator iter = capRequire.begin(); iter != capRequire.end(); iter++) {
386         *file << "<addRequire " << " kind=\"" << toLower (iter->kind().asString()) << "\""
387           << " name=\"" << iter->asString() << "\"" << "/>" << endl;    
388     }
389     for (CapSet::const_iterator iter = capConflict.begin(); iter != capConflict.end(); iter++) {
390         *file << "<addConflict " << " kind=\"" << toLower (iter->kind().asString()) << "\""
391           << " name=\"" << iter->asString() << "\"" << "/>" << endl;    
392     }    
393 }
394
395
396       ///////////////////////////////////////////////////////////////////
397     };// namespace detail
398     /////////////////////////////////////////////////////////////////////
399     /////////////////////////////////////////////////////////////////////
400   };// namespace solver
401   ///////////////////////////////////////////////////////////////////////
402   ///////////////////////////////////////////////////////////////////////
403 };// namespace zypp
404 /////////////////////////////////////////////////////////////////////////