PR: add pyalpm.vercmp as alternative to parse_version

`pkg_resources.parse_version` has the different result from
`pyalpm.vercmp`. This change to github repos allows to use
`pyalpm.vercmp` as sort key.

This is just a PR for review comments. If this PR is accepted,
I will further implement this option in other repos and core.
This commit is contained in:
Jiachen Yang 2016-04-02 23:13:41 +09:00
parent 9df0bb5433
commit fac1c14916
2 changed files with 32 additions and 3 deletions

View file

@ -180,6 +180,11 @@ ignored_tags
be useful to avoid some known badly versioned tags, so the newer tags won't
be "overridden" by the old broken ones.
sort_version_key
Sort the version string using this key function. Choose between ``parse_version`` and
``vercmp``. Default value is ``parse_version``. ``parse_version`` use
``pkg_resources.parse_version``. ``vercmp`` use ``pyalpm.vercmp``.
An environment variable ``NVCHECKER_GITHUB_TOKEN`` can be set to a GitHub OAuth token in order to request more frequently than anonymously.
Check BitBucket

View file

@ -5,6 +5,29 @@ from functools import partial
from pkg_resources import parse_version
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
try:
import pyalpm
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
vercmp = cmp_to_key(pyalpm.vercmp)
except ImportError:
vercmp = None
GITHUB_URL = 'https://api.github.com/repos/%s/commits?sha=%s'
GITHUB_LATEST_RELEASE = 'https://api.github.com/repos/%s/releases/latest'
GITHUB_MAX_TAG = 'https://api.github.com/repos/%s/tags'
@ -15,6 +38,7 @@ def get_version(name, conf, callback):
use_latest_release = conf.getboolean('use_latest_release', False)
use_max_tag = conf.getboolean('use_max_tag', False)
ignored_tags = conf.get("ignored_tags", "").split()
sort_version_key = {"parse_version": parse_version, "vercmp": vercmp}[conf.get("sort_version_key", "parse_version")]
if use_latest_release:
url = GITHUB_LATEST_RELEASE % repo
elif use_max_tag:
@ -26,15 +50,15 @@ def get_version(name, conf, callback):
headers['Authorization'] = 'token %s' % os.environ['NVCHECKER_GITHUB_TOKEN']
request = HTTPRequest(url, headers=headers, user_agent='lilydjwg/nvchecker')
AsyncHTTPClient().fetch(request,
callback=partial(_github_done, name, use_latest_release, use_max_tag, ignored_tags, callback))
callback=partial(_github_done, name, use_latest_release, use_max_tag, ignored_tags, sort_version_key, callback))
def _github_done(name, use_latest_release, use_max_tag, ignored_tags, callback, res):
def _github_done(name, use_latest_release, use_max_tag, ignored_tags, sort_version_key, callback, res):
data = json.loads(res.body.decode('utf-8'))
if use_latest_release:
version = data['tag_name']
elif use_max_tag:
data = [tag["name"] for tag in data if tag["name"] not in ignored_tags]
data.sort(key=parse_version)
data.sort(key=sort_version_key)
version = data[-1]
else:
# YYYYMMDD.HHMMSS