2012-04-20 17:27:16 +02:00
|
|
|
import re
|
|
|
|
import portage
|
|
|
|
import urllib2
|
|
|
|
import json
|
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
from euscan import helpers, output, mangling
|
2012-05-23 16:30:43 +02:00
|
|
|
|
|
|
|
HANDLER_NAME = "cpan"
|
2012-07-27 11:26:54 +02:00
|
|
|
CONFIDENCE = 100
|
2012-07-24 15:02:36 +02:00
|
|
|
PRIORITY = 90
|
2012-04-20 17:27:16 +02:00
|
|
|
|
|
|
|
_cpan_package_name_re = re.compile("mirror://cpan/authors/.*/([^/.]*).*")
|
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
def can_handle(pkg, url=None):
|
|
|
|
return url and url.startswith('mirror://cpan/')
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-04-20 17:27:16 +02:00
|
|
|
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
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
def mangle_version(up_pv):
|
2012-07-31 09:50:11 +02:00
|
|
|
# clean
|
|
|
|
up_pv = up_pv.replace("._", "_") # e.g.: 0.999._002 -> 0.999_002
|
|
|
|
up_pv = up_pv.replace("_0.", "_") # e.g.: 0.30_0.1 -> 0.30_1
|
|
|
|
|
|
|
|
# Detect _rc versions
|
|
|
|
rc_part = ""
|
|
|
|
if up_pv.count("_") == 1:
|
|
|
|
up_pv, rc_part = up_pv.split("_")
|
|
|
|
|
|
|
|
# Gentoo creates groups of 3 digits, except for the first digit,
|
|
|
|
# or when last digit is 0. e.g.: 4.11 -> 4.110.0
|
|
|
|
splitted = up_pv.split(".")
|
|
|
|
if rc_part:
|
|
|
|
splitted.append(rc_part)
|
|
|
|
|
|
|
|
if len(splitted) == 2: # add last group if it's missing
|
|
|
|
splitted.append("0")
|
|
|
|
|
|
|
|
groups = [splitted[0]]
|
|
|
|
for part in splitted[1:-1]:
|
|
|
|
groups.append(part.ljust(3, "0"))
|
|
|
|
if splitted[-1] == "0":
|
|
|
|
groups.append(splitted[-1])
|
2012-04-23 17:05:23 +02:00
|
|
|
else:
|
2012-07-31 09:50:11 +02:00
|
|
|
groups.append(splitted[-1].ljust(3, "0"))
|
|
|
|
|
|
|
|
# if there's a group with leading zeros strip it. e.g.: 002 -> 2
|
|
|
|
groups = [g.lstrip("0") if g != "0" else g for g in groups]
|
|
|
|
|
|
|
|
pv = ".".join(groups)
|
|
|
|
|
|
|
|
if rc_part:
|
|
|
|
pv = "%s_rc" % pv
|
2012-04-23 17:05:23 +02:00
|
|
|
|
|
|
|
return pv
|
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
def scan_url(pkg, url, options):
|
2012-07-24 08:56:00 +02:00
|
|
|
cp, ver, rev = portage.pkgsplit(pkg.cpv)
|
2012-07-26 17:46:53 +02:00
|
|
|
remote_pkg = guess_package(cp, url)
|
2012-04-20 17:27:16 +02:00
|
|
|
|
2012-07-26 17:46:53 +02:00
|
|
|
output.einfo("Using CPAN API: %s", remote_pkg)
|
2012-04-20 17:27:16 +02:00
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
return scan_pkg(pkg, {'data' : remote_pkg})
|
2012-07-26 17:46:53 +02:00
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
def scan_pkg(pkg, options):
|
|
|
|
remote_pkg = options['data']
|
2012-07-26 17:46:53 +02:00
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
# Defaults to CPAN mangling rules
|
|
|
|
if 'versionmangle' not in options:
|
|
|
|
options['versionmangle'] = ['cpan', 'gentoo']
|
2012-07-26 17:46:53 +02:00
|
|
|
|
|
|
|
url = 'http://search.cpan.org/api/dist/%s' % remote_pkg
|
2012-08-03 21:50:54 +02:00
|
|
|
cp, ver, rev = pkg.cp, pkg.version, pkg.revision
|
2012-04-20 17:27:16 +02:00
|
|
|
|
|
|
|
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']:
|
2012-04-23 17:05:23 +02:00
|
|
|
#if version['status'] == 'testing':
|
|
|
|
# continue
|
|
|
|
|
2012-04-20 17:27:16 +02:00
|
|
|
up_pv = version['version']
|
2012-08-03 21:50:54 +02:00
|
|
|
pv = mangling.mangle_version(up_pv, options)
|
2012-04-20 17:27:16 +02:00
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
if helpers.version_filtered(cp, ver, pv):
|
2012-04-20 17:27:16 +02:00
|
|
|
continue
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
url = 'mirror://cpan/authors/id/%s/%s/%s/%s' % (
|
|
|
|
version['cpanid'][0],
|
|
|
|
version['cpanid'][0:1],
|
|
|
|
version['cpanid'],
|
|
|
|
version['archive']
|
|
|
|
)
|
2012-04-20 17:27:16 +02:00
|
|
|
|
2012-08-03 21:50:54 +02:00
|
|
|
url = mangling.mangle_url(url, options)
|
|
|
|
ret.append((url, pv, HANDLER_NAME, CONFIDENCE))
|
2012-04-20 17:27:16 +02:00
|
|
|
|
|
|
|
return ret
|