Commit 49e5b835 authored by Julien Muchembled's avatar Julien Muchembled

build: review of file/dir removal

parent 053815eb
......@@ -24,6 +24,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import errno
import logging
import os
from platform import uname
......@@ -121,6 +122,14 @@ def guessOperatingSystem():
assert target, 'Unknown architecture'
return target
def rmtree(path):
try:
os.remove(path)
except OSError, e:
if e.errno != errno.EISDIR:
raise
shutil.rmtree(path)
TRUE_LIST = ('y', 'on', 'yes', 'true', '1')
class Script:
......@@ -189,14 +198,14 @@ class Script:
hash_name=True, cache=self.buildout['buildout'].get('download-cache'))
path, is_temp = download(url, md5sum=md5sum)
if is_temp:
self.cleanup_file_list.append(path)
self.cleanup_list.append(path)
return path
def extract(self, path):
extract_dir = tempfile.mkdtemp(self.name)
self.logger.debug('Created working directory %r' % extract_dir)
setuptools.archive_util.unpack_archive(path, extract_dir)
self.cleanup_dir_list.append(extract_dir)
self.cleanup_list.append(extract_dir)
return extract_dir
def pipeCommand(self, command_list_list, *args, **kwargs):
......@@ -252,11 +261,12 @@ class Script:
raise error
return True
cleanup_dir_list = cleanup_file_list = property(
lambda self: self.cleanup_list)
script = 'raise NotImplementedError'
def __init__(self, buildout, name, options):
self.cleanup_dir_list = []
self.cleanup_file_list = []
self.cleanup_list = []
self.options = options
self.buildout = buildout
self.name = name
......@@ -308,36 +318,26 @@ class Script:
return env
def install(self):
location = self.options['location']
if os.path.lexists(location):
self.logger.warning('Removing already existing path %r', location)
rmtree(location)
env = self.getEnvironment() # env is used in script exec'ed below
try:
if os.path.exists(self.options['location']):
self.cleanup_dir_list.append(self.options['location'])
raise shutil.Error('Directory %s already exists' %
self.options['location'])
env = self.getEnvironment() # env is used in script exec'ed below
exec self.options['script']
try:
self._checkPromise('slapos_promise', self.options['location'])
except Exception:
if os.path.exists(self.options['location']):
self.cleanup_dir_list.append(self.options['location'])
raise
self._checkPromise('slapos_promise', location)
except:
self.cleanup_list.append(location)
raise
finally:
for d in self.cleanup_dir_list:
if os.path.exists(d):
if not self.keep_on_error:
self.logger.debug('Cleanup directory %r' % d)
shutil.rmtree(d)
for path in self.cleanup_list:
if os.path.lexists(path):
if self.keep_on_error:
self.logger.info('Left %r as requested', path)
else:
self.logger.info('Left directory %r as requested' % d)
for f in self.cleanup_file_list:
if os.path.exists(f):
if not self.keep_on_error:
self.logger.debug('Cleanup file %r' % f)
os.unlink(f)
else:
self.logger.info('Left file %r as requested' % f)
return [self.options['location']]
self.logger.debug('Removing %r', path)
rmtree(path)
return [location]
def update(self):
pass
......
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