#!/usr/bin/env python## A quick script to unzip a .zip archive and put the files in a# subdirectory that matches the basename of the .zip file.## This is actually generic functionality, it's not SCons-specific, but# I'm using this to make it more convenient to manage working on multiple# changes on Windows, where I don't have access to my Aegis tools.#importgetoptimportos.pathimportsysimportzipfilehelpstr="""\Usage: scons-unzip.py [-o outdir] zipfileOptions: -o DIR, --out DIR Change output directory name to DIR -v, --verbose Print file names when extracting"""opts,args=getopt.getopt(sys.argv[1:],"o:v",['out=','verbose'])outdir=Noneprintname=lambdax:xforo,ainopts:ifo=='-o'oro=='--out':outdir=aelifo=='-v'oro=='--verbose':defprintname(x):printxiflen(args)!=1:sys.stderr.write("scons-unzip.py: \n")sys.exit(1)zf=zipfile.ZipFile(str(args[0]),'r')ifoutdirisNone:outdir,_=os.path.splitext(os.path.basename(args[0]))defoutname(n,outdir=outdir):l=[]whileTrue:n,tail=os.path.split(n)ifnotn:breakl.append(tail)l.append(outdir)l.reverse()returnos.path.join(*l)fornameinzf.namelist():dest=outname(name)dir=os.path.dirname(dest)try:os.makedirs(dir)except:passprintname(dest)# if the file exists, then delete it before writing# to it so that we don't end up trying to write to a symlink:ifos.path.isfile(dest)oros.path.islink(dest):os.unlink(dest)ifnotos.path.isdir(dest):open(dest,'w').write(zf.read(name))# Local Variables:# tab-width:4# indent-tabs-mode:nil# End:# vim: set expandtab tabstop=4 shiftwidth=4: