summaryrefslogtreecommitdiffstats
path: root/a
blob: ccb8275030b0ca11297fac0c717c35adb4d535ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
#----------------------------------------------------
# Version:    0.2.1
# Author:   Florian "Bluewind" Pritz <f-p@gmx.at>
#
# Licensed under WTFPL v2
# (see COPYING for full license text)
#
#----------------------------------------------------
# got bored and a.sh has some problems with spaces
#----------------------------------------------------

import sys
import tarfile
import os
from optparse import OptionParser

def main():
  usage = "usage: %prog [options] <files>"
  p = OptionParser(usage)
  p.add_option("-f", "--file", dest="tarname", default=False,
      help="use <file>.tar.gz instead of $1.tar.gz", metavar="<file>")
  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()


  if len(sys.argv) == 1:
    p.print_help()
    sys.exit()

  if options.tarname:
    tarname = options.tarname
  else:
    tarname = args[0]

  if options.bz2:
    tarname += ".tar.bz2"
    file_exists(tarname, options)
    tar = tarfile.open(tarname, "w|bz2")
  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:
    try:
      tar.add(name)
    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()