check against an "oldverfile" for updates and write to a new one

This commit is contained in:
lilydjwg 2013-05-26 22:59:05 +08:00
parent 6a04a0c5d6
commit a5aad83a51
1 changed files with 51 additions and 9 deletions

View File

@ -8,15 +8,25 @@ import logging
from functools import partial
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))
from pkg_resources import parse_version
from tornado.ioloop import IOLoop
import tornado.options
from tornado.options import parse_command_line, define, options
from get_version import get_version
import notify
logger = logging.getLogger(__name__)
g_counter = 0
notifications = []
g_counter = 0
g_oldver = {}
g_curver = {}
define("notify", type=bool,
help="show desktop notifications when a new version is available")
define("oldverfile", type=str, metavar="FILE",
help="a text file listing current version info in format 'name: version'")
define("verfile", type=str, metavar="FILE",
help="write a new version file")
def task_inc():
global g_counter
@ -27,6 +37,7 @@ def task_dec():
g_counter -= 1
if g_counter == 0:
IOLoop.instance().stop()
write_verfile()
def load_config(*files):
config = configparser.ConfigParser(
@ -38,25 +49,56 @@ def load_config(*files):
return config
def print_version(name, version):
print('%s: %s' % (name, version))
msg = '%s lastest version: %s' % (name, version)
notifications.append(msg)
notify.update('nvchecker', '\n'.join(notifications))
def load_oldverfile(file):
v = {}
with open(file) as f:
for l in f:
name, ver = [x.strip() for x in l.split(':', 1)]
v[name] = ver
return v
def write_verfile():
if not options.verfile:
return
with open(options.verfile, 'w') as f:
for item in sorted(g_curver.items()):
print('%s: %s' % item, file=f)
def print_version_update(name, version):
oldver = g_oldver.get(name, None)
if not oldver or parse_version(oldver) < parse_version(version):
logger.info('%s: updated version %s', name, version)
_updated(name, version)
else:
logger.info('%s: current version %s', name, version)
task_dec()
def _updated(name, version):
g_curver[name] = version
if options.notify:
msg = '%s updated to version %s' % (name, version)
notifications.append(msg)
notify.update('nvchecker', '\n'.join(notifications))
def get_versions(config):
task_inc()
for name in config.sections():
task_inc()
get_version(name, config[name], print_version)
get_version(name, config[name], print_version_update)
task_dec()
def test():
files = tornado.options.parse_command_line()
files = parse_command_line()
if not files:
return
def run_test():
config = load_config(*files)
if options.oldverfile:
g_oldver.update(load_oldverfile(options.oldverfile))
g_curver.update(g_oldver)
get_versions(config)
ioloop = IOLoop.instance()