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/base/String.h"
26 #include "zypp/base/PtrTypes.h"
27 #include "zypp/Capabilities.h"
30 /////////////////////////////////////////////////////////////////////////
32 { ///////////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////
36 Arch defaultArchitecture();
37 /////////////////////////////////////////////////////////////////
38 } // namespace zypp_detail
39 ///////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////
42 { /////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////
45 { ///////////////////////////////////////////////////////////////////
51 using namespace zypp::str;
53 IMPL_PTR_TYPE(HelixResolvable);
55 static std::string xml_escape( const std::string &text )
57 iobind::parser::xml_escape_parser parser;
58 return parser.escape(text);
61 static std::string xml_tag_enclose( const std::string &text, const std::string &tag, bool escape = false )
64 result += "<" + tag + ">";
67 result += xml_escape(text);
71 result += "</" + tag + ">";
77 std::string helixXML( const T &obj ); //undefined
80 std::string helixXML( const Edition &edition )
83 str << xml_tag_enclose(edition.version(), "version");
84 if (!edition.release().empty())
85 str << xml_tag_enclose(edition.release(), "release");
86 if (edition.epoch() != Edition::noepoch)
87 str << xml_tag_enclose(numstring(edition.epoch()), "epoch");
92 std::string helixXML( const Arch &arch )
95 str << xml_tag_enclose(arch.asString(), "arch");
100 std::string helixXML( const Capability &cap )
103 str << "<dep name='" << xml_escape(cap.asString()) << "' />" << endl;
109 std::string helixXML( const Capabilities &caps )
112 Capabilities::const_iterator it = caps.begin();
114 for ( ; it != caps.end(); ++it)
116 str << TAB2 << helixXML((*it));
123 std::string helixXML( const CapabilitySet &caps )
126 CapabilitySet::const_iterator it = caps.begin();
128 for ( ; it != caps.end(); ++it)
130 str << TAB2 << helixXML((*it));
138 std::string helixXML( const Dependencies &dep )
141 if ( dep[Dep::PROVIDES].size() > 0 )
142 str << TAB << xml_tag_enclose(helixXML(dep[Dep::PROVIDES]), "provides") << endl;
143 if ( dep[Dep::CONFLICTS].size() > 0 )
144 str << TAB << xml_tag_enclose(helixXML(dep[Dep::CONFLICTS]), "conflicts") << endl;
145 if ( dep[Dep::OBSOLETES].size() > 0 )
146 str << TAB << xml_tag_enclose(helixXML(dep[Dep::OBSOLETES]), "obsoletes") << endl;
147 if ( dep[Dep::FRESHENS].size() > 0 )
148 str << TAB << xml_tag_enclose(helixXML(dep[Dep::FRESHENS]), "freshens") << endl;
149 if ( dep[Dep::REQUIRES].size() > 0 )
150 str << TAB << xml_tag_enclose(helixXML(dep[Dep::REQUIRES]), "requires") << endl;
151 if ( dep[Dep::RECOMMENDS].size() > 0 )
152 str << TAB << xml_tag_enclose(helixXML(dep[Dep::RECOMMENDS]), "recommends") << endl;
153 if ( dep[Dep::ENHANCES].size() > 0 )
154 str << TAB << xml_tag_enclose(helixXML(dep[Dep::ENHANCES]), "enhances") << endl;
155 if ( dep[Dep::SUPPLEMENTS].size() > 0 )
156 str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUPPLEMENTS]), "supplements") << endl;
157 if ( dep[Dep::SUGGESTS].size() > 0 )
158 str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUGGESTS]), "suggests") << endl;
162 inline string helixXML( const Resolvable::constPtr &obj, Dep deptag_r )
165 Capabilities caps( obj->dep(deptag_r) );
166 if ( ! caps.empty() )
167 out << " " << xml_tag_enclose(helixXML(caps), deptag_r.asString()) << endl;
171 std::string helixXML( const PoolItem_Ref &item )
173 const Resolvable::constPtr resolvable = item.resolvable();
175 if ( isKind<Language>(resolvable) ) {
176 // language dependencies will be written in another part
180 str << "<" << toLower (resolvable->kind().asString()) << ">" << endl;
181 str << TAB << xml_tag_enclose (resolvable->name(), "name", true) << endl;
182 str << TAB << xml_tag_enclose (item->vendor(), "vendor", true) << endl;
183 if ( isKind<Package>(resolvable) ) {
184 str << TAB << "<history>" << endl << TAB << "<update>" << endl;
185 str << TAB2 << helixXML (resolvable->arch()) << endl;
186 str << TAB2 << helixXML (resolvable->edition()) << endl;
187 str << TAB << "</update>" << endl << TAB << "</history>" << endl;
189 str << TAB << helixXML (resolvable->arch()) << endl;
190 str << TAB << helixXML (resolvable->edition()) << endl;
192 str << helixXML( resolvable, Dep::PROVIDES);
193 str << helixXML( resolvable, Dep::PREREQUIRES);
194 str << helixXML( resolvable, Dep::CONFLICTS);
195 str << helixXML( resolvable, Dep::OBSOLETES);
196 str << helixXML( resolvable, Dep::FRESHENS);
197 str << helixXML( resolvable, Dep::REQUIRES);
198 str << helixXML( resolvable, Dep::RECOMMENDS);
199 str << helixXML( resolvable, Dep::ENHANCES);
200 str << helixXML( resolvable, Dep::SUPPLEMENTS);
201 str << helixXML( resolvable, Dep::SUGGESTS);
203 str << "</" << toLower (resolvable->kind().asString()) << ">" << endl;
207 //---------------------------------------------------------------------------
210 :dumpPath("/var/log/YaST2/solverTestcase")
214 Testcase::Testcase(const std::string & path)
220 Testcase::~Testcase()
225 bool Testcase::createTestcase(Resolver & resolver, bool dumpPool, bool runSolver)
227 PathInfo path (dumpPath);
229 if ( !path.isExist() ) {
230 if (zypp::filesystem::mkdir (dumpPath)!=0) {
231 ERR << "Cannot create directory " << dumpPath << endl;
236 ERR << dumpPath << " is not a directory." << endl;
239 // remove old stuff if pool will be dump
241 zypp::filesystem::clean_dir (dumpPath);
245 zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
246 zypp::base::LogControl::TmpExcessive excessive;
248 resolver.reset(true); // true = resetting all valid solverresults
249 resolver.resolvePool();
251 zypp::base::LogControl::instance().logfile( "/var/log/YaST2/y2log" );
254 ResPool pool = resolver.pool();
255 RepositoryTable repoTable;
256 PoolItemList items_to_install;
257 PoolItemList items_to_remove;
258 PoolItemList items_locked;
259 PoolItemList items_keep;
260 PoolItemList language;
261 HelixResolvable_Ptr system = NULL;
264 system = new HelixResolvable(dumpPath + "/solver-system.xml.gz");
266 for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
268 Resolvable::constPtr res = it->resolvable();
270 if (isKind<Language>(res)) {
271 if ( it->status().isInstalled()
272 || it->status().isToBeInstalled()) {
273 language.push_back (*it);
276 if ( system && it->status().isInstalled() ) {
278 system->addResolvable (*it);
281 ResObject::constPtr repoItem = it->resolvable();
282 Repository repo = repoItem->repository();
284 if (repoTable.find (repo) == repoTable.end()) {
285 repoTable[repo] = new HelixResolvable(dumpPath + "/"
286 + numstring(repo.numericId())
287 + "-package.xml.gz");
289 repoTable[repo]->addResolvable (*it);
293 if ( it->status().isToBeInstalled()
294 && !(it->status().isBySolver())) {
295 items_to_install.push_back (*it);
297 if ( it->status().isKept()
298 && !(it->status().isBySolver())) {
299 items_keep.push_back (*it);
301 if ( it->status().isToBeUninstalled()
302 && !(it->status().isBySolver())) {
303 items_to_remove.push_back (*it);
305 if ( it->status().isLocked()
306 && !(it->status().isBySolver())) {
307 items_locked.push_back (*it);
312 // writing control file "*-test.xml"
314 HelixControl control (dumpPath + "/solver-test.xml",
316 zypp_detail::defaultArchitecture(),
319 for (PoolItemList::const_iterator iter = items_to_install.begin(); iter != items_to_install.end(); iter++) {
320 control.installResolvable (iter->resolvable(), iter->status());
323 for (PoolItemList::const_iterator iter = items_locked.begin(); iter != items_locked.end(); iter++) {
324 control.lockResolvable (iter->resolvable(), iter->status());
327 for (PoolItemList::const_iterator iter = items_keep.begin(); iter != items_keep.end(); iter++) {
328 control.keepResolvable (iter->resolvable(), iter->status());
331 for (PoolItemList::const_iterator iter = items_to_remove.begin(); iter != items_to_remove.end(); iter++) {
332 control.deleteResolvable (iter->resolvable(), iter->status());
335 control.addDependencies (resolver.extraRequires(), resolver.extraConflicts());
340 //---------------------------------------------------------------------------
342 HelixResolvable::HelixResolvable(const std::string & path)
345 file = new ofgzstream(path.c_str());
347 ZYPP_THROW (Exception( "Can't open " + path ) );
350 *file << "<channel><subchannel>" << endl;
353 HelixResolvable::~HelixResolvable()
355 *file << "</subchannel></channel>" << endl;
359 void HelixResolvable::addResolvable(const PoolItem_Ref item)
361 *file << helixXML (item);
364 //---------------------------------------------------------------------------
366 HelixControl::HelixControl(const std::string & controlPath,
367 const RepositoryTable & repoTable,
368 const Arch & systemArchitecture,
369 const PoolItemList &languages,
370 const std::string & systemPath)
371 :dumpFile (controlPath)
373 file = new ofstream(controlPath.c_str());
375 ZYPP_THROW (Exception( "Can't open " + controlPath ) );
378 *file << "<?xml version=\"1.0\"?>" << endl
379 << "<!-- testcase generated by YaST -->" << endl
381 << "<setup arch=\"" << systemArchitecture << "\">" << endl
382 << TAB << "<system file=\"" << systemPath << "\"/>" << endl << endl;
383 for ( RepositoryTable::const_iterator it = repoTable.begin();
384 it != repoTable.end(); ++it ) {
385 Repository repo = it->first;
386 *file << TAB << "<!-- " << endl
387 << TAB << "- alias : " << repo.info().alias() << endl;
388 for ( RepoInfo::urls_const_iterator itUrl = repo.info().baseUrlsBegin();
389 itUrl != repo.info().baseUrlsEnd();
392 *file << TAB << "- url : " << *itUrl << endl;
394 *file << TAB << "- path : " << repo.info().path() << endl;
395 *file << TAB << "- type : " << repo.info().type() << endl;
396 *file << TAB << " -->" << endl;
398 *file << TAB << "<channel file=\"" << numstring(repo.numericId())
399 << "-package.xml.gz\" name=\"" << numstring(repo.numericId())
400 << "\" />" << endl << endl;
402 for (PoolItemList::const_iterator iter = languages.begin(); iter != languages.end(); iter++) {
403 *file << TAB << "<locale name=\"" << iter->resolvable()->name()
406 *file << "</setup>" << endl
408 << "<showpool all=\"yes\"/>" << endl
409 << "<establish/>" << endl
410 << "<showpool all=\"true\" prefix=\">!> ESTABLISHED:\"/>" << endl;
413 HelixControl::HelixControl()
414 :dumpFile ("/var/log/YaST2/solverTestcase/solver-test.xml")
416 HelixControl (dumpFile);
419 HelixControl::~HelixControl()
421 *file << "</trial>" << endl
422 << "</test>" << endl;
425 void HelixControl::installResolvable(const ResObject::constPtr &resObject,
426 const ResStatus &status)
428 Repository repo = resObject->repository();
429 *file << "<install channel=\"" << numstring(repo.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
430 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
431 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
432 << " status=\"" << status << "\""
436 void HelixControl::lockResolvable(const ResObject::constPtr &resObject,
437 const ResStatus &status)
439 Repository repo = resObject->repository();
440 *file << "<lock channel=\"" << numstring(repo.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
441 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
442 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
443 << " status=\"" << status << "\""
447 void HelixControl::keepResolvable(const ResObject::constPtr &resObject,
448 const ResStatus &status)
450 Repository repo = resObject->repository();
451 *file << "<keep channel=\"" << numstring(repo.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
452 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
453 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
454 << " status=\"" << status << "\""
458 void HelixControl::deleteResolvable(const ResObject::constPtr &resObject,
459 const ResStatus &status)
461 Repository repo = resObject->repository();
462 *file << "<uninstall " << " kind=\"" << toLower (resObject->kind().asString()) << "\""
463 << " name=\"" << resObject->name() << "\""
464 << " status=\"" << status << "\""
468 void HelixControl::addDependencies (const CapabilitySet & capRequire, const CapabilitySet & capConflict)
470 for (CapabilitySet::const_iterator iter = capRequire.begin(); iter != capRequire.end(); iter++) {
471 *file << "<addRequire " << " name=\"" << iter->asString() << "\"" << "/>" << endl;
473 for (CapabilitySet::const_iterator iter = capConflict.begin(); iter != capConflict.end(); iter++) {
474 *file << "<addConflict " << " name=\"" << iter->asString() << "\"" << "/>" << endl;
479 ///////////////////////////////////////////////////////////////////
480 };// namespace detail
481 /////////////////////////////////////////////////////////////////////
482 /////////////////////////////////////////////////////////////////////
483 };// namespace solver
484 ///////////////////////////////////////////////////////////////////////
485 ///////////////////////////////////////////////////////////////////////
487 /////////////////////////////////////////////////////////////////////////