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

build: review of file/dir removal

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