add the nvtake command utility

This commit is contained in:
lilydjwg 2013-09-05 16:53:19 +08:00
parent fd99a076e9
commit b58e3f70e5
5 changed files with 66 additions and 21 deletions

View File

@ -40,6 +40,14 @@ Compare the two files for updates (assuming they are sorted alphabetically; file
# show both old and new versions
join old_ver.txt new_ver.txt | awk '$2 != $3'
The ``nvtake`` Command
----------------------
This command helps to manage version record files. It reads both old and new version record files, and a list of names given on the commandline. It then update the versions of those names in the old version record file.
This helps when you have known (and processed) some of the updated software, but not all. You can tell nvchecker that via this command instead of editing the file by hand.
This command will help most if you specify where you version record files are in your config file. See below for how to use a config file.
Version Source Files
====================
The software version source files are in ini format. *Section names* is the name of the software. Following fields are used to tell nvchecker how to determine the current version of that software.
@ -106,4 +114,4 @@ Bugs
TODO
====
* ``nvtake`` command
* Tool to replace the ``join`` command

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import sys
@ -43,23 +42,10 @@ def load_config(*files):
return config
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 args.newver:
return
with open(args.newver, 'w') as f:
# sort using only alphanums, as done by the sort command, and needed by
# comm command
for item in sorted(g_curver.items(), key=lambda i: (''.join(filter(str.isalnum, i[0])), i[1])):
print('%s: %s' % item, file=f)
util.write_verfile(args.newver, g_curver)
def print_version_update(name, version):
oldver = g_oldver.get(name, None)
@ -105,7 +91,7 @@ def main():
def run_test():
config = load_config(*args.files)
if args.oldver:
g_oldver.update(load_oldverfile(args.oldver))
g_oldver.update(util.read_verfile(args.oldver))
g_curver.update(g_oldver)
get_versions(config)

View File

@ -1,6 +1,27 @@
# vim:fileencoding=utf-8
# vim: se sw=2:
import sys
import argparse
from . import util
def take():
raise NotImplementedError
parser = argparse.ArgumentParser(description='update version records of nvchecker')
parser.add_argument('names', metavar='NAME', nargs='*',
help='software name to be updated')
util.add_common_arguments(parser)
args = util.parse_args(parser)
if util.process_common_arguments(args):
return
if not args.oldver or not args.newver:
sys.exit('You must specify old and new version records so that I can update.')
oldvers = util.read_verfile(args.oldver)
newvers = util.read_verfile(args.newver)
for name in args.names:
oldvers[name] = newvers[name]
util.write_verfile(args.oldver, oldvers)

View File

@ -1,3 +1,5 @@
# vim: se sw=2:
import os
import sys
import logging
@ -13,7 +15,7 @@ def add_common_arguments(parser):
help='read an existing version record file')
parser.add_argument('-o', '--newver',
help='write a new version record file')
parser.add_argument('-c', default=_DEFAULT_CONFIG,
parser.add_argument('-c', metavar='CONFIG_FILE', default=_DEFAULT_CONFIG,
help='specify the nvcheckerrc file to use')
parser.add_argument('-l', '--logging',
choices=('debug', 'info', 'warning', 'error'), default='info',
@ -52,5 +54,32 @@ def process_common_arguments(args):
nicelogger.enable_pretty_logging(getattr(logging, args.logging.upper()))
if args.version:
print('nvchecker v' + __version__)
progname = os.path.basename(sys.argv[0])
print('%s v%s' % (progname, __version__))
return True
def safe_overwrite(fname, data, *, method='write', mode='w', encoding=None):
# FIXME: directory has no read perm
# FIXME: symlinks and hard links
tmpname = fname + '.tmp'
# if not using "with", write can fail without exception
with open(tmpname, mode, encoding=encoding) as f:
getattr(f, method)(data)
# if the above write failed (because disk is full etc), the old data should be kept
os.rename(tmpname, fname)
def read_verfile(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(file, versions):
# sort using only alphanums, as done by the sort command, and needed by
# comm command
data = ['%s: %s\n' % item
for item in sorted(versions.items(), key=lambda i: (''.join(filter(str.isalnum, i[0])), i[1]))]
safe_overwrite(file, data, method='writelines')

View File

@ -12,6 +12,7 @@ setup(
entry_points = {
'console_scripts': [
'nvchecker = nvchecker.main:main',
'nvtake = nvchecker.tools:take',
],
},