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/ZConfig.h"
21 #include "zypp/PathInfo.h"
22 #include "zypp/Product.h"
23 #include "zypp/Package.h"
24 #include "zypp/Edition.h"
25 #include "zypp/parser/xml_escape_parser.hpp"
26 #include "zypp/base/String.h"
27 #include "zypp/base/PtrTypes.h"
28 #include "zypp/Capabilities.h"
29 #include "zypp/sat/Solvable.h"
30 #include "zypp/sat/detail/PoolImpl.h"
33 /////////////////////////////////////////////////////////////////////////
35 { ///////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////
38 { /////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////
41 { ///////////////////////////////////////////////////////////////////
47 using namespace zypp::str;
49 IMPL_PTR_TYPE(HelixResolvable);
51 static std::string xml_escape( const std::string &text )
53 iobind::parser::xml_escape_parser parser;
54 return parser.escape(text);
57 static std::string xml_tag_enclose( const std::string &text, const std::string &tag, bool escape = false )
60 result += "<" + tag + ">";
63 result += xml_escape(text);
67 result += "</" + tag + ">";
73 std::string helixXML( const T &obj ); //undefined
76 std::string helixXML( const Edition &edition )
79 str << xml_tag_enclose(edition.version(), "version");
80 if (!edition.release().empty())
81 str << xml_tag_enclose(edition.release(), "release");
82 if (edition.epoch() != Edition::noepoch)
83 str << xml_tag_enclose(numstring(edition.epoch()), "epoch");
88 std::string helixXML( const Arch &arch )
91 str << xml_tag_enclose(arch.asString(), "arch");
96 std::string helixXML( const Capability &cap )
99 CapDetail detail = cap.detail();
100 if (detail.isSimple()) {
101 if (detail.isVersioned()) {
102 str << "<dep name='" << xml_escape(detail.name().asString()) << "'"
103 << " op='" << xml_escape(detail.op().asString()) << "'"
104 << " version='" << xml_escape(detail.ed().version()) << "'";
105 if (!detail.ed().release().empty())
106 str << " release='" << xml_escape(detail.ed().release()) << "'";
107 if (detail.ed().epoch() != Edition::noepoch)
108 str << " epoch='" << xml_escape(numstring(detail.ed().epoch())) << "'";
109 str << " />" << endl;
111 str << "<dep name='" << xml_escape(cap.asString()) << "' />" << endl;
113 } else if (detail.isExpression()) {
114 if (detail.capRel() == CapDetail::CAP_AND
115 && detail.lhs().detail().isNamed()
116 && detail.rhs().detail().isNamed()) {
117 // packageand dependency
118 str << "<dep name='packageand("
119 << IdString(detail.lhs().id()) << ","
120 << IdString(detail.rhs().id()) << ")' />" << endl;
123 IdString packageName;
124 if (detail.capRel() == CapDetail::CAP_AND) {
125 packageName = IdString(detail.lhs().id());
126 detail = detail.rhs().detail();
128 if (detail.capRel() == CapDetail::CAP_NAMESPACE
129 && detail.lhs().id() == NAMESPACE_MODALIAS) {
130 str << "<dep name='modalias(";
131 if (!packageName.empty())
132 str << packageName << ":";
133 str << IdString(detail.rhs().id()) << ")' />" << endl;
135 str << "<!--- ignoring '" << xml_escape(cap.asString()) << "' -->" << endl;
136 MIL << "ignoring " << cap << " cause this format will be supported" << endl;
140 str << "<!--- ignoring '" << xml_escape(cap.asString()) << "' -->" << endl;
141 MIL << "ignoring " << cap << " cause this format will be supported" << endl;
148 std::string helixXML( const Capabilities &caps )
151 Capabilities::const_iterator it = caps.begin();
153 for ( ; it != caps.end(); ++it)
155 str << TAB2 << helixXML((*it));
162 std::string helixXML( const CapabilitySet &caps )
165 CapabilitySet::const_iterator it = caps.begin();
167 for ( ; it != caps.end(); ++it)
169 str << TAB2 << helixXML((*it));
175 inline string helixXML( const Resolvable::constPtr &obj, Dep deptag_r )
178 Capabilities caps( obj->dep(deptag_r) );
179 if ( ! caps.empty() )
180 out << TAB << xml_tag_enclose(helixXML(caps), deptag_r.asString()) << endl;
184 std::string helixXML( const PoolItem &item )
186 const Resolvable::constPtr resolvable = item.resolvable();
188 str << "<" << toLower (resolvable->kind().asString()) << ">" << endl;
189 str << TAB << xml_tag_enclose (resolvable->name(), "name", true) << endl;
190 str << TAB << xml_tag_enclose (item->vendor(), "vendor", true) << endl;
191 str << TAB << xml_tag_enclose (item->buildtime().asSeconds(), "buildtime", true) << endl;
192 if ( isKind<Package>(resolvable) ) {
193 str << TAB << "<history>" << endl << TAB << "<update>" << endl;
194 str << TAB2 << helixXML (resolvable->arch()) << endl;
195 str << TAB2 << helixXML (resolvable->edition()) << endl;
196 str << TAB << "</update>" << endl << TAB << "</history>" << endl;
198 str << TAB << helixXML (resolvable->arch()) << endl;
199 str << TAB << helixXML (resolvable->edition()) << endl;
201 str << helixXML( resolvable, Dep::PROVIDES);
202 str << helixXML( resolvable, Dep::PREREQUIRES);
203 str << helixXML( resolvable, Dep::CONFLICTS);
204 str << helixXML( resolvable, Dep::OBSOLETES);
205 str << helixXML( resolvable, Dep::FRESHENS);
206 str << helixXML( resolvable, Dep::REQUIRES);
207 str << helixXML( resolvable, Dep::RECOMMENDS);
208 str << helixXML( resolvable, Dep::ENHANCES);
209 str << helixXML( resolvable, Dep::SUPPLEMENTS);
210 str << helixXML( resolvable, Dep::SUGGESTS);
212 str << "</" << toLower (resolvable->kind().asString()) << ">" << endl;
216 //---------------------------------------------------------------------------
219 :dumpPath("/var/log/YaST2/solverTestcase")
223 Testcase::Testcase(const std::string & path)
229 Testcase::~Testcase()
234 bool Testcase::createTestcasePool(const ResPool &pool)
236 PathInfo path (dumpPath);
238 if ( !path.isExist() ) {
239 if (zypp::filesystem::mkdir (dumpPath)!=0) {
240 ERR << "Cannot create directory " << dumpPath << endl;
245 ERR << dumpPath << " is not a directory." << endl;
249 zypp::filesystem::clean_dir (dumpPath);
252 RepositoryTable repoTable;
253 HelixResolvable system (dumpPath + "/solver-system.xml.gz");
255 for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
257 if ( it->status().isInstalled() ) {
259 system.addResolvable (*it);
262 Repository repo = it->resolvable()->satSolvable().repository();
263 if (repoTable.find (repo) == repoTable.end()) {
264 repoTable[repo] = new HelixResolvable(dumpPath + "/"
265 + str::numstring((long)repo.id())
266 + "-package.xml.gz");
268 repoTable[repo]->addResolvable (*it);
274 bool Testcase::createTestcase(Resolver & resolver, bool dumpPool, bool runSolver)
276 PathInfo path (dumpPath);
278 if ( !path.isExist() ) {
279 if (zypp::filesystem::mkdir (dumpPath)!=0) {
280 ERR << "Cannot create directory " << dumpPath << endl;
285 ERR << dumpPath << " is not a directory." << endl;
288 // remove old stuff if pool will be dump
290 zypp::filesystem::clean_dir (dumpPath);
294 zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
295 zypp::base::LogControl::TmpExcessive excessive;
297 resolver.reset(true); // true = resetting all valid solverresults
298 resolver.resolvePool();
300 zypp::base::LogControl::instance().logfile( "/var/log/YaST2/y2log" );
303 ResPool pool = resolver.pool();
304 RepositoryTable repoTable;
305 PoolItemList items_to_install;
306 PoolItemList items_to_remove;
307 PoolItemList items_locked;
308 PoolItemList items_keep;
309 HelixResolvable_Ptr system = NULL;
312 system = new HelixResolvable(dumpPath + "/solver-system.xml.gz");
314 for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
316 Resolvable::constPtr res = it->resolvable();
318 if ( system && it->status().isInstalled() ) {
320 system->addResolvable (*it);
323 Repository repo = it->resolvable()->satSolvable().repository();
325 if (repoTable.find (repo) == repoTable.end()) {
326 repoTable[repo] = new HelixResolvable(dumpPath + "/"
327 + str::numstring((long)repo.id())
328 + "-package.xml.gz");
330 repoTable[repo]->addResolvable (*it);
334 if ( it->status().isToBeInstalled()
335 && !(it->status().isBySolver())) {
336 items_to_install.push_back (*it);
338 if ( it->status().isKept()
339 && !(it->status().isBySolver())) {
340 items_keep.push_back (*it);
342 if ( it->status().isToBeUninstalled()
343 && !(it->status().isBySolver())) {
344 items_to_remove.push_back (*it);
346 if ( it->status().isLocked()
347 && !(it->status().isBySolver())) {
348 items_locked.push_back (*it);
352 // writing control file "*-test.xml"
353 HelixControl control (dumpPath + "/solver-test.xml",
355 ZConfig::instance().systemArchitecture(),
356 pool.getRequestedLocales(),
357 "solver-system.xml.gz",
358 resolver.forceResolve());
360 for (PoolItemList::const_iterator iter = items_to_install.begin(); iter != items_to_install.end(); iter++) {
361 control.installResolvable (iter->resolvable(), iter->status());
364 for (PoolItemList::const_iterator iter = items_locked.begin(); iter != items_locked.end(); iter++) {
365 control.lockResolvable (iter->resolvable(), iter->status());
368 for (PoolItemList::const_iterator iter = items_keep.begin(); iter != items_keep.end(); iter++) {
369 control.keepResolvable (iter->resolvable(), iter->status());
372 for (PoolItemList::const_iterator iter = items_to_remove.begin(); iter != items_to_remove.end(); iter++) {
373 control.deleteResolvable (iter->resolvable(), iter->status());
376 control.addDependencies (resolver.extraRequires(), resolver.extraConflicts());
381 //---------------------------------------------------------------------------
383 HelixResolvable::HelixResolvable(const std::string & path)
386 file = new ofgzstream(path.c_str());
388 ZYPP_THROW (Exception( "Can't open " + path ) );
391 *file << "<channel><subchannel>" << endl;
394 HelixResolvable::~HelixResolvable()
396 *file << "</subchannel></channel>" << endl;
401 void HelixResolvable::addResolvable(const PoolItem item)
403 *file << helixXML (item);
406 //---------------------------------------------------------------------------
408 HelixControl::HelixControl(const std::string & controlPath,
409 const RepositoryTable & repoTable,
410 const Arch & systemArchitecture,
411 const LocaleSet &languages,
412 const std::string & systemPath,
413 const bool forceResolve)
414 :dumpFile (controlPath)
416 file = new ofstream(controlPath.c_str());
418 ZYPP_THROW (Exception( "Can't open " + controlPath ) );
421 *file << "<?xml version=\"1.0\"?>" << endl
422 << "<!-- testcase generated by YaST -->" << endl
424 << "<setup arch=\"" << systemArchitecture << "\">" << endl
425 << TAB << "<system file=\"" << systemPath << "\"/>" << endl << endl;
426 for ( RepositoryTable::const_iterator it = repoTable.begin();
427 it != repoTable.end(); ++it ) {
428 RepoInfo repo = it->first.info();
429 *file << TAB << "<!-- " << endl
430 << TAB << "- alias : " << repo.alias() << endl;
431 for ( RepoInfo::urls_const_iterator itUrl = repo.baseUrlsBegin();
432 itUrl != repo.baseUrlsEnd();
435 *file << TAB << "- url : " << *itUrl << endl;
437 *file << TAB << "- path : " << repo.path() << endl;
438 *file << TAB << "- type : " << repo.type() << endl;
439 *file << TAB << " -->" << endl;
441 *file << TAB << "<channel file=\"" << str::numstring((long)it->first.id())
442 << "-package.xml.gz\" name=\"" << repo.alias()
443 << "\" />" << endl << endl;
445 for (LocaleSet::const_iterator iter = languages.begin(); iter != languages.end(); iter++) {
446 *file << TAB << "<locale name=\"" << iter->code()
451 *file << TAB << "<forceResolve/>" << endl;
453 *file << "</setup>" << endl
455 << "<showpool all=\"yes\"/>" << endl;
458 HelixControl::HelixControl()
459 :dumpFile ("/var/log/YaST2/solverTestcase/solver-test.xml")
461 HelixControl (dumpFile);
464 HelixControl::~HelixControl()
466 *file << "</trial>" << endl
467 << "</test>" << endl;
471 void HelixControl::installResolvable(const ResObject::constPtr &resObject,
472 const ResStatus &status)
474 *file << "<install channel=\"" << resObject->repoInfo().alias() << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
475 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
476 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
477 << " status=\"" << status << "\""
481 void HelixControl::lockResolvable(const ResObject::constPtr &resObject,
482 const ResStatus &status)
484 *file << "<lock channel=\"" << resObject->repoInfo().alias() << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
485 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
486 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
487 << " status=\"" << status << "\""
491 void HelixControl::keepResolvable(const ResObject::constPtr &resObject,
492 const ResStatus &status)
494 *file << "<keep channel=\"" << resObject->repoInfo().alias() << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
495 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
496 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
497 << " status=\"" << status << "\""
501 void HelixControl::deleteResolvable(const ResObject::constPtr &resObject,
502 const ResStatus &status)
504 *file << "<uninstall " << " kind=\"" << toLower (resObject->kind().asString()) << "\""
505 << " name=\"" << resObject->name() << "\""
506 << " status=\"" << status << "\""
510 void HelixControl::addDependencies (const CapabilitySet & capRequire, const CapabilitySet & capConflict)
512 for (CapabilitySet::const_iterator iter = capRequire.begin(); iter != capRequire.end(); iter++) {
513 *file << "<addRequire " << " name=\"" << iter->asString() << "\"" << "/>" << endl;
515 for (CapabilitySet::const_iterator iter = capConflict.begin(); iter != capConflict.end(); iter++) {
516 *file << "<addConflict " << " name=\"" << iter->asString() << "\"" << "/>" << endl;
521 ///////////////////////////////////////////////////////////////////
522 };// namespace detail
523 /////////////////////////////////////////////////////////////////////
524 /////////////////////////////////////////////////////////////////////
525 };// namespace solver
526 ///////////////////////////////////////////////////////////////////////
527 ///////////////////////////////////////////////////////////////////////
529 /////////////////////////////////////////////////////////////////////////