Commit d5f5d399 authored by Julien Muchembled's avatar Julien Muchembled

Make 'download-unpacked' compatible with Python 2.6

parent 3ffdd641
......@@ -44,8 +44,8 @@ class EnvironMixin:
raise zc.buildout.UserError('Key %r is repeated' % k)
env[k] = v.strip() % environ
else:
self._environ = {k: v.strip() % environ
for k, v in self.buildout[environment].iteritems()}
self._environ = dict((k, v.strip() % environ)
for k, v in self.buildout[environment].iteritems())
else:
self._environ = None if allow_none else {}
......
......@@ -33,24 +33,10 @@ import zc.buildout
import tempfile
import setuptools
TRUE_VALUES = ('yes', 'true', '1', 'on')
is_true = ('false', 'true').index
class Recipe:
def calculate_base(self, extract_dir):
log = logging.getLogger(self.name)
# Move the contents of the package in to the correct destination
top_level_contents = os.listdir(extract_dir)
if self.options['strip-top-level-dir'].strip().lower() in TRUE_VALUES:
if len(top_level_contents) != 1:
log.error('Unable to strip top level directory because there are more '
'than one element in the root of the package.')
raise zc.buildout.UserError('Invalid package contents')
base = os.path.join(extract_dir, top_level_contents[0])
else:
base = extract_dir
return base
def __init__(self, buildout, name, options):
self.buildout = buildout
self.name = name
......@@ -93,57 +79,59 @@ class Recipe:
if self.parts is not None:
if not os.path.isdir(self.parts):
os.mkdir(self.parts)
download = zc.buildout.download.Download(self.buildout['buildout'],
hash_name=True, cache=self.buildout['buildout'].get('download-cache'))
path, is_temp = download(self.options['url'],
md5sum=self.options.get('md5sum'))
extract_dir = tempfile.mkdtemp(self.name)
self.logger.debug('Created working directory %r' % extract_dir)
try:
patch_archive_util()
# ad-hoc support for .xz and .lz archive
hdr = file(path).read(6)
if hdr == '\xfd7zXZ\x00':
new_path = os.path.join(extract_dir, os.path.basename(path))
file(new_path, 'w').write(subprocess.check_output(['xzcat', path], env=self.environ))
setuptools.archive_util.unpack_archive(new_path, extract_dir)
os.unlink(new_path)
elif hdr.startswith('LZIP'):
new_path = os.path.join(extract_dir, os.path.basename(path))
file(new_path, 'w').write(subprocess.check_output(['lunzip', '-c', path], env=self.environ))
setuptools.archive_util.unpack_archive(new_path, extract_dir)
os.unlink(new_path)
else:
setuptools.archive_util.unpack_archive(path, extract_dir)
finally:
unpatch_archive_util()
if is_temp:
os.unlink(path)
self.logger.debug('Created working directory %r', extract_dir)
path, is_temp = download(self.options['url'],
md5sum=self.options.get('md5sum'))
try:
patch_archive_util()
# ad-hoc support for .xz and .lz archive
hdr = open(path, 'rb').read(6)
for magic, cmd in (('\xfd7zXZ\x00', ('xzcat',)),
('LZIP', ('lunzip', '-c'))):
if hdr.startswith(magic):
new_path = os.path.join(extract_dir, os.path.basename(path))
with open(new_path, 'wb') as stdout:
subprocess.check_call(cmd + (path,),
stdout=stdout, env=self.environ)
setuptools.archive_util.unpack_archive(new_path, extract_dir)
os.unlink(new_path)
break
else:
setuptools.archive_util.unpack_archive(path, extract_dir)
finally:
unpatch_archive_util()
if is_temp:
os.unlink(path)
# Delete destination directory if exist
if os.path.exists(self.destination):
shutil.rmtree(self.destination)
# Create destination directory
if not os.path.isdir(self.destination):
if os.path.exists(self.destination):
shutil.rmtree(self.destination)
os.makedirs(self.destination)
base_dir = extract_dir
if not self.options.has_key('strip-top-level-dir'):
directories = os.listdir(extract_dir)
if len(directories) == 1 and os.path.isdir(os.path.join(extract_dir, directories[0])):
base_dir = os.path.join(extract_dir, directories[0])
else:
base_dir = self.calculate_base(extract_dir)
extract_dir = os.path.join(base_dir, self.options['extract-directory'])
for filename in os.listdir(extract_dir):
dest = os.path.join(self.destination, filename)
shutil.move(os.path.join(extract_dir, filename), dest)
shutil.rmtree(extract_dir)
self.logger.debug('Downloaded %r and saved to %r.' % (self.options['url'],
self.destination))
strip = self.options.get('strip-top-level-dir')
if strip:
if is_true(strip.lower()):
base_dir, = os.listdir(extract_dir)
base_dir = os.path.join(extract_dir, base_dir)
else:
base_dir = extract_dir
else:
directories = os.listdir(extract_dir)
if len(directories) == 1:
base_dir = os.path.join(extract_dir, directories[0])
if not os.path.isdir(base_dir):
base_dir = extract_dir
base_dir = os.path.join(base_dir, self.options['extract-directory'])
for filename in os.listdir(base_dir):
shutil.move(os.path.join(base_dir, filename), self.destination)
finally:
shutil.rmtree(extract_dir)
self.logger.debug('Downloaded %r and saved to %r.',
self.options['url'], self.destination)
if self.parts is not None:
return [self.parts]
else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment