From b58e3f70e5dd18440738ac94c8653ff1373e0696 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Thu, 5 Sep 2013 16:53:19 +0800 Subject: [PATCH] add the nvtake command utility --- README.rst | 10 +++++++++- nvchecker/main.py | 18 ++---------------- nvchecker/tools.py | 25 +++++++++++++++++++++++-- nvchecker/util.py | 33 +++++++++++++++++++++++++++++++-- setup.py | 1 + 5 files changed, 66 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index 301bc8f..ab5f6ca 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/nvchecker/main.py b/nvchecker/main.py index 363e771..42e9ef1 100755 --- a/nvchecker/main.py +++ b/nvchecker/main.py @@ -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) diff --git a/nvchecker/tools.py b/nvchecker/tools.py index 2dfa97f..ea3f240 100644 --- a/nvchecker/tools.py +++ b/nvchecker/tools.py @@ -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) diff --git a/nvchecker/util.py b/nvchecker/util.py index 4d95cee..d3303cd 100644 --- a/nvchecker/util.py +++ b/nvchecker/util.py @@ -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') + diff --git a/setup.py b/setup.py index 67c76cf..bede8ac 100755 --- a/setup.py +++ b/setup.py @@ -12,6 +12,7 @@ setup( entry_points = { 'console_scripts': [ 'nvchecker = nvchecker.main:main', + 'nvtake = nvchecker.tools:take', ], },