add cmd type to retrieve version info

This commit is contained in:
lilydjwg 2013-05-26 21:30:52 +08:00
parent 53e87b9ebe
commit 9497fca961
2 changed files with 49 additions and 10 deletions

View File

@ -2,12 +2,26 @@ import re
import sre_constants
import logging
from functools import partial
import queue
from pkg_resources import parse_version
from tornado.httpclient import AsyncHTTPClient
import tornado.process
from tornado.ioloop import IOLoop
handler_precedence = ('regex',)
logger = logging.getLogger(__name__)
handler_precedence = ('cmd', 'regex')
def get_version(name, conf, callback):
g = globals()
for key in handler_precedence:
if key in conf:
funcname = 'get_version_by_' + key
g[funcname](name, conf, callback)
break
else:
logger.error('%s: no idea to get version info.', name)
callback(name, None)
def get_version_by_regex(name, conf, callback):
try:
@ -33,13 +47,35 @@ def _get_version_by_regex(name, regex, encoding, callback, res):
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)
cmd_q = queue.Queue()
cmd_q.running = False
def get_version_by_cmd(name, conf, callback):
cmd = conf['cmd']
cmd_q.put((name, cmd, callback))
if not cmd_q.running:
_run_command()
def _run_command():
cmd_q.running = True
try:
name, cmd, callback = cmd_q.get_nowait()
except queue.Empty:
cmd_q.running = False
return
p = tornado.process.Subprocess(cmd, shell=True, io_loop=IOLoop.instance(),
stdout=tornado.process.Subprocess.STREAM)
p.set_exit_callback(partial(_command_done, name, callback, p))
def _command_done(name, callback, process, status):
if status != 0:
logger.error('%s: command exited with %d.', name, status)
callback(name, None)
else:
process.stdout.read_until_close(partial(_got_version_from_cmd, callback, name))
_run_command()
def _got_version_from_cmd(callback, name, output):
output = output.strip().decode('latin1')
callback(name, output)

View File

@ -9,3 +9,6 @@ regex = 7\.3\.\d+
; [badone]
; url = http://www.baidu.com/
; regex = baidu (\d+)
[google-chrome]
cmd = wget -qO- http://dl.google.com/linux/chrome/rpm/stable/x86_64/repodata/other.xml.gz | zgrep "google-chrome-stable" | awk -F\" '{print $10"-"$12}'