do not create empty entries
[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     if (isKind<VersionedCap>(cap)
98         && cap.op() != Rel::NONE
99         && cap.op() != Rel::ANY
100         && !cap.edition().version().empty()) {
101         // version capability
102         str << "<dep name='" << cap.index() << "' op='" << xml_escape(cap.op().asString()) <<
103             "' version='" << cap.edition().version() << "'";
104         if (!cap.edition().release().empty())
105             str << " release='" << cap.edition().release() << "'";
106         if (cap.edition().epoch() != Edition::noepoch)
107             str << " epoch='" << numstring(cap.edition().epoch()) << "'";
108         str << " />" << endl;
109     } else {
110         // anything else
111         str << "<dep name='" << cap.asString() << "' />" << endl;       
112     }
113     return str.str();    
114 }
115
116 template<> 
117 std::string helixXML( const CapSet &caps )
118 {
119     stringstream str;
120     CapSet::iterator it = caps.begin();
121     str << endl;
122     for ( ; it != caps.end(); ++it)
123     {
124         str << TAB2 << helixXML((*it));
125     }
126     str << TAB;
127     return str.str();
128 }
129
130 template<> 
131 std::string helixXML( const Dependencies &dep )
132 {
133     stringstream str;
134     if ( dep[Dep::PROVIDES].size() > 0 )
135         str << TAB << xml_tag_enclose(helixXML(dep[Dep::PROVIDES]), "provides") << endl;
136     if ( dep[Dep::CONFLICTS].size() > 0 )
137         str << TAB << xml_tag_enclose(helixXML(dep[Dep::CONFLICTS]), "conflicts") << endl;
138     if ( dep[Dep::OBSOLETES].size() > 0 )
139         str << TAB << xml_tag_enclose(helixXML(dep[Dep::OBSOLETES]), "obsoletes") << endl;
140     if ( dep[Dep::FRESHENS].size() > 0 )
141         str << TAB << xml_tag_enclose(helixXML(dep[Dep::FRESHENS]), "freshens") << endl;
142     if ( dep[Dep::REQUIRES].size() > 0 )
143         str << TAB << xml_tag_enclose(helixXML(dep[Dep::REQUIRES]), "requires") << endl;  
144     if ( dep[Dep::RECOMMENDS].size() > 0 )
145         str << TAB << xml_tag_enclose(helixXML(dep[Dep::RECOMMENDS]), "recommends") << endl;
146     if ( dep[Dep::ENHANCES].size() > 0 )
147         str << TAB << xml_tag_enclose(helixXML(dep[Dep::ENHANCES]), "enhances") << endl;
148     if ( dep[Dep::SUPPLEMENTS].size() > 0 )
149         str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUPPLEMENTS]), "supplements") << endl;
150     if ( dep[Dep::SUGGESTS].size() > 0 )
151         str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUGGESTS]), "suggests") << endl;
152     return str.str();    
153 }
154
155 std::string helixXML( const Resolvable::constPtr &resolvable )
156 {
157   stringstream str;
158   str << "<" << toLower (resolvable->kind().asString()) << ">" << endl;
159   str << TAB << xml_tag_enclose (resolvable->name(), "name") << endl;  
160   if ( isKind<Package>(resolvable) ) {
161       str << TAB << "<history>" << endl << TAB << "<update>" << endl;
162       str << TAB2 << helixXML (resolvable->arch()) << endl;
163       str << TAB2 << helixXML (resolvable->edition()) << endl;      
164       str << TAB << "</update>" << endl << TAB << "</history>" << endl;
165   } else {
166       str << TAB << helixXML (resolvable->arch()) << endl;      
167       str << TAB << helixXML (resolvable->edition()) << endl;            
168   }
169   str << helixXML (resolvable->deps());              
170
171   str << "</" << toLower (resolvable->kind().asString()) << ">" << endl;  
172   return str.str();
173 }
174
175 //---------------------------------------------------------------------------
176
177 Testcase::Testcase()
178     :dumpPath("/var/log/YaST2/solverTestcase")    
179 {
180 }
181
182 Testcase::Testcase(const std::string & path)
183     :dumpPath(path)
184 {
185 }
186         
187
188 Testcase::~Testcase()
189 {
190 }
191
192 bool Testcase::createTestcase(Resolver & resolver)
193 {
194     PathInfo path (dumpPath);
195
196     if ( !path.isExist() ) {
197         if (zypp::filesystem::mkdir (dumpPath)!=0) {
198             ERR << "Cannot create directory " << dumpPath << endl;
199             return false;
200         }
201     } else {
202         if (!path.isDir()) {
203             ERR << dumpPath << " is not a directory." << endl;
204             return false;
205         }
206     }
207     zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
208     zypp::base::LogControl::TmpExcessive excessive; // ZYPP_FULLLOG=1
209     
210     resolver.resolveDependencies();
211
212     ResPool pool        = resolver.pool();
213     SourceTable         sourceTable;
214     PoolItemList        items_to_install;
215     PoolItemList        items_to_remove;    
216
217     HelixResolvable     system (dumpPath + "/solver-system.xml");    
218
219     for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
220     {
221         Resolvable::constPtr res = it->resolvable();
222         
223         if ( it->status().isInstalled() ) {
224             // system channel
225             system.addResolvable (res);
226         } else {
227             // source channels
228             ResObject::constPtr sourceItem = it->resolvable();
229             Source_Ref source  = sourceItem->source();
230             if (sourceTable.find (source) == sourceTable.end()) {
231                 sourceTable[source] = new HelixResolvable(dumpPath + "/"
232                                                           + numstring(source.numericId())
233                                                           + "-package.xml");
234             }
235             sourceTable[source]->addResolvable (res);
236         }
237         
238         if ( it->status().isToBeInstalled()
239              && !(it->status().isBySolver())) {
240             items_to_install.push_back (*it);
241         }
242         if ( it->status().isToBeUninstalled()
243              && !(it->status().isBySolver())) {
244             items_to_remove.push_back (*it);
245         }
246     }
247
248     // writing control file "*-test.xml"
249
250     HelixControl control (dumpPath + "/solver-test.xml",
251                           sourceTable);
252
253     for (PoolItemList::const_iterator iter = items_to_install.begin(); iter != items_to_install.end(); iter++) {
254         control.installResolvable (iter->resolvable()); 
255     }
256
257     for (PoolItemList::const_iterator iter = items_to_remove.begin(); iter != items_to_remove.end(); iter++) {
258         control.deleteResolvable (iter->resolvable());  
259     }
260
261     return true;
262 }
263
264 //---------------------------------------------------------------------------
265
266 HelixResolvable::HelixResolvable(const std::string & path)
267     :dumpFile (path)    
268 {
269     file = new ofstream(path.c_str());
270     if (!file) {
271         ZYPP_THROW (Exception( "Can't open " + path ) );
272     }
273
274     *file << "<channel><subchannel>" << endl;
275 }
276
277 HelixResolvable::~HelixResolvable()
278 {
279     *file << "</subchannel></channel>" << endl;
280 }
281     
282
283 void HelixResolvable::addResolvable(const Resolvable::constPtr &resolvable)
284 {
285     *file << helixXML (resolvable);
286 }
287
288 //---------------------------------------------------------------------------
289
290 HelixControl::HelixControl(const std::string & controlPath,
291                            const SourceTable & sourceTable,
292                            const std::string & systemPath)
293     :dumpFile (controlPath) 
294 {
295     file = new ofstream(controlPath.c_str());
296     if (!file) {
297         ZYPP_THROW (Exception( "Can't open " + controlPath ) );
298     }
299
300     *file << "<?xml version=\"1.0\"?>" << endl
301           << "<!-- testcase generated by YaST -->" << endl
302           << "<test>" << endl
303           << "<setup>" << endl
304           << TAB << "<system file=\"" << systemPath << "\"/>" << endl;
305     for ( SourceTable::const_iterator it = sourceTable.begin();
306           it != sourceTable.end(); ++it ) {
307         Source_Ref source = it->first;
308         *file << TAB << "<channel file=\"" << numstring(source.numericId())
309               << "-package.xml\" name=\"" << numstring(source.numericId())
310               << "\" />" << endl;
311     }
312     *file << "</setup>" << endl
313           << "<trial>" << endl
314           << "<showpool all=\"yes\"/>" << endl
315           << "<establish/>" << endl
316           << "<showpool all=\"true\" prefix=\">!> ESTABLISHED:\"/>" << endl;
317 }
318
319 HelixControl::HelixControl()
320     :dumpFile ("/var/log/YaST2/solverTestcase/solver-test.xml")
321 {
322     HelixControl (dumpFile);
323 }
324
325 HelixControl::~HelixControl()
326 {
327     *file << "</trial>" << endl
328           << "</test>" << endl;
329 }
330
331 void HelixControl::installResolvable(const ResObject::constPtr &resObject)
332 {
333     Source_Ref source  = resObject->source();
334     *file << "<install channel=\"" << numstring(source.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
335           << " name=\"" << resObject->name() << "\"" << "/>" << endl;
336 }
337     
338 void HelixControl::deleteResolvable(const ResObject::constPtr &resObject)
339 {
340     Source_Ref source  = resObject->source();    
341     *file << "<uninstall channel=\"" << numstring(source.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
342           << " name=\"" << resObject->name() << "\"" << "/>" << endl;    
343 }
344
345
346       ///////////////////////////////////////////////////////////////////
347     };// namespace detail
348     /////////////////////////////////////////////////////////////////////
349     /////////////////////////////////////////////////////////////////////
350   };// namespace solver
351   ///////////////////////////////////////////////////////////////////////
352   ///////////////////////////////////////////////////////////////////////
353 };// namespace zypp
354 /////////////////////////////////////////////////////////////////////////