From b0ccdd2157b6d2ba93c198b0b45b0e94948ecb68 Mon Sep 17 00:00:00 2001 From: Corentin Chary Date: Fri, 20 Apr 2012 17:27:16 +0200 Subject: [PATCH] euscan: new cpan handler Signed-off-by: Corentin Chary --- TODO | 3 ++ pym/euscan/handlers/__init__.py | 4 +- pym/euscan/handlers/cpan.py | 75 +++++++++++++++++++++++++++++++++ pym/euscan/handlers/php.py | 5 +-- 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 pym/euscan/handlers/cpan.py diff --git a/TODO b/TODO index e2337bb..11d089c 100644 --- a/TODO +++ b/TODO @@ -22,6 +22,9 @@ euscan - Add a way to how we failled to find a new version - add a HTTP cache (1day, configurable) - add a way to enable/disable methods: other_distributions,remote_euscan,handlers, etc.... +- try to differenciate stable, beta, alpha versions +- give the full Package() to handlers, not just the cpv string +- better handlers detection logic __init__.py Site Handlers ------------- diff --git a/pym/euscan/handlers/__init__.py b/pym/euscan/handlers/__init__.py index 0008535..ed5db31 100644 --- a/pym/euscan/handlers/__init__.py +++ b/pym/euscan/handlers/__init__.py @@ -1,6 +1,6 @@ -from euscan.handlers import generic, php, pypi, rubygem, kde +from euscan.handlers import generic, php, pypi, rubygem, kde, cpan -handlers = [ kde, php, pypi, rubygem, generic ] +handlers = [ kde, php, pypi, rubygem, cpan, generic ] def find_best_handler(cpv, url): for handler in handlers: diff --git a/pym/euscan/handlers/cpan.py b/pym/euscan/handlers/cpan.py new file mode 100644 index 0000000..8eb85b3 --- /dev/null +++ b/pym/euscan/handlers/cpan.py @@ -0,0 +1,75 @@ +import re +import portage +import urllib2 +import json + +from euscan import helpers +import euscan + +_cpan_package_name_re = re.compile("mirror://cpan/authors/.*/([^/.]*).*") + +def can_handle(cpv, url): + return url.startswith('mirror://cpan/') + +def guess_package(cp, url): + match = _cpan_package_name_re.search(url) + + pkg = None + + if match: + pkg = match.group(1) + try: + cp, ver, rev = portage.pkgsplit('fake/' + pkg) + except: + pass + + cat, pkg = cp.split("/") + + return pkg + +def scan(cpv, url): + cp, ver, rev = portage.pkgsplit(cpv) + pkg = guess_package(cp, url) + + orig_url = url + url = 'http://search.cpan.org/api/dist/%s' % pkg + + euscan.output.einfo("Using: " + url) + + try: + fp = helpers.urlopen(url) + except urllib2.URLError: + return [] + except IOError: + return [] + + if not fp: + return [] + + data = fp.read() + data = json.loads(data) + + if 'releases' not in data: + return [] + + ret = [] + + for version in data['releases']: + up_pv = version['version'] + pv = helpers.gentoo_mangle_version(up_pv) + + if helpers.version_filtered(cp, ver, pv): + continue + + url = 'mirror://cpan/authors/id/%s/%s/%s/%s' % \ + (version['cpanid'][0], version['cpanid'][0:1], version['cpanid'], version['archive']) + + if url == orig_url: + continue + + ret.append(( url, pv )) + + return ret + +def brute_force(cpv, url): + return [] diff --git a/pym/euscan/handlers/php.py b/pym/euscan/handlers/php.py index 0dc2fb9..8919279 100644 --- a/pym/euscan/handlers/php.py +++ b/pym/euscan/handlers/php.py @@ -25,7 +25,8 @@ def guess_package_and_channel(cp, url): return pkg, host def scan(cpv, url): - pkg, channel = guess_package_and_channel(cpv, url) + cp, ver, rev = portage.pkgsplit(cpv) + pkg, channel = guess_package_and_channel(cp, url) orig_url = url url = 'http://%s/rest/r/%s/allreleases.xml' % (channel, pkg.lower()) @@ -49,8 +50,6 @@ def scan(cpv, url): nodes = dom.getElementsByTagName("v") ret = [] - cp, ver, rev = portage.pkgsplit(cpv) - for node in nodes: up_pv = node.childNodes[0].data pv = helpers.gentoo_mangle_version(up_pv)