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 Dependencies &dep )
126 if ( dep[Dep::PROVIDES].size() > 0 )
127 str << TAB << xml_tag_enclose(helixXML(dep[Dep::PROVIDES]), "provides") << endl;
128 if ( dep[Dep::CONFLICTS].size() > 0 )
129 str << TAB << xml_tag_enclose(helixXML(dep[Dep::CONFLICTS]), "conflicts") << endl;
130 if ( dep[Dep::OBSOLETES].size() > 0 )
131 str << TAB << xml_tag_enclose(helixXML(dep[Dep::OBSOLETES]), "obsoletes") << endl;
132 if ( dep[Dep::FRESHENS].size() > 0 )
133 str << TAB << xml_tag_enclose(helixXML(dep[Dep::FRESHENS]), "freshens") << endl;
134 if ( dep[Dep::REQUIRES].size() > 0 )
135 str << TAB << xml_tag_enclose(helixXML(dep[Dep::REQUIRES]), "requires") << endl;
136 if ( dep[Dep::RECOMMENDS].size() > 0 )
137 str << TAB << xml_tag_enclose(helixXML(dep[Dep::RECOMMENDS]), "recommends") << endl;
138 if ( dep[Dep::ENHANCES].size() > 0 )
139 str << TAB << xml_tag_enclose(helixXML(dep[Dep::ENHANCES]), "enhances") << endl;
140 if ( dep[Dep::SUPPLEMENTS].size() > 0 )
141 str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUPPLEMENTS]), "supplements") << endl;
142 if ( dep[Dep::SUGGESTS].size() > 0 )
143 str << TAB << xml_tag_enclose(helixXML(dep[Dep::SUGGESTS]), "suggests") << endl;
147 std::string helixXML( const PoolItem_Ref &item )
149 const Resolvable::constPtr resolvable = item.resolvable();
151 if ( isKind<Language>(resolvable) ) {
152 // language dependencies will be written in another part
156 str << "<" << toLower (resolvable->kind().asString()) << ">" << endl;
157 str << TAB << xml_tag_enclose (resolvable->name(), "name", true) << endl;
158 str << TAB << xml_tag_enclose (item->vendor(), "vendor", true) << endl;
159 if ( isKind<Package>(resolvable) ) {
160 str << TAB << "<history>" << endl << TAB << "<update>" << endl;
161 str << TAB2 << helixXML (resolvable->arch()) << endl;
162 str << TAB2 << helixXML (resolvable->edition()) << endl;
163 str << TAB << "</update>" << endl << TAB << "</history>" << endl;
165 str << TAB << helixXML (resolvable->arch()) << endl;
166 str << TAB << helixXML (resolvable->edition()) << endl;
168 str << helixXML (resolvable->deps());
170 str << "</" << toLower (resolvable->kind().asString()) << ">" << endl;
174 //---------------------------------------------------------------------------
177 :dumpPath("/var/log/YaST2/solverTestcase")
181 Testcase::Testcase(const std::string & path)
187 Testcase::~Testcase()
191 bool Testcase::createTestcase(Resolver & resolver)
193 PathInfo path (dumpPath);
195 if ( !path.isExist() ) {
196 if (zypp::filesystem::mkdir (dumpPath)!=0) {
197 ERR << "Cannot create directory " << dumpPath << endl;
202 ERR << dumpPath << " is not a directory." << endl;
206 zypp::filesystem::clean_dir (dumpPath);
209 zypp::base::LogControl::instance().logfile( dumpPath +"/y2log" );
210 zypp::base::LogControl::TmpExcessive excessive;
212 resolver.reset(true); // true = resetting all valid solverresults
213 resolver.resolvePool();
215 zypp::base::LogControl::instance().logfile( "/var/log/YaST2/y2log" );
217 ResPool pool = resolver.pool();
218 RepositoryTable repoTable;
219 PoolItemList items_to_install;
220 PoolItemList items_to_remove;
221 PoolItemList items_locked;
222 PoolItemList items_keep;
223 PoolItemList language;
224 HelixResolvable system (dumpPath + "/solver-system.xml");
226 for ( ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it )
228 Resolvable::constPtr res = it->resolvable();
230 if (isKind<Language>(res)) {
231 if ( it->status().isInstalled()
232 || it->status().isToBeInstalled()) {
233 language.push_back (*it);
236 if ( it->status().isInstalled() ) {
238 system.addResolvable (*it);
241 ResObject::constPtr repoItem = it->resolvable();
242 Repository repo = repoItem->repository();
243 if (repoTable.find (repo) == repoTable.end()) {
244 repoTable[repo] = new HelixResolvable(dumpPath + "/"
245 + numstring(repo.numericId())
248 repoTable[repo]->addResolvable (*it);
251 if ( it->status().isToBeInstalled()
252 && !(it->status().isBySolver())) {
253 items_to_install.push_back (*it);
255 if ( it->status().isKept()
256 && !(it->status().isBySolver())) {
257 items_keep.push_back (*it);
259 if ( it->status().isToBeUninstalled()
260 && !(it->status().isBySolver())) {
261 items_to_remove.push_back (*it);
263 if ( it->status().isLocked()
264 && !(it->status().isBySolver())) {
265 items_locked.push_back (*it);
271 // writing control file "*-test.xml"
273 HelixControl control (dumpPath + "/solver-test.xml",
275 zypp_detail::defaultArchitecture(),
278 for (PoolItemList::const_iterator iter = items_to_install.begin(); iter != items_to_install.end(); iter++) {
279 control.installResolvable (iter->resolvable());
282 for (PoolItemList::const_iterator iter = items_locked.begin(); iter != items_locked.end(); iter++) {
283 control.lockResolvable (iter->resolvable());
286 for (PoolItemList::const_iterator iter = items_keep.begin(); iter != items_keep.end(); iter++) {
287 control.keepResolvable (iter->resolvable());
290 for (PoolItemList::const_iterator iter = items_to_remove.begin(); iter != items_to_remove.end(); iter++) {
291 control.deleteResolvable (iter->resolvable());
294 control.addDependencies (resolver.extraRequires(), resolver.extraConflicts());
299 //---------------------------------------------------------------------------
301 HelixResolvable::HelixResolvable(const std::string & path)
304 file = new ofstream(path.c_str());
306 ZYPP_THROW (Exception( "Can't open " + path ) );
309 *file << "<channel><subchannel>" << endl;
312 HelixResolvable::~HelixResolvable()
314 *file << "</subchannel></channel>" << endl;
318 void HelixResolvable::addResolvable(const PoolItem_Ref item)
320 *file << helixXML (item);
323 //---------------------------------------------------------------------------
325 HelixControl::HelixControl(const std::string & controlPath,
326 const RepositoryTable & repoTable,
327 const Arch & systemArchitecture,
328 const PoolItemList &languages,
329 const std::string & systemPath)
330 :dumpFile (controlPath)
332 file = new ofstream(controlPath.c_str());
334 ZYPP_THROW (Exception( "Can't open " + controlPath ) );
337 *file << "<?xml version=\"1.0\"?>" << endl
338 << "<!-- testcase generated by YaST -->" << endl
340 << "<setup arch=\"" << systemArchitecture << "\">" << endl
341 << TAB << "<system file=\"" << systemPath << "\"/>" << endl << endl;
342 for ( RepositoryTable::const_iterator it = repoTable.begin();
343 it != repoTable.end(); ++it ) {
344 Repository repo = it->first;
345 *file << TAB << "<!-- " << endl
346 << TAB << "- alias : " << repo.info().alias() << endl;
347 for ( RepoInfo::urls_const_iterator itUrl = repo.info().baseUrlsBegin();
348 itUrl != repo.info().baseUrlsEnd();
351 *file << TAB << "- url : " << *itUrl << endl;
353 *file << TAB << "- path : " << repo.info().path() << endl;
354 *file << TAB << "- type : " << repo.info().type() << endl;
355 *file << TAB << " -->" << endl;
357 *file << TAB << "<channel file=\"" << numstring(repo.numericId())
358 << "-package.xml\" name=\"" << numstring(repo.numericId())
359 << "\" />" << endl << endl;
361 for (PoolItemList::const_iterator iter = languages.begin(); iter != languages.end(); iter++) {
362 *file << TAB << "<locale name=\"" << iter->resolvable()->name()
365 *file << "</setup>" << endl
367 << "<showpool all=\"yes\"/>" << endl
368 << "<establish/>" << endl
369 << "<showpool all=\"true\" prefix=\">!> ESTABLISHED:\"/>" << endl;
372 HelixControl::HelixControl()
373 :dumpFile ("/var/log/YaST2/solverTestcase/solver-test.xml")
375 HelixControl (dumpFile);
378 HelixControl::~HelixControl()
380 *file << "</trial>" << endl
381 << "</test>" << endl;
384 void HelixControl::installResolvable(const ResObject::constPtr &resObject)
386 Repository repo = resObject->repository();
387 *file << "<install channel=\"" << numstring(repo.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
388 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
389 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
393 void HelixControl::lockResolvable(const ResObject::constPtr &resObject)
395 Repository repo = resObject->repository();
396 *file << "<lock channel=\"" << numstring(repo.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
397 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
398 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
402 void HelixControl::keepResolvable(const ResObject::constPtr &resObject)
404 Repository repo = resObject->repository();
405 *file << "<keep channel=\"" << numstring(repo.numericId()) << "\" kind=\"" << toLower (resObject->kind().asString()) << "\""
406 << " name=\"" << resObject->name() << "\"" << " arch=\"" << resObject->arch().asString() << "\""
407 << " version=\"" << resObject->edition().version() << "\"" << " release=\"" << resObject->edition().release() << "\""
411 void HelixControl::deleteResolvable(const ResObject::constPtr &resObject)
413 Repository repo = resObject->repository();
414 *file << "<uninstall " << " kind=\"" << toLower (resObject->kind().asString()) << "\""
415 << " name=\"" << resObject->name() << "\"" << "/>" << endl;
418 void HelixControl::addDependencies (const CapabilitySet & capRequire, const CapabilitySet & capConflict)
420 for (CapabilitySet::const_iterator iter = capRequire.begin(); iter != capRequire.end(); iter++) {
421 *file << "<addRequire " << " name=\"" << iter->asString() << "\"" << "/>" << endl;
423 for (CapabilitySet::const_iterator iter = capConflict.begin(); iter != capConflict.end(); iter++) {
424 *file << "<addConflict " << " name=\"" << iter->asString() << "\"" << "/>" << endl;
429 ///////////////////////////////////////////////////////////////////
430 };// namespace detail
431 /////////////////////////////////////////////////////////////////////
432 /////////////////////////////////////////////////////////////////////
433 };// namespace solver
434 ///////////////////////////////////////////////////////////////////////
435 ///////////////////////////////////////////////////////////////////////
437 /////////////////////////////////////////////////////////////////////////