Commit dfb24fa1 authored by Thomas Larrieu's avatar Thomas Larrieu

finished implementing cleanup feature. still have to implement testing for this.

parent f5d260a2
...@@ -228,6 +228,48 @@ class Recipe(object): ...@@ -228,6 +228,48 @@ class Recipe(object):
log.error('Command failed: %s: %s' % (e, cmd)) log.error('Command failed: %s: %s' % (e, cmd))
raise zc.buildout.UserError('System error') raise zc.buildout.UserError('System error')
def clean(self, cleanup_list):
"""
Basically, this method does not do much, appart from removing
what it is told to, as a list passed in args
"""
import glob
def build_working_iterator(cleanup_list):
current_dir = os.getcwd()
for pattern in cleanup_list:
# if current pattern contains wildcards, we search for all matching
# files/directories
if pattern.find('*') > -1 or pattern.find('?') > -1 or pattern.find('[') > -1:
g = glob.glob(pattern)
if g:
for x in g:
yield x
else:
raise IOError("Pattern " + pattern + " did not match anything")
elif os.path.exists(pattern):
if os.path.relpath(pattern).startswith('..'):
raise IOError(current_dir + " does not contain " + pattern)
else:
yield pattern
else:
raise IOError(pattern + " does not exist")
# As build_working_iterator is a generator and since we want to first
# check every file (raising exceptions when necessary) BEFORE removing
# anything from the file system, we enforce full lookup by build a tuple
# out of it
for x in tuple(build_working_iterator(cleanup_list)):
# This first condition is mandatory as we might have removed an entire
# tree during a previous iteration, possibly leading to a case where
# some items are still to be removed but do not exist anymore
if os.path.exists(x):
if os.path.isdir(x):
shutil.rmtree(x)
else:
os.remove(x)
logging.getLogger(self.name).info('%s has been successfully removed', x)
def install(self): def install(self):
log = logging.getLogger(self.name) log = logging.getLogger(self.name)
parts = [] parts = []
...@@ -370,15 +412,5 @@ class Recipe(object): ...@@ -370,15 +412,5 @@ class Recipe(object):
# Cleanup state # Cleanup state
cleanup_list = self.options.get('cleanup', '').split() cleanup_list = self.options.get('cleanup', '').split()
self.clean(cleanup_list) self.clean(cleanup_list)
return parts return parts
def clean(self, cleanup_list):
"""
Basically, this method does not do much, appart from removing
what it is told to, as a list passed in args
"""
current_dir = os.getcwd()
for i, directory in enumerate(cleanup_list):
print str(i) + "->" + directory
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