From 7e5113cfc7cf3ff51ffb8bb9191c8fb4a0251d9e Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 1 Jun 2009 12:29:10 +0200 Subject: add xz compression and file exists check --- a | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'a') diff --git a/a b/a index e48ce2b..ccb8275 100755 --- a/a +++ b/a @@ -1,6 +1,6 @@ #!/usr/bin/python #---------------------------------------------------- -# Version: 0.2.0 +# Version: 0.2.1 # Author: Florian "Bluewind" Pritz # # Licensed under WTFPL v2 @@ -12,6 +12,7 @@ import sys import tarfile +import os from optparse import OptionParser def main(): @@ -19,10 +20,14 @@ def main(): p = OptionParser(usage) p.add_option("-f", "--file", dest="tarname", default=False, help="use .tar.gz instead of $1.tar.gz", metavar="") + p.add_option("--force", action="store_true", dest="force", default=False, + help="overwrite existing target files") p.add_option("-b", "--bzip2", action="store_true", dest="bz2", default=False, help="use bzip2 compression") p.add_option("-u", "--uncompressed", action="store_true", dest="uncompressed", default=False, help="don't use compression at all") + p.add_option("-x", "--xz", action="store_true", dest="xz", default=False, + help="use xz compression") (options, args) = p.parse_args() @@ -38,12 +43,15 @@ def main(): if options.bz2: tarname += ".tar.bz2" + file_exists(tarname, options) tar = tarfile.open(tarname, "w|bz2") - elif options.uncompressed: + elif options.uncompressed or options.xz: tarname += ".tar" + file_exists(tarname, options) tar = tarfile.open(tarname, "w") else: tarname += ".tar.gz" + file_exists(tarname, options) tar = tarfile.open(tarname, "w|gz") for name in args: @@ -52,6 +60,13 @@ def main(): except OSError: sys.stderr.write("No such file or directory: '%s'\n" % name) tar.close() + if options.xz: + os.system('xz '+tarname) + +def file_exists(filename, options): + if os.path.exists(filename) and not options.force: + sys.stderr.write('Target "'+filename+'" already exists. Use --force to overwrite.\n') + sys.exit(1) if __name__ == '__main__': main() -- cgit v1.2.3-24-g4f1b