[Assorted-commits] SF.net SVN: assorted:[1375] shell-tools/trunk/src/clobber-if-diff.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-05-07 05:34:06
|
Revision: 1375 http://assorted.svn.sourceforge.net/assorted/?rev=1375&view=rev Author: yangzhang Date: 2009-05-07 05:34:05 +0000 (Thu, 07 May 2009) Log Message: ----------- - added usage, --copy, --remove - handle arg error cases - fixed a few bugs Modified Paths: -------------- shell-tools/trunk/src/clobber-if-diff.py Modified: shell-tools/trunk/src/clobber-if-diff.py =================================================================== --- shell-tools/trunk/src/clobber-if-diff.py 2009-05-07 03:01:26 UTC (rev 1374) +++ shell-tools/trunk/src/clobber-if-diff.py 2009-05-07 05:34:05 UTC (rev 1375) @@ -4,43 +4,68 @@ # TODO: use SpooledTemporaryFile, new in Python 2.6 from __future__ import with_statement -import sys, optparse, subprocess, shutil, commons.startup, os.path +import sys, optparse, subprocess, shutil, commons.startup, os, os.path -def foo(inpath, outpath, opts): +def handle_input_file(inpath, outpath, opts): + '''This is called when the inpath is a normal existing file.''' if os.path.isfile(outpath): if 0 == subprocess.call(['cmp', '-s', inpath, outpath]): return opts.exitcode - shutil.move(inpath, outpath) + if opts.remove: os.remove(outpath) + if opts.copy: shutil.copy(inpath, outpath) + else: shutil.move(inpath, outpath) return 0 def main(argv): - parser = optparse.OptionParser() + + # + # Command-line options. + # + + parser = optparse.OptionParser( + usage = '%prog [OPTIONS] [INPATH] OUTPATH') + parser.add_option('-b', '--buffer', type = 'int', default = 1<<20, help = 'maximum buffer size in bytes, if reading from stdin') + parser.add_option('-c', '--copy', action = 'store_true', + help = "if reading a file, leave orig in place (don't move)") parser.add_option('-t', '--tempfile', type = 'string', default = None, - help = '''path to temporary file where the candidate is to be written, if - reading from stdin; if unspecified, then generate tmp file name''') + help = '''path to temporary file where the candidate is to be written, + if reading from stdin; if unspecified, then generate tmp file name''') parser.add_option('-x', '--exit-code', dest = 'exitcode', type = 'int', default = 0, help = 'exit status if clobbering did not happen') - #parser.add_option('-d', '--debug', action = 'store_true', - #help = 'debug output') - opts, [cmd, inpath, outpath] = parser.parse_args(argv) + parser.add_option('-r', '--remove', action = 'store_true', + help = 'if reading stdin, rm the old file when clobbering') - if os.path.isfile(inpath): - return foo(inpath, outpath, opts) + opts, args = parser.parse_args(argv) + + if len(args) == 3: [cmd, inpath, outpath] = args + elif len(args) == 2: [cmd, outpath] = args; inpath = '-' + else: parser.error('bad number of args; specify OUTPATH or INPATH OUTPATH') + + # + # Perform the operation. + # + + if inpath != '-': + return handle_input_file(inpath, outpath, opts) else: new = sys.stdin.read(opts.buffer) if len(new) == opts.buffer: - f = tempfile.TemporaryFile() if opts.tempfile is None else file(opts.tempfile, 'w') + f = ( tempfile.TemporaryFile() if opts.tempfile is None else + file(opts.tempfile, 'w') ) with f: f.write(new) while len(buf) == opts.buffer: buf = sys.stdin.read(opts.buffer) f.write(buf) - return foo(f.name if opts.tempfile is None else opts.tempfile, outpath, opts) + return handle_input_file(f.name if opts.tempfile is None else + opts.tempfile, outpath, opts) else: - with file(outpath) as f: old = f.read(len(new) + 1) - if old == new: return opts.exitcode + if os.path.isfile(outpath): + with file(outpath) as f: old = f.read(len(new) + 1) + if old == new: return opts.exitcode + if opts.remove: os.remove(outpath) with file(outpath, 'w') as f: f.write(new) commons.startup.run_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |