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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
"""Bootstrap module to download/quasi-install 'setuptools' package
Usage::
from setuptools_boot import require_version
require_version('0.0.1')
from setuptools import setup, Extension, ...
Note that if a suitable version of 'setuptools' is not found on 'sys.path',
it will be downloaded and installed to the current directory. This means
that if you are using 'setuptools.find_packages()' in the same directory, you
will need to exclude the setuptools package from the distribution (unless you
want setuptools to be installed as part of your distribution). To do this,
you can simply use:
setup(
# ...
packages = [pkg for pkg in find_packages()
if not pkg.startswith('setuptools')
],
# ...
)
to eliminate the setuptools packages from consideration. However, if you are
using a 'lib' or 'src' directory to contain your distribution's packages, this
will not be an issue.
"""
from distutils.version import StrictVersion
from distutils.util import convert_path
import os.path
__all__ = ['require_version']
def require_version(version='0.0.1', dlbase='file:../../setuptools/dist'):
"""Request to use setuptools of specified version
'dlbase', if provided, is the base URL that should be used to download
a particular version of setuptools. '/setuptools-VERSION.zip' will be
added to 'dlbase' to construct the download URL, if a download is needed.
XXX current dlbase works for local testing only
"""
if StrictVersion(version) > get_installed_version():
unload_setuptools()
download_setuptools(version,dlbase)
if StrictVersion(version) > get_installed_version():
# Should never get here
raise SystemExit(
"Downloaded new version of setuptools, but it's not on sys.path"
)
def get_installed_version():
"""Return version of currently-installed setuptools, or '"0.0.0"'"""
try:
from setuptools import __version__
return __version__
except ImportError:
return '0.0.0'
def download_setuptools(version,dlbase):
"""Download setuptools-VERSION.zip from dlbase and extract in local dir"""
basename = 'setuptools-%s' % version
filename = basename+'.zip'
url = '%s/%s' % (dlbase,filename)
download_file(url,filename)
extract_zipdir(filename,basename+'/setuptools','setuptools')
def unload_setuptools():
"""Unload the current (outdated) 'setuptools' version from memory"""
import sys
for k in sys.modules.keys():
if k.startswith('setuptools.') or k=='setuptools':
del sys.modules[k]
def download_file(url,filename):
"""Download 'url', saving to 'filename'"""
from urllib2 import urlopen
f = urlopen(url); bytes = f.read(); f.close()
f = open(filename,'wb'); f.write(bytes); f.close()
def extract_zipdir(filename,zipdir,targetdir):
"""Unpack zipfile 'filename', extracting 'zipdir' to 'targetdir'"""
from zipfile import ZipFile
f = ZipFile(filename)
if zipdir and not zipdir.endswith('/'):
zipdir+='/'
plen = len(zipdir)
paths = [
path for path in f.namelist()
if path.startswith(zipdir) and not path.endswith('/')
]
paths.sort()
paths.reverse() # unpack in reverse order so __init__ goes last!
for path in paths:
out = os.path.join(targetdir,convert_path(path[plen:]))
dir = os.path.dirname(out)
if not os.path.isdir(dir):
os.makedirs(dir)
out=open(out,'wb'); out.write(f.read(path)); out.close()
f.close()
|