Migrate LIBLITERALPREFIX test to file with same name
[scons.git] / bin / scons-unzip.py
blob412c56ae38eeda484c45d0eb8d69a8558440ceed
1 #!/usr/bin/env python
3 # A quick script to unzip a .zip archive and put the files in a
4 # subdirectory that matches the basename of the .zip file.
6 # This is actually generic functionality, it's not SCons-specific, but
7 # I'm using this to make it more convenient to manage working on multiple
8 # changes on Windows, where I don't have access to my Aegis tools.
10 import getopt
11 import os.path
12 import sys
13 import zipfile
15 helpstr = """\
16 Usage: scons-unzip.py [-o outdir] zipfile
17 Options:
18 -o DIR, --out DIR Change output directory name to DIR
19 -v, --verbose Print file names when extracting
20 """
22 opts, args = getopt.getopt(sys.argv[1:],
23 "o:v",
24 ['out=', 'verbose'])
26 outdir = None
27 printname = lambda x: x
29 for o, a in opts:
30 if o == '-o' or o == '--out':
31 outdir = a
32 elif o == '-v' or o == '--verbose':
33 def printname(x):
34 print(x)
36 if len(args) != 1:
37 sys.stderr.write("scons-unzip.py: \n")
38 sys.exit(1)
40 zf = zipfile.ZipFile(str(args[0]), 'r')
42 if outdir is None:
43 outdir, _ = os.path.splitext(os.path.basename(args[0]))
45 def outname(n, outdir=outdir):
46 l = []
47 while True:
48 n, tail = os.path.split(n)
49 if not n:
50 break
51 l.append(tail)
52 l.append(outdir)
53 l.reverse()
54 return os.path.join(*l)
56 for name in zf.namelist():
57 dest = outname(name)
58 dir = os.path.dirname(dest)
59 try:
60 os.makedirs(dir)
61 except:
62 pass
63 printname(dest)
64 # if the file exists, then delete it before writing
65 # to it so that we don't end up trying to write to a symlink:
66 if os.path.isfile(dest) or os.path.islink(dest):
67 os.unlink(dest)
68 if not os.path.isdir(dest):
69 open(dest, 'w').write(zf.read(name))
71 # Local Variables:
72 # tab-width:4
73 # indent-tabs-mode:nil
74 # End:
75 # vim: set expandtab tabstop=4 shiftwidth=4: