From 4950a657c4a87919fdfa3e19ad6a7e18c79f0082 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 6 Oct 2006 13:31:00 +0000 Subject: [PATCH] get rid of the token usage --- tools/zmart/zypp-checkpatches.cc | 190 ++++++++++++++++++++++----------------- 1 file changed, 108 insertions(+), 82 deletions(-) diff --git a/tools/zmart/zypp-checkpatches.cc b/tools/zmart/zypp-checkpatches.cc index ea95e16..7b46687 100644 --- a/tools/zmart/zypp-checkpatches.cc +++ b/tools/zmart/zypp-checkpatches.cc @@ -3,8 +3,6 @@ #include #include -#include - #include #include #include @@ -32,32 +30,74 @@ using namespace std; using namespace zypp; using namespace boost; -namespace po = boost::program_options; #define ZYPP_CHECKPATCHES_LOG "/var/log/zypp-checkpatches.log" +#define TOKEN_FILE "/var/lib/zypp/cache/updates_token" +#define RESULT_FILE "/var/lib/zypp/cache/updates_result.xml" + ZYpp::Ptr God; RuntimeData gData; Settings gSettings; ostream no_stream(NULL); -//using namespace DbXml; +static string read_old_token() +{ + string buffer; + string token; + std::ifstream is(TOKEN_FILE); + if ( is.good() ) + { + while(is && !is.eof()) + { + getline(is, buffer); + token += buffer; + } + is.close(); + } + return token; +} + + +static void save_token( const std::string &token ) +{ + std::ofstream os(TOKEN_FILE); + if ( os.good() ) + { + os << token << endl;; + } + os.close(); +} + +static void render_error( std::ostream &out, const std::string &reason ) +{ + out << "" << std::endl; + out << "" << reason << "" << std::endl; + out << "" << std::endl; +} + +static void render_unchanged( std::ostream &out, const std::string &token ) +{ + out << "" << std::endl; + // out << " " << std::endl; + out << "" << std::endl; +} -static void render_xml( const zypp::ResPool &pool, const string &token ) +static void render_result( std::ostream &out, const zypp::ResPool &pool) { int count = 0; int security_count = 0; - cout << "" << std::endl; - cout << "" << std::endl; - cout << " " << std::endl; - cout << " " << std::endl; + out << "" << std::endl; + out << "" << std::endl; + //out << " " << std::endl; + out << " " << std::endl; for ( std::list::const_iterator it = gData.sources.begin(); it != gData.sources.end(); ++it ) { - cout << " url() << "\" alias=\"" << it->alias() << "\">" << std::endl; + out << " url() << "\" alias=\"" << it->alias() << "\">" << std::endl; } - cout << " " << std::endl; - cout << " " << std::endl; + out << " " << std::endl; + out << " " << std::endl; for ( ResPool::byKind_iterator it = pool.byKindBegin(); it != pool.byKindEnd(); ++it ) { Resolvable::constPtr res = it->resolvable(); @@ -65,9 +105,9 @@ static void render_xml( const zypp::ResPool &pool, const string &token ) MIL << patch->name() << " " << patch->edition() << " " << "[" << patch->category() << "]" << ( it->status().isNeeded() ? " [needed]" : " [unneeded]" )<< std::endl; if ( it->status().isNeeded() ) { - cout << " category() << "\">" << std::endl; - cout << " " << patch->name() << "" <" << patch->edition() << "" <category() << "\">" << std::endl; + out << " " << patch->name() << "" <" << patch->edition() << "" <" << std::endl; - cout << " " << std::endl; - cout << "" << std::endl; + out << " " << std::endl; + out << " " << std::endl; + out << "" << std::endl; } int main(int argc, char **argv) @@ -88,60 +128,6 @@ int main(int argc, char **argv) else zypp::base::LogControl::instance().logfile( ZYPP_CHECKPATCHES_LOG ); - po::positional_options_description pos_options; - pos_options.add("command", -1); - - po::options_description general_options("General options"); - general_options.add_options() - ("help,h", "produce a help message") - ("version,v", "output the version number") - ; - - po::options_description check_options("Check options"); - check_options.add_options() - ("previous-token,t", po::value< string >(), "The token got from last run.") - ("previous-result,r", po::value< int >(), "Previous result. If repositories are the same as last run, this will be shown.") - ; - - //po::options_description source_options("Source options"); - //source_options.add_options() - // ("disable-system-sources,D", "Don't read the system sources.") - // ("sources,S", po::value< vector >(), "Read from additional sources") - // ; - - // Declare an options description instance which will include - // all the options - po::options_description all_options("Allowed options"); - all_options.add(general_options).add(check_options); - - // Declare an options description instance which will be shown - // to the user - po::options_description visible_options("Allowed options"); - visible_options.add(general_options).add(check_options); - - po::variables_map vm; - //po::store(po::parse_command_line(argc, argv, visible_options), vm); - po::store(po::command_line_parser(argc, argv).options(visible_options).positional(pos_options).run(), vm); - po::notify(vm); - - if (vm.count("help")) { - cout << visible_options << "\n"; - return 1; - } - - std::string previous_token; - if (vm.count("previous-token")) - { - previous_token = vm["previous-token"].as< string >(); - } - - int previous_code = -1; - if (vm.count("previous-result")) - { - previous_code = vm["previous-result"].as< int >(); - } - - MIL << argv[0] << " started with arguments " << previous_token << " " << previous_code << std::endl; ZYpp::Ptr God = NULL; try @@ -151,9 +137,14 @@ int main(int argc, char **argv) catch (Exception & excpt_r) { ZYPP_CAUGHT (excpt_r); - ERR << "a ZYpp transaction is already in progress." << endl; - cerr << "a ZYpp transaction is already in progress." << endl; - //cout << RANDOM_TOKEN; + + std::ofstream os(TOKEN_FILE); + if ( os.good() ) + { + render_error( os, "a ZYpp transaction is already in progress."); + render_error( cout, "a ZYpp transaction is already in progress."); + os.close(); + } return -1; } @@ -170,8 +161,16 @@ int main(int argc, char **argv) catch (Exception & excpt_r) { ZYPP_CAUGHT (excpt_r); - ERR << "Couldn't restore sources" << endl; - //return -1; + + std::ofstream os(TOKEN_FILE); + if ( os.good() ) + { + render_error( os, "Couldn't restore sources"); + render_error( cout, "Couldn't restore sources"); + os.close(); + } + + return -1; } // dont add rpms @@ -202,14 +201,35 @@ int main(int argc, char **argv) token_stream << "[" << "target" << "| " << God->target()->timestamp() << "]"; + string previous_token; + if ( PathInfo(TOKEN_FILE).isExist() ) + previous_token = read_old_token(); + else + previous_token = RANDOM_TOKEN; + //static std::string digest(const std::string& name, std::istream& is token = Digest::digest("sha1", token_stream); //cout << token; - MIL << "new token [" << token << "]" << " previous: [" << previous_token << "] previous code: " << previous_code << std::endl; + MIL << "new token [" << token << "]" << " previous: [" << previous_token << "]" << std::endl; + + // use the old result if ( token == previous_token ) { - return previous_code; + std::ifstream is(RESULT_FILE); + + string buffer; + while(is && !is.eof()) + { + getline(is, buffer); + cout << buffer << endl; + } + //return previous_code; + return -1; + } + else + { + MIL << "System has changed, recalculation of updates needed" << endl; } for ( std::list::const_iterator it = gData.sources.begin(); it != gData.sources.end(); ++it ) @@ -225,9 +245,15 @@ int main(int argc, char **argv) int security_count = 0; MIL << "Pool contains " << God->pool().size() << " items. Checking whether available patches are needed." << std::endl; - - render_xml(God->pool(), token ); - + std::ofstream os(RESULT_FILE); + if ( os.good() ) + { + render_result( os, God->pool()); + render_result( cout, God->pool()); + os.close(); + } + // save token + save_token(token); //MIL << "Patches " << security_count << " " << count << std::endl; if ( security_count > 0 ) -- 2.7.4