2012-04-20 17:27:16 +02:00
|
|
|
import re
|
|
|
|
import portage
|
|
|
|
import urllib2
|
|
|
|
import json
|
|
|
|
|
2012-05-23 16:30:43 +02:00
|
|
|
from euscan import helpers, output
|
|
|
|
|
|
|
|
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-04-28 18:16:05 +02:00
|
|
|
|
2012-07-24 08:56:00 +02:00
|
|
|
def can_handle(pkg, url):
|
2012-04-20 17:27:16 +02:00
|
|
|
return 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-04-23 17:05:23 +02:00
|
|
|
def gentoo_mangle_version(up_pv):
|
|
|
|
pv = ""
|
|
|
|
|
|
|
|
if up_pv.count('.') == 1:
|
|
|
|
digits = 0
|
|
|
|
for i in range(len(up_pv)):
|
|
|
|
if digits == 3:
|
|
|
|
pv += "."
|
|
|
|
digits = 0
|
|
|
|
c = up_pv[i]
|
|
|
|
pv += c
|
|
|
|
digits += int(c.isdigit())
|
2012-04-28 18:16:05 +02:00
|
|
|
if c == '.':
|
|
|
|
digits = 0
|
2012-04-23 17:05:23 +02:00
|
|
|
else:
|
|
|
|
pv = up_pv
|
|
|
|
|
|
|
|
return helpers.gentoo_mangle_version(pv)
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-04-23 17:05:23 +02:00
|
|
|
def cpan_trim_version(pv):
|
|
|
|
pv = re.sub('^[a-zA-Z]+', '', pv)
|
|
|
|
pv = re.sub('[a-zA-Z]$', '', pv)
|
|
|
|
return pv
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-04-23 17:05:23 +02:00
|
|
|
def cpan_mangle_version(pv):
|
|
|
|
pos = pv.find('.')
|
|
|
|
if pos < 0:
|
|
|
|
return pv
|
|
|
|
up_pv = pv.replace('.', '')
|
|
|
|
up_pv = up_pv[0:pos] + '.' + up_pv[pos:]
|
|
|
|
up_pv = cpan_trim_version(up_pv)
|
|
|
|
return up_pv
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-04-23 17:05:23 +02:00
|
|
|
def cpan_vercmp(cp, a, b):
|
|
|
|
try:
|
|
|
|
return float(a) - float(b)
|
|
|
|
except:
|
|
|
|
if a < b:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2012-07-24 08:56:00 +02:00
|
|
|
def scan(pkg, url):
|
|
|
|
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-07-26 17:46:53 +02:00
|
|
|
result = scan_remote(pkg, [remote_pkg])
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for url, pv in result:
|
|
|
|
ret.append((url, pv, HANDLER_NAME, CONFIDENCE))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def scan_remote(pkg, remote_data):
|
|
|
|
remote_pkg = remote_data[0]
|
|
|
|
url = 'http://search.cpan.org/api/dist/%s' % remote_pkg
|
|
|
|
cp, ver, rev = portage.pkgsplit(pkg.cpv)
|
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-04-23 17:05:23 +02:00
|
|
|
up_pv = cpan_trim_version(up_pv)
|
|
|
|
pv = gentoo_mangle_version(up_pv)
|
|
|
|
up_ver = cpan_mangle_version(ver)
|
2012-04-20 17:27:16 +02:00
|
|
|
|
2012-04-23 17:05:23 +02:00
|
|
|
if helpers.version_filtered(cp, up_ver, up_pv, cpan_vercmp):
|
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-07-26 17:46:53 +02:00
|
|
|
ret.append((url, pv))
|
2012-04-20 17:27:16 +02:00
|
|
|
|
|
|
|
return ret
|