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 << xml_tag_enclose(edition.release(), "release")
79 << xml_tag_enclose(numstring(edition.epoch()), "epoch");
84 std::string helixXML( const Arch &arch )
87 str << xml_tag_enclose(arch.asString(), "arch");
92 std::string helixXML( const Capability &cap )
95 if (isKind<VersionedCap>(cap)
96 && cap.op() != Rel::NONE
97 && cap.op() != Rel::ANY
98 && !cap.edition().version().empty()) {
100 str << "<dep name='" << cap.index() << "' op='" << xml_escape(cap.op().asString()) <<
101 "' version='" << cap.edition().version() << "' release='" << cap.edition().release() << "' />" << endl;
104 str << "<dep name='" << cap.asString() << "' />" << endl;
110 std::string helixXML( const CapSet &caps )
113 CapSet::iterator it = caps.begin();
115 for ( ; it != caps.end(); ++it)
117 str << TAB2 << helixXML((*it));
124 std::string helixXML( const Dependencies &dep )
127 if ( dep[Dep::PROVIDES].size() > 0 )
128 str << TAB << xml_tag_enclose(helixXML(dep[Dep::PROVIDES]), "provides") << endl;
129 if ( dep[Dep::CONFLICTS].size() > 0 )
130 str << TAB << xml_tag_enclose(helixXML(dep[Dep::CONFLICTS]), "conflicts") << endl;
131 if ( dep[Dep::OBSOLETES].size() > 0 )
132 str << TAB << xml_tag_enclose(helixXML(dep[Dep::OBSOLETES]), "obsoletes") << endl;
133 if ( dep[Dep::FRESHENS].size() > 0 )
134 str << TAB << xml_tag_enclose(helixXML(dep[Dep::FRESHENS]), "freshens") << endl;
135 if ( dep[Dep::REQUIRES].size() > 0 )
136 str << TAB << xml_tag_enclose(helixXML(dep[Dep::REQUIRES]), "requires") << endl;
137 if ( dep[Dep::RECOMMENDS].size() > 0 )
138 str << TAB << xml_tag_enclose(helixXML(dep[Dep::RECOMMENDS]), "recommends") << endl;
139 if ( dep[Dep::ENHANCES].size() > 0 )
140 str << TAB << xml_tag_enclose(helixXML(dep[Dep::ENHANCES]), "enhances") << endl;
141 if ( dep[Dep::SUPPLEMENTS].size() > 0 )
142 str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUPPLEMENTS]), "supplements") << endl;
143 if ( dep[Dep::SUGGESTS].size() > 0 )
144 str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUGGESTS]), "suggests") << endl;
148 std::string helixXML( const Resolvable::constPtr &resolvable )
151 str << "<" << toLower (resolvable->kind().asString()) << ">" << endl;
152 str << TAB << xml_tag_enclose (resolvable->name(), "name") << endl;
153 if ( isKind<Package>(resolvable) ) {
154 str << TAB << "<history>" << endl << TAB << "<update>" << endl;
155 str << TAB2 << helixXML (resolvable->arch()) << endl;
156 str << TAB2 << helixXML (resolvable->edition()) << endl;
157 str << TAB << "</update>" << endl << TAB << "</history>" << endl;
159 str << TAB << helixXML (resolvable->arch()) << endl;
160 str << TAB << helixXML (resolvable->edition()) << endl;
162 str << helixXML (resolvable->deps());
164 str << "</" << toLower (resolvable->kind().asString()) << ">" << endl;
168 //---------------------------------------------------------------------------
171 :dumpPath("/var/log/YaST2/solverTestcase")
175 Testcase::Testcase(const std::string & path)
181 Testcase::~Testcase()
185 bool Testcase::createTestcase(Resolver & resolver)
187 PathInfo path (dumpPath);
189 if ( !path.isExist() ) {
190 if (zypp::filesystem::mkdir (dumpPath)!=0) {
191 ERR << "Cannot create directory " << dumpPath << endl;
196 ERR << dumpPath << " is not a directory." << endl;
200 zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
201 zypp::base::LogControl::TmpExcessive excessive; // ZYPP_FULLLOG=1
203 resolver.resolveDependencies();
205 ResPool pool = resolver.pool();
206 SourceTable sourceTable;
207 PoolItemList items_to_install;
208 PoolItemList items_to_remove;
210 HelixResolvable system (dumpPath + "/solver-system.xml");
212 for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
214 Resolvable::constPtr res = it->resolvable();
216 if ( it->status().isInstalled() ) {
218 system.addResolvable (res);
221 ResObject::constPtr sourceItem = it->resolvable();
222 Source_Ref source = sourceItem->source();
223 if (sourceTable.find (source) == sourceTable.end()) {
224 sourceTable[source] = new HelixResolvable(dumpPath + "/"
225 + numstring(source.numericId())
228 sourceTable[source]->addResolvable (res);
231 if ( it->status().isToBeInstalled()
232 && !(it->status().isBySolver())) {
233 items_to_install.push_back (*it);
235 if ( it->status().isToBeUninstalled()
236 && !(it->status().isBySolver())) {
237 items_to_remove.push_back (*it);
241 // writing control file "*-test.xml"
243 HelixControl control (dumpPath + "/solver-test.xml",
246 for (PoolItemList::const_iterator iter = items_to_install.begin(); iter != items_to_install.end(); iter++) {
247 control.installResolvable (iter->resolvable());
250 for (PoolItemList::const_iterator iter = items_to_remove.begin(); iter != items_to_remove.end(); iter++) {
251 control.deleteResolvable (iter->resolvable());
257 //---------------------------------------------------------------------------
259 HelixResolvable::HelixResolvable(const std::string & path)
262 file = new ofstream(path.c_str());
264 ZYPP_THROW (Exception( "Can't open " + path ) );
267 *file << "<channel><subchannel>" << endl;
270 HelixResolvable::~HelixResolvable()
272 *file << "</subchannel></channel>" << endl;
276 void HelixResolvable::addResolvable(const Resolvable::constPtr &resolvable)
278 *file << helixXML (resolvable);
281 //---------------------------------------------------------------------------
283 HelixControl::HelixControl(const std::string & controlPath,
284 const SourceTable & sourceTable,
285 const std::string & systemPath)
286 :dumpFile (controlPath)
288 file = new ofstream(controlPath.c_str());
290 ZYPP_THROW (Exception( "Can't open " + controlPath ) );
293 *file << "<?xml version=\"1.0\"?>" << endl
294 << "<!-- testcase generated by YaST -->" << endl
297 << TAB << "<system file=\"" << systemPath << "\"/>" << endl;
298 for ( SourceTable::const_iterator it = sourceTable.begin();
299 it != sourceTable.end(); ++it ) {
300 Source_Ref source = it->first;
301 *file << TAB << "<channel file=\"" << numstring(source.numericId())
302 << "-package.xml\" name=\"" << numstring(source.numericId())
305 *file << "</setup>" << endl
307 << "<showpool all=\"yes\"/>" << endl
308 << "<establish/>" << endl
309 << "<showpool all=\"true\" prefix=\">!> ESTABLISHED:\"/>" << endl;
312 HelixControl::HelixControl()
313 :dumpFile ("/var/log/YaST2/solverTestcase/solver-test.xml")
315 HelixControl (dumpFile);
318 HelixControl::~HelixControl()
320 *file << "</trial>" << endl
321 << "</test>" << endl;
324 void HelixControl::installResolvable(const ResObject::constPtr &resObject)
326 Source_Ref source = resObject->source();
327 *file << "<install channel=\"" << numstring(source.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
328 << " name=\"" << resObject->name() << "\"" << "/>" << endl;
331 void HelixControl::deleteResolvable(const ResObject::constPtr &resObject)
333 Source_Ref source = resObject->source();
334 *file << "<uninstall channel=\"" << numstring(source.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
335 << " name=\"" << resObject->name() << "\"" << "/>" << endl;
339 ///////////////////////////////////////////////////////////////////
340 };// namespace detail
341 /////////////////////////////////////////////////////////////////////
342 /////////////////////////////////////////////////////////////////////
343 };// namespace solver
344 ///////////////////////////////////////////////////////////////////////
345 ///////////////////////////////////////////////////////////////////////
347 /////////////////////////////////////////////////////////////////////////