Commit cdc72d1f authored by Vincent Pelletier's avatar Vincent Pelletier

Assorted improvements.

- Do not use a property for a local variable.
- Use new-style class.
- Stop using logger.
- Save a few getattr calls.
- Do not keep references to buildout & options outside of __init__ .
- Convert mode in __init__ to detect errors early.
- Factorise several accesses (buildout['buildout'], properties)
- Drop redundant "cache" download.Download parameter: it is automatically
  fetched from first parameter internally.
- "Backward compatibility with other recipes" is just not "backward".
- Return downloaded file path, not just the optionally-created directory.
parent 4a132aa4
...@@ -25,51 +25,48 @@ ...@@ -25,51 +25,48 @@
# #
############################################################################## ##############################################################################
import os import os
import logging
import shutil import shutil
import zc.buildout import zc.buildout
class Recipe:
class Recipe(object):
_parts = None
def __init__(self, buildout, name, options): def __init__(self, buildout, name, options):
self.buildout = buildout buildout_section = buildout['buildout']
self.name = name self._downloader = zc.buildout.download.Download(buildout_section,
self.options = options hash_name=True)
self.logger = logging.getLogger(self.name) self._url = options['url']
if 'filename' in self.options and 'destination' in self.options: self._md5sum = options.get('md5sum')
mode = options.get('mode')
if mode is not None:
mode = int(mode, 8)
self._mode = mode
if 'filename' in options and 'destination' in options:
raise zc.buildout.UserError('Parameters filename and destination are ' raise zc.buildout.UserError('Parameters filename and destination are '
'exclusive.') 'exclusive.')
self.parts = None destination = options.get('destination', None)
self.destination = self.options.get('destination', None) if destination is None:
if self.destination is None: self._parts = parts = os.path.join(buildout_section['parts-directory'],
self.parts = os.path.join(self.buildout['buildout']['parts-directory'], name)
self.name) destination = os.path.join(parts, options.get('filename', name))
self.destination = os.path.join(self.parts, self.options.get('filename', # Compatibility with other recipes: expose location
self.name)) options['location'] = parts
# backward compatibility with other recipes -- expose location options['target'] = self._destination = destination
options['location'] = os.path.join(self.buildout['buildout'][
'parts-directory'], self.name)
options['target'] = self.destination
def install(self): def install(self):
if self.parts is not None: destination = self._destination
if not os.path.isdir(self.parts): result = [destination]
os.mkdir(self.parts) parts = self._parts
download = zc.buildout.download.Download(self.buildout['buildout'], if parts is not None and not os.path.isdir(parts):
hash_name=True, cache=self.buildout['buildout'].get('download-cache')) os.mkdir(parts)
path, is_temp = download(self.options['url'], result.append(parts)
md5sum=self.options.get('md5sum')) path, _ = self._downloader(self._url, md5sum=self._md5sum)
if os.path.exists(self.destination): if os.path.exists(destination):
os.unlink(self.destination) os.unlink(destination)
shutil.copy(path, self.destination) shutil.copy(path, destination)
mode = self.options.get('mode') mode = self._mode
if mode is not None: if mode is not None:
mode = int(mode, 8) os.chmod(destination, mode)
os.chmod(self.destination, mode) return result
self.logger.debug('Mode of %r set to 0%o.' % (self.destination, mode))
self.logger.debug('Downloaded %r and saved to %r.' % (self.options['url'],
self.destination))
if self.parts is not None:
return [self.parts]
else:
return []
update = install update = install
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