From bcda1a7ce349a130990c29536798b3a42bf079cb Mon Sep 17 00:00:00 2001 From: Corentin Chary Date: Fri, 20 Jul 2012 08:22:52 +0200 Subject: [PATCH] euscan: add --ebuild-uri to use ${PV}, etc. Signed-off-by: Corentin Chary --- bin/euscan | 13 +++++++++---- pym/euscan/__init__.py | 1 + pym/euscan/out.py | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/bin/euscan b/bin/euscan index 784daf9..8696848 100755 --- a/bin/euscan +++ b/bin/euscan @@ -128,7 +128,10 @@ def print_usage(_error=None, help=None): print(yellow(" -I, --ignore-pre-release-if-stable") + " - Ignore non-stable versions only if current\n" + " " * 38 + "version is stable", file=out) - + print(yellow(" --mirror") + + " - use mirror:// URIs", file=out) + print(yellow(" --ebuild-uri") + + " - use ebuild variables in URIs", file=out) print(file=out) if _error in ('packages',) or help: @@ -182,12 +185,14 @@ def parse_args(): pp.output.nocolor() elif o in ("-p", "--progress"): CONFIG['progress'] = isatty - elif o in ("-m", "--mirror"): + elif o in ("--mirror"): CONFIG['mirror'] = True elif o in ("-i", "--ignore-pre-release"): CONFIG['ignore-pre-release'] = True elif o in ("-I", "--ignore-pre-release-if-stable"): CONFIG['ignore-pre-release-if-stable'] = True + elif o in ("--ebuild-uri"): + CONFIG['ebuild-uri'] = True else: return_code = False @@ -195,11 +200,11 @@ def parse_args(): # here are the different allowed command line options (getopt args) getopt_options = {'short': {}, 'long': {}} - getopt_options['short']['global'] = "hVCqv1bf:pmiI" + getopt_options['short']['global'] = "hVCqv1bf:piI" getopt_options['long']['global'] = [ "help", "version", "nocolor", "quiet", "verbose", "oneshot", "brute-force=", "format=", "progress", "mirror", "ignore-pre-release", - "ignore-pre-release-if-stable", + "ignore-pre-release-if-stable", "ebuild-uri" ] short_opts = getopt_options['short']['global'] diff --git a/pym/euscan/__init__.py b/pym/euscan/__init__.py index 53d51f6..8efdce4 100644 --- a/pym/euscan/__init__.py +++ b/pym/euscan/__init__.py @@ -24,6 +24,7 @@ CONFIG = { 'mirror': False, 'ignore-pre-release': False, 'ignore-pre-release-if-stable': False, + 'ebuild-uri': False, } BLACKLIST_VERSIONS = [ diff --git a/pym/euscan/out.py b/pym/euscan/out.py index 4a68982..0717a4b 100644 --- a/pym/euscan/out.py +++ b/pym/euscan/out.py @@ -5,10 +5,9 @@ import signal import time import re -from gentoolkit import pprinter as pp import portage from portage.output import EOutput, TermProgressBar - +from gentoolkit import pprinter as pp class ProgressHandler(object): def __init__(self, progress_bar): @@ -66,6 +65,31 @@ def clean_colors(string): return string +def transform_url(config, cpv, url): + if config['mirror']: + url = to_mirror(url) + if config['ebuild-uri']: + url = to_ebuild_uri(cpv, url) + return url + +def to_ebuild_uri(cpv, url): + cat, pkg, ver, rev = portage.catpkgsplit(cpv) + p = '%s-%s' % (pkg, ver) + pvr = '%s%s' % (ver, '-%s' % rev if rev != 'r0' else '') + pf = '%s-%s' % (pkg, pvr) + evars = ( + (p , 'P'), + (pkg, 'PN'), + (ver, 'PV'), + (rev, 'PR'), + (pvr, 'PVR'), + (pf , 'PF'), + (cat, 'CATEGORY') + ) + for src, dst in evars: + url = url.replace(src, '${%s}' % dst) + return url + def to_mirror(url): mirrors = portage.settings.thirdpartymirrors() for mirror_name in mirrors: @@ -148,13 +172,15 @@ class EuscanOutput(object): def result(self, cp, version, urls, handler, confidence): from euscan.helpers import get_version_type - if self.config['format']: + cpv = '%s-%s' % (cp, version) + urls = ' '.join(transform_url(self.config, cpv, url) for url in urls.split()) + + if self.config['format'] in ['json']: _curr = self.queries[self.current_query] _curr["result"].append( { "version": version, - "urls": [to_mirror(url) if self.config['mirror'] else url - for url in urls.split()], + "urls": urls.split(), "handler": handler, "confidence": confidence, "type": get_version_type(version)