1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/solver/detail/Testcase.cc
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"
30 /////////////////////////////////////////////////////////////////////////
32 { ///////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////////
36 /////////////////////////////////////////////////////////////////////
38 { ///////////////////////////////////////////////////////////////////
44 using namespace zypp::capability;
45 using namespace zypp::str;
47 IMPL_PTR_TYPE(HelixResolvable);
49 static std::string xml_escape( const std::string &text )
51 iobind::parser::xml_escape_parser parser;
52 return parser.escape(text);
55 static std::string xml_tag_enclose( const std::string &text, const std::string &tag, bool escape = false )
58 result += "<" + tag + ">";
61 result += xml_escape(text);
65 result += "</" + tag + ">";
71 std::string helixXML( const T &obj ); //undefined
74 std::string helixXML( const Edition &edition )
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");
86 std::string helixXML( const Arch &arch )
89 str << xml_tag_enclose(arch.asString(), "arch");
94 std::string helixXML( const Capability &cap )
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='" << xml_escape(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;
111 str << "<dep name='" << xml_escape(cap.asString()) << "' />" << endl;
117 std::string helixXML( const CapSet &caps )
120 CapSet::iterator it = caps.begin();
122 for ( ; it != caps.end(); ++it)
124 str << TAB2 << helixXML((*it));
131 std::string helixXML( const Dependencies &dep )
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;
155 std::string helixXML( const Resolvable::constPtr &resolvable )
158 str << "<" << toLower (resolvable->kind().asString()) << ">" << endl;
159 str << TAB << xml_tag_enclose (resolvable->name(), "name", true) << 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;
166 str << TAB << helixXML (resolvable->arch()) << endl;
167 str << TAB << helixXML (resolvable->edition()) << endl;
169 str << helixXML (resolvable->deps());
171 str << "</" << toLower (resolvable->kind().asString()) << ">" << endl;
175 //---------------------------------------------------------------------------
178 :dumpPath("/var/log/YaST2/solverTestcase")
182 Testcase::Testcase(const std::string & path)
188 Testcase::~Testcase()
192 bool Testcase::createTestcase(Resolver & resolver)
194 PathInfo path (dumpPath);
196 if ( !path.isExist() ) {
197 if (zypp::filesystem::mkdir (dumpPath)!=0) {
198 ERR << "Cannot create directory " << dumpPath << endl;
203 ERR << dumpPath << " is not a directory." << endl;
207 zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
208 zypp::base::LogControl::TmpExcessive excessive; // ZYPP_FULLLOG=1
210 resolver.resolveDependencies();
212 ResPool pool = resolver.pool();
213 SourceTable sourceTable;
214 PoolItemList items_to_install;
215 PoolItemList items_to_remove;
217 HelixResolvable system (dumpPath + "/solver-system.xml");
219 for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
221 Resolvable::constPtr res = it->resolvable();
223 if ( it->status().isInstalled() ) {
225 system.addResolvable (res);
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())
235 sourceTable[source]->addResolvable (res);
238 if ( it->status().isToBeInstalled()
239 && !(it->status().isBySolver())) {
240 items_to_install.push_back (*it);
242 if ( it->status().isToBeUninstalled()
243 && !(it->status().isBySolver())) {
244 items_to_remove.push_back (*it);
248 // writing control file "*-test.xml"
250 HelixControl control (dumpPath + "/solver-test.xml",
253 for (PoolItemList::const_iterator iter = items_to_install.begin(); iter != items_to_install.end(); iter++) {
254 control.installResolvable (iter->resolvable());
257 for (PoolItemList::const_iterator iter = items_to_remove.begin(); iter != items_to_remove.end(); iter++) {
258 control.deleteResolvable (iter->resolvable());
264 //---------------------------------------------------------------------------
266 HelixResolvable::HelixResolvable(const std::string & path)
269 file = new ofstream(path.c_str());
271 ZYPP_THROW (Exception( "Can't open " + path ) );
274 *file << "<channel><subchannel>" << endl;
277 HelixResolvable::~HelixResolvable()
279 *file << "</subchannel></channel>" << endl;
283 void HelixResolvable::addResolvable(const Resolvable::constPtr &resolvable)
285 *file << helixXML (resolvable);
288 //---------------------------------------------------------------------------
290 HelixControl::HelixControl(const std::string & controlPath,
291 const SourceTable & sourceTable,
292 const std::string & systemPath)
293 :dumpFile (controlPath)
295 file = new ofstream(controlPath.c_str());
297 ZYPP_THROW (Exception( "Can't open " + controlPath ) );
300 *file << "<?xml version=\"1.0\"?>" << endl
301 << "<!-- testcase generated by YaST -->" << 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())
312 *file << "</setup>" << endl
314 << "<showpool all=\"yes\"/>" << endl
315 << "<establish/>" << endl
316 << "<showpool all=\"true\" prefix=\">!> ESTABLISHED:\"/>" << endl;
319 HelixControl::HelixControl()
320 :dumpFile ("/var/log/YaST2/solverTestcase/solver-test.xml")
322 HelixControl (dumpFile);
325 HelixControl::~HelixControl()
327 *file << "</trial>" << endl
328 << "</test>" << endl;
331 void HelixControl::installResolvable(const ResObject::constPtr &resObject)
333 Source_Ref source = resObject->source();
334 *file << "<install channel=\"" << numstring(source.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
335 << " name=\"" << resObject->name() << "\"" << "/>" << endl;
338 void HelixControl::deleteResolvable(const ResObject::constPtr &resObject)
340 Source_Ref source = resObject->source();
341 *file << "<uninstall channel=\"" << numstring(source.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
342 << " name=\"" << resObject->name() << "\"" << "/>" << endl;
346 ///////////////////////////////////////////////////////////////////
347 };// namespace detail
348 /////////////////////////////////////////////////////////////////////
349 /////////////////////////////////////////////////////////////////////
350 };// namespace solver
351 ///////////////////////////////////////////////////////////////////////
352 ///////////////////////////////////////////////////////////////////////
354 /////////////////////////////////////////////////////////////////////////