From b6e10b43f466f485a2826a35fadb64a786a235c5 Mon Sep 17 00:00:00 2001 From: aeroniero33 Date: Fri, 17 Jun 2016 17:40:01 +0000 Subject: Added automatic update check + fixes --- gkeys/gkeys/actions.py | 17 +++++++----- gkeys/gkeys/base.py | 68 +++++++++++++++++++++++++++++++++++++++++++++- gkeys/gkeys/seedhandler.py | 9 +++++- 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/gkeys/gkeys/actions.py b/gkeys/gkeys/actions.py index fe45222..f31c728 100644 --- a/gkeys/gkeys/actions.py +++ b/gkeys/gkeys/actions.py @@ -133,19 +133,22 @@ class Actions(ActionBase): print("Update succeeded.\n") messages = fetch_messages + [" Update operation:"] + [install_messages] success, new_gkeys = self.listseed(args) - added_gkeys, changed_gkeys = self.seedhandler.compare_seeds(old_gkeys, new_gkeys) + added_gkeys, changed_gkeys, removed_gkeys = self.seedhandler.compare_seeds(old_gkeys, new_gkeys) print("Updated revoked GKeys:") - if changed_gkeys: - for gkey in changed_gkeys: - self.output(['', changed_gkeys]) + for gkey in changed_gkeys: + self.output(['', changed_gkeys]) else: print("No GKeys were revoked") print("Added GKeys:") - if added_gkeys: - for gkey in added_gkeys: - self.output(['', added_gkeys]) + for gkey in added_gkeys: + self.output(['', added_gkeys]) else: print("No GKeys were added") + print("Removed GKeys:") + for gkey in removed_gkeys: + self.output(['', added_gkeys]) + else: + print("No GKeys were removed") return (success, messages) def addseed(self, args): diff --git a/gkeys/gkeys/base.py b/gkeys/gkeys/base.py index a0224c0..783b0a5 100644 --- a/gkeys/gkeys/base.py +++ b/gkeys/gkeys/base.py @@ -17,10 +17,20 @@ from __future__ import print_function import argparse import os import sys +import copy from gkeys.fileops import ensure_dirs from gkeys.log import log_levels, set_logger +from gkeys.gkey import GKEY +if sys.version_info[0] >= 3: + from urllib.request import urlopen + py_input = input + _unicode = str +else: + from urllib2 import urlopen + py_input = raw_input + _unicode = unicode if sys.version_info[0] >= 3: unicode = str @@ -291,6 +301,10 @@ class CliBase(object): for opt in options: getattr(self, '_option_%s' % opt)(parser) + def warning_output(self, info): + ''' We don't want this message to be spammed 4 times everytime gkeys is run''' + if "Re-fetch cycle timeout of" not in info: + print(info) def setup(self, args, configs): '''Set up the args and configs passed in @@ -352,7 +366,59 @@ class CliBase(object): ''' # establish our actions instance self.actions = self.cli_config['Actions'](self.config, self.output_results, self.logger) - + # check for seed update + from sslfetch.connections import Connector + connector_output = { + 'info': self.logger.info, + 'debug': self.logger.debug, + 'error': self.logger.error, + 'exception': self.logger.exception, + 'warning': self.warning_output, + 'kwargs-info': {}, + 'kwargs-debug': {}, + 'kwargs-error': {}, + 'kwargs-exception': {}, + 'kwargs-warning': {}, + } + fetcher = Connector(connector_output, None, "Gentoo Keys") + successes = [] + up_to_date = True + categories = list(self.config.defaults['seeds']) + '''Attemp to download seed and seed.sig files for each available category''' + for category in categories: + filepath = self.config.defaults['seedsdir'] + "/" + category + ".seeds" + timestamp_path = filepath + ".timestamp" + url = self.config.defaults['seedurls'][category] + success, signedfile, timestamp = fetcher.fetch_file( + url, filepath, timestamp_path) + if timestamp != "": + up_to_date = False + successes.append(success) + url += ".sig" + filepath += ".sig" + success, signedfile, timestamp = fetcher.fetch_file( + url, filepath, timestamp_path) + if timestamp != "": + up_to_date = False + successes.append(success) + if False not in successes and not up_to_date: + print("Seeds need to be updated") + ack = None + while ack not in ("y", "yes", "n", "no"): + ack = py_input("Would you like to update the seeds now? (y/n) ").lower() + if ack in ("y", "yes"): + custom_args = copy.copy(args) + for attr in GKEY._fields: + if attr != "debug": + custom_args.attr = None + custom_args.category = None + custom_args.action = "update-seed" + print("Updating seeds") + self.run(custom_args) + elif False not in successes: + print("Seeds are up to date") + else: + print("Seed update check failed, check your internet connection.") # run the action func = getattr(self.actions, '%s' % self.cli_config['Action_Map'][args.action]['func']) diff --git a/gkeys/gkeys/seedhandler.py b/gkeys/gkeys/seedhandler.py index 993f260..3eadcd6 100644 --- a/gkeys/gkeys/seedhandler.py +++ b/gkeys/gkeys/seedhandler.py @@ -78,21 +78,28 @@ class SeedHandler(object): @param seeds2: set of seeds to be compared @return added_gkeys: list of keys that are included in seed2 but not seed1 @return changed_gkeys: list of keys that are included in seed1 and seed2 but have been altered + @return removed_gkeys: list of keys that are included in seed1 but not in seed2 ''' old_gkeys = seeds1[1] new_gkeys = seeds2[1] changed_gkeys = [] + old_changed_gkeys = [] + removed_gkeys = [] added_gkeys = [] if old_gkeys: for new_gkey in new_gkeys: for old_gkey in old_gkeys: if new_gkey.nick == old_gkey.nick and new_gkey != old_gkey: changed_gkeys.append(new_gkey) + old_changed_gkeys.append(old_gkey) if new_gkey not in old_gkeys and new_gkey not in changed_gkeys: added_gkeys.append(new_gkey) + for old_gkey in old_gkeys: + if old_gkey not in new_gkeys and old_gkey not in old_changed_gkeys: + removed_gkeys.append(old_gkey) else: added_gkeys = new_gkeys - return(added_gkeys, changed_gkeys) + return(added_gkeys, changed_gkeys, removed_gkeys) def load_seeds(self, seedfile=None, filepath=None, refresh=False): '''Load seed file -- cgit v1.2.3-65-gdbad