From 2b5f2d190d0502932b5dc9a8641c5833bda43f55 Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Tue, 4 Jul 2017 01:14:35 +0800 Subject: [PATCH] Add Debian package source --- README.rst | 13 ++++++++++++ nvchecker/get_version.py | 2 +- nvchecker/source/debianpkg.py | 39 +++++++++++++++++++++++++++++++++++ tests/test_debianpkg.py | 16 ++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 nvchecker/source/debianpkg.py create mode 100644 tests/test_debianpkg.py diff --git a/README.rst b/README.rst index 3aaa936..369f7d6 100644 --- a/README.rst +++ b/README.rst @@ -317,6 +317,19 @@ archpkg strip-release Strip the release part. +Check Debian Linux official packages +---------------------------------- +This enables you to track the update of `Debian Linux official packages `_, without needing of apt and an updated local APT database. + +debianpkg + Name of the Debian Linux source package. + +suite + Name of the Debian release (jessie, wheezy, etc, defaults to sid) + +strip-release + Strip the release part. + Check Google Code (hg repository) --------------------------------- Check a mercurial (hg) repository on `Google Code `_ for updates. The version returned is in date format ``%Y%m%d``, e.g. ``20130701``. diff --git a/nvchecker/get_version.py b/nvchecker/get_version.py index ad41e61..666ab69 100644 --- a/nvchecker/get_version.py +++ b/nvchecker/get_version.py @@ -6,7 +6,7 @@ from importlib import import_module logger = logging.getLogger(__name__) handler_precedence = ( - 'github', 'aur', 'pypi', 'archpkg', 'gems', 'pacman', + 'github', 'aur', 'pypi', 'archpkg', 'debianpkg', 'gems', 'pacman', 'cmd', 'bitbucket', 'gcode_hg', 'gcode_svn', 'regex', 'manual', 'vcs', 'cratesio', 'npm', 'hackage', 'cpan', 'gitlab', 'packagist' ) diff --git a/nvchecker/source/debianpkg.py b/nvchecker/source/debianpkg.py new file mode 100644 index 0000000..ac4d15c --- /dev/null +++ b/nvchecker/source/debianpkg.py @@ -0,0 +1,39 @@ +# MIT licensed +# Copyright (c) 2017 Felix Yan , et al. + +from functools import partial +import logging +import json + +from tornado.httpclient import AsyncHTTPClient + +logger = logging.getLogger(__name__) + +URL = 'https://sources.debian.net/api/src/%(pkgname)s/?suite=%(suite)s' + +def get_version(name, conf, callback): + pkg = conf.get('debianpkg') or name + strip_release = conf.getboolean('strip-release', False) + suite = conf.get('suite') or "sid" + url = URL % {"pkgname": pkg, "suite": suite} + AsyncHTTPClient().fetch( + url, partial(_pkg_done, name, strip_release, callback)) + +def _pkg_done(name, strip_release, callback, res): + if res.error: + raise res.error + + data = json.loads(res.body.decode('utf-8')) + + if not data.get('versions'): + logger.error('Debian package not found: %s', name) + callback(name, None) + return + + r = data['versions'][0] + if strip_release: + version = r['version'].split("-")[0] + else: + version = r['version'] + + callback(name, version) diff --git a/tests/test_debianpkg.py b/tests/test_debianpkg.py new file mode 100644 index 0000000..e4b420a --- /dev/null +++ b/tests/test_debianpkg.py @@ -0,0 +1,16 @@ +# MIT licensed +# Copyright (c) 2017 Felix Yan , et al. + +import os + +from tests.helper import ExternalVersionTestCase + +class DebianPKGTest(ExternalVersionTestCase): + def test_debianpkg(self): + self.assertEqual(self.sync_get_version("sigrok-firmware-fx2lafw", {"debianpkg": None}), "0.1.3-1") + + def test_debianpkg_strip_release(self): + self.assertEqual(self.sync_get_version("sigrok-firmware-fx2lafw", {"debianpkg": None, "strip-release": 1}), "0.1.3") + + def test_debianpkg_suite(self): + self.assertEqual(self.sync_get_version("sigrok-firmware-fx2lafw", {"debianpkg": None, "suite": "jessie"}), "0.1.2-1")