utility for install in zipped distutils packages

C

Craig D

I got really tired of unpacking a distutils package, installing then
cleaning up. So I wrote distinstall.py - it reads a zip file, unpacks
it, installs the bits and cleans up. I've only used it on windows and
Linux and for pure Python packages.
Windows: distinstall.py Photronics.zip
Linux: python distinstall.py Photronics.zip

Hopefully somebody else will find it useful.

#======================== distinstall.py =====================
#!/usr/bin/env python
'''Install a zipped up distutils package'''

import zipfile
import sys, os, os.path

def usage():
print "distinstall.py pack.zip: Install a zipped distutil python
package"
print "One arg: name of the zip package"
print "THIS PROGRAM ONLY WORKS WITH ZIPPED PURE PYTHON PACKAGES!"
sys.exit(1)

def nuke(top):
# Delete everything reachable from the directory named in 'top'.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(top)


def explodeZipFile(zippedPackageFileName):
if not zipfile.is_zipfile(zippedPackageFileName):
print '"%s" is not a zip file!' % zippedPackageFileName
usage()
zip = zipfile.ZipFile(zippedPackageFileName,'r')
if not filter(lambda n:"setup.py" in n, zip.namelist()):
print "setup.py isn't in the zip file! Not a distutil package?"
usage()

tmp = os.getenv('TMP') or os.getenv('TMPDIR')
if (not tmp) and os.path.isdir('/tmp'): tmp = '/tmp'
if not tmp:
print 'Can\'t find the tmp directory!'
sys.exit(1)

path,fileName = os.path.split(zippedPackageFileName)
tmpDir = os.path.join(tmp,os.path.splitext(fileName)[0])

print "unzip %s to %s" % (zippedPackageFileName,tmpDir)
for z in zip.namelist():
# print z
data = zip.read(z)
try:
os.makedirs(os.path.join(tmp,os.path.split(z)[0]),0777)
except: pass
f = file(os.path.join(tmp,z),'w') #?? what's up with wb?
f.writelines(data.split('\r')) # this really sucks
f.close()

zip.close()
setup = os.path.join(tmpDir,"setup.py")
return tmpDir,setup


if len(sys.argv) < 2: usage()

cwd = os.getcwd()
zippedPackageFileName = sys.argv[1]
tmpDir,setup = explodeZipFile(zippedPackageFileName)

print "====================Install================="
os.chdir(tmpDir)
install = "%s setup.py install --force" % sys.executable
print install
os.system(install)

os.chdir(cwd)
print "\nClean up %s" % tmpDir
nuke(tmpDir) # this will clean up the build directory
#======================== distinstall.py =====================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top