initial commit: works with regex pattern
This commit is contained in:
commit
ae66b40369
|
@ -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)
|
|
@ -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()
|
|
@ -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+)
|
Loading…
Reference in New Issue