libzypp  10.5.0
SystemCheck.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include <fstream>
00014 #include <vector>
00015 
00016 #include "zypp/base/LogTools.h"
00017 #include "zypp/base/IOStream.h"
00018 #include "zypp/base/String.h"
00019 
00020 #include "zypp/ZYppFactory.h"
00021 #include "zypp/ZConfig.h"
00022 #include "zypp/Pathname.h"
00023 #include "zypp/PathInfo.h"
00024 #include "zypp/solver/detail/SystemCheck.h"
00025 
00026 using namespace std;
00027 
00029 namespace zypp
00030 { 
00031 
00032     Pathname            _file = "";
00033     CapabilitySet       _require;
00034     CapabilitySet       _conflict;
00035 
00036     typedef vector<string> CapList;
00037 
00038     const SystemCheck & SystemCheck::instance()
00039     {
00040         static SystemCheck _val;
00041         return _val;
00042     }
00043 
00044 
00045     SystemCheck::SystemCheck() {
00046         if (_file.empty()) {
00047             _file = ZConfig::instance().solver_checkSystemFile();
00048             loadFile();
00049         }
00050     }
00051 
00052     bool SystemCheck::setFile(const Pathname & file) const{
00053         MIL << "Setting checkFile to : " << file << endl;
00054         _file = file;
00055         loadFile();
00056         return true;
00057     }
00058 
00059     const Pathname & SystemCheck::file() {
00060         return _file;
00061     }
00062 
00063     const CapabilitySet & SystemCheck::requiredSystemCap() const{
00064         return _require;
00065     }
00066 
00067     const CapabilitySet & SystemCheck::conflictSystemCap() const{
00068         return _conflict;
00069     }
00070 
00071     bool SystemCheck::loadFile() const{
00072         Target_Ptr trg( getZYpp()->getTarget() );
00073         if ( trg )
00074           _file = trg->assertRootPrefix( _file );
00075 
00076         PathInfo pi( _file );
00077         if ( ! pi.isFile() ) {
00078             WAR << "Can't read " << _file << " " << pi << endl;
00079             return false;
00080         }
00081 
00082         _require.clear();
00083         _conflict.clear();
00084 
00085         std::ifstream infile( _file.c_str() );
00086         for( iostr::EachLine in( infile ); in; in.next() ) {
00087             std::string l( str::trim(*in) );
00088             if ( ! l.empty() && l[0] != '#' )
00089             {
00090                 CapList capList;
00091                 str::split( l, back_inserter(capList), ":" );
00092                 if (capList.size() == 2 ) {
00093                     CapList::iterator it = capList.begin();
00094                     if (*it == "requires") {
00095                         _require.insert(Capability(*(it+1)));
00096                     } else if (*it == "conflicts") {
00097                         _conflict.insert(Capability(*(it+1)));
00098                     } else {
00099                         ERR << "Wrong parameter: " << l << endl;
00100                     }
00101                 } else {
00102                     ERR << "Wrong line: " << l << endl;
00103                 }
00104             }
00105         }
00106         MIL << "Read " << pi << endl;
00107         return true;
00108     }
00109 
00110 
00111     /******************************************************************
00112     **
00113     **  FUNCTION NAME : operator<<
00114     **  FUNCTION TYPE : std::ostream &
00115     */
00116     std::ostream & operator<<( std::ostream & str, const SystemCheck & obj )
00117     {
00118       str << _file << endl;
00119       str << "requires" << endl;
00120       for (CapabilitySet::const_iterator it = _require.begin(); it != _require.end(); ++it)
00121           str << "  " << *it << endl;
00122 
00123       str << "conflicts" << endl;
00124       for (CapabilitySet::const_iterator it = _conflict.begin(); it != _conflict.end(); ++it)
00125           str << "  " << *it << endl;
00126 
00127       return str;
00128     }
00129 
00131 } // namespace zypp