commit ae66b4036977482743c0e91cc1bc7228bdbc44ee Author: lilydjwg Date: Sun May 26 14:58:00 2013 +0800 initial commit: works with regex pattern diff --git a/get_version.py b/get_version.py new file mode 100644 index 0000000..f9799f9 --- /dev/null +++ b/get_version.py @@ -0,0 +1,45 @@ +import re +import sre_constants +import logging +from functools import partial + +from pkg_resources import parse_version +from tornado.httpclient import AsyncHTTPClient + +handler_precedence = ('regex',) +logger = logging.getLogger(__name__) + +def get_version_by_regex(name, conf, callback): + try: + r = re.compile(conf['regex']) + except sre_constants.error: + logger.warn('%s: bad regex, skipped.', name, exc_info=True) + callback(name, None) + return + + encoding = conf.get('encoding', 'latin1') + httpclient = AsyncHTTPClient() + httpclient.fetch(conf['url'], partial( + _get_version_by_regex, name, r, encoding, callback + )) + +def _get_version_by_regex(name, regex, encoding, callback, res): + body = res.body.decode(encoding) + try: + version = max(regex.findall(body), key=parse_version) + except ValueError: + logger.error('%s: version string not found.', name) + callback(name, None) + else: + callback(name, version) + +def get_version(name, conf, callback): + g = globals() + for key in handler_precedence: + funcname = 'get_version_by_' + key + if funcname in g: + g[funcname](name, conf, callback) + break + else: + logger.error('%s: no idea to get version info.', name) + callback(name, None) diff --git a/nvchecker b/nvchecker new file mode 100755 index 0000000..287440d --- /dev/null +++ b/nvchecker @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# vim:fileencoding=utf-8 + +import sys +import configparser +import logging +from functools import partial + +from tornado.ioloop import IOLoop +import tornado.options + +from get_version import get_version + +logger = logging.getLogger(__name__) +g_counter = 0 + +def task_inc(): + global g_counter + g_counter += 1 + +def task_dec(): + global g_counter + g_counter -= 1 + if g_counter == 0: + IOLoop.instance().stop() + +def load_config(*files): + config = configparser.ConfigParser( + dict_type=dict, allow_no_value=True + ) + for file in files: + with open(file) as f: + config.read_file(f) + + return config + +def print_version(name, version): + print('%s: %s' % (name, version)) + task_dec() + +def get_versions(config): + task_inc() + for name in config.sections(): + task_inc() + get_version(name, config[name], print_version) + task_dec() + +def test(): + files = tornado.options.parse_command_line() + + def run_test(): + config = load_config(*files) + get_versions(config) + + ioloop = IOLoop.instance() + ioloop.add_callback(run_test) + ioloop.start() + +if __name__ == '__main__': + test() diff --git a/sample_config.ini b/sample_config.ini new file mode 100644 index 0000000..cfe852c --- /dev/null +++ b/sample_config.ini @@ -0,0 +1,11 @@ +[fcitx] +url = https://code.google.com/p/fcitx/ +regex = fcitx-([\d.]+)\.tar\.xz + +[vim] +url = http://ftp.vim.org/pub/vim/patches/7.3/ +regex = 7\.3\.\d+ + +; [badone] +; url = http://www.baidu.com/ +; regex = baidu (\d+)