From fb25b7a0440ad3d003f00f253941e60701dc9c45 Mon Sep 17 00:00:00 2001 From: Jimmy Berry Date: Fri, 21 Apr 2017 17:09:17 -0500 Subject: [PATCH] osc-staging: implement colorization using colorama for adi and list. --- osc-staging.py | 6 ++++++ osclib/adi_command.py | 35 ++++++++++++++++++++++------------- osclib/list_command.py | 11 +++++++---- requirements.txt | 1 + 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/osc-staging.py b/osc-staging.py index 33b51960..af1c37a0 100644 --- a/osc-staging.py +++ b/osc-staging.py @@ -23,7 +23,10 @@ import tempfile import warnings import yaml +import colorama + from osc import cmdln +from osc import conf from osc import oscerr # Expand sys.path to search modules inside the pluging directory @@ -112,6 +115,7 @@ def _full_project_name(self, project): @cmdln.option('--merge', action='store_true', help='propose merge where applicable and store details to allow future merges') @cmdln.option('--try-strategies', action='store_true', default=False, help='apply strategies and keep any with desireable outcome') @cmdln.option('--strategy', help='apply a specific strategy') +@cmdln.option('--no-color', action='store_true', help='strip colors from output (or add staging.color = 0 to the .oscrc general section') def do_staging(self, subcmd, opts, *args): """${cmd_name}: Commands to work with staging projects @@ -314,6 +318,8 @@ def do_staging(self, subcmd, opts, *args): opts.apiurl = self.get_api_url() opts.verbose = False Config(opts.project) + colorama.init(autoreset=True, + strip=(opts.no_color or not bool(int(conf.config.get('staging.color', True))))) if opts.wipe_cache: Cache.delete_all() diff --git a/osclib/adi_command.py b/osclib/adi_command.py index 3c82dc1f..fc3a1085 100644 --- a/osclib/adi_command.py +++ b/osclib/adi_command.py @@ -1,5 +1,7 @@ import json +from colorama import Fore + from osc import oscerr from osc.core import delete_project from osc.core import show_package_meta @@ -17,31 +19,38 @@ class AdiCommand: query_project = self.api.extract_staging_short(project) info = self.api.project_status(project, True) if len(info['building_repositories']): - print query_project, "still building" + print query_project, Fore.MAGENTA + 'building' return if len(info['untracked_requests']): - print query_project, "untracked:", ', '.join(['%s[%s]'%(req['package'], req['number']) for req in info['untracked_requests']]) + print query_project, Fore.YELLOW + 'untracked:', ', '.join(['{}[{}]'.format( + Fore.CYAN + req['package'] + Fore.RESET, req['number']) for req in info['untracked_requests']]) return if len(info['obsolete_requests']): - print query_project, "obsolete:", ', '.join(['%s[%s]'%(req['package'], req['number']) for req in info['obsolete_requests']]) + print query_project, Fore.YELLOW + 'obsolete:', ', '.join(['{}[{}]'.format( + Fore.CYAN + req['package'] + Fore.RESET, req['number']) for req in info['obsolete_requests']]) return if len(info['broken_packages']): - print query_project, "broken:", ', '.join([p['package'] for p in info['broken_packages']]) + print query_project, Fore.RED + 'broken:', ', '.join([ + Fore.CYAN + p['package'] + Fore.RESET for p in info['broken_packages']]) return for review in info['missing_reviews']: - print query_project, "missing review by {} for {}[{}]".format(review['by'], review['package'], review['request']) + print query_project, Fore.WHITE + 'review:', '{} for {}[{}]'.format( + Fore.YELLOW + review['by'] + Fore.RESET, + Fore.CYAN + review['package'] + Fore.RESET, + review['request']) return if self.api.is_user_member_of(self.api.user, 'factory-staging'): - print query_project, "is ready" + print query_project, Fore.GREEN + 'ready' packages = [] for req in info['selected_requests']: - print " - %s [%s]"%(req['package'], req['number']) + print ' - {} [{}]'.format(Fore.CYAN + req['package'] + Fore.RESET, req['number']) self.api.rm_from_prj(project, request_id=req['number'], msg='ready to accept') packages.append(req['package']) self.api.accept_status_comment(project, packages) delete_project(self.api.apiurl, project) else: - print query_project, "ready:", ', '.join(['%s[%s]'%(req['package'], req['number']) for req in info['selected_requests']]) + print query_project, Fore.GREEN + 'ready:', ', '.join(['{}[{}]'.format( + Fore.CYAN + req['package'] + Fore.RESET, req['number']) for req in info['selected_requests']]) def check_adi_projects(self): for p in self.api.get_adi_projects(): @@ -64,21 +73,21 @@ class AdiCommand: splitter.split() for group in sorted(splitter.grouped.keys()): - print(group if group != '' else 'wanted') + print(Fore.YELLOW + (group if group != '' else 'wanted') + Fore.RESET) name = None for request in splitter.grouped[group]['requests']: request_id = int(request.get('id')) target_package = request.find('./action/target').get('package') - line = '- sr#{}: {:<30}'.format(request_id, target_package) + line = '- sr#{}: {}{:<30}{}'.format(request_id, Fore.CYAN, target_package, Fore.RESET) if request_id in self.requests_ignored: - print(line + '\n ignored: ' + str(self.requests_ignored[request_id])) + print(line + Fore.WHITE + '\n ignored: ' + str(self.requests_ignored[request_id]) + Fore.RESET) continue # Auto-superseding request in adi command if self.api.update_superseded_request(request): - print(line + ' (superseded)') + print(line + Fore.MAGENTA + ' (superseded)' + Fore.RESET) continue # Only create staging projec the first time a non superseded @@ -89,7 +98,7 @@ class AdiCommand: if not self.api.rq_to_prj(request_id, name): return False - print(line + ' (staged in {})'.format(name)) + print(line + Fore.GREEN + ' (staged in {})'.format(name) + Fore.RESET) if name: # Notify everybody about the changes. diff --git a/osclib/list_command.py b/osclib/list_command.py index 878835d1..a22ccad6 100644 --- a/osclib/list_command.py +++ b/osclib/list_command.py @@ -1,3 +1,4 @@ +from colorama import Fore from osc import oscerr from osclib.request_splitter import RequestSplitter from osclib.supersede_command import SupersedeCommand @@ -37,17 +38,19 @@ class ListCommand: is_factory = self.api.project != 'openSUSE:Factory' for group in sorted(splitter.grouped.keys()): - print group + print Fore.YELLOW + group for request in splitter.grouped[group]['requests']: request_id = int(request.get('id')) action = request.find('action') target_package = action.find('target').get('package') ring = action.find('target').get('ring') + if ring.startswith('0'): + ring = Fore.MAGENTA + ring + Fore.RESET if action.get('type') == 'delete': - ring += ' (delete request)' + ring += Fore.RED + ' (delete request)' - line = 'sr#{}: {:<30} -> {:<12}'.format(request_id, target_package, ring) + line = 'sr#{}: {}{:<30}{} -> {:<12}'.format(request_id, Fore.CYAN, target_package, Fore.RESET, ring) if is_factory and action.find('source') != None: source_project = action.find('source').get('project') @@ -55,7 +58,7 @@ class ListCommand: line += ' ({})'.format(source_project) if request_id in requests_ignored: - line += '\n ignored: ' + str(requests_ignored[request_id]) + line += Fore.WHITE + '\n ignored: ' + str(requests_ignored[request_id]) + Fore.RESET print ' ', line diff --git a/requirements.txt b/requirements.txt index 0f4c6f5f..3602479f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +colorama lxml PyYAML pycurl