def test_remove_some(self): files = [] # create a few files, every other file is one week old new_date = timestamp_before(weeks=1) for n in range(10): fname = 'foo'+str(n) filename = self.mkfile(fname) if n % 2 == 0: os.utime(filename, (new_date, new_date)) files.append(filename) # check all files are present for filename in files: assert os.path.exists(filename), filename # cleanup_directory for all files older then one minute cleanup_directory(self.tmpdir, timestamp_before(minutes=1)) # check old files and dirs are removed for filename in files[::2]: assert not os.path.exists(filename), filename assert not os.path.exists(os.path.dirname(filename)), filename # check new files are still present for filename in files[1::2]: assert os.path.exists(filename), filename
def test_remove_some(self): files = [] # create a few files, every other file is one week old new_date = timestamp_before(weeks=1) for n in range(10): fname = 'foo' + str(n) filename = self.mkfile(fname) if n % 2 == 0: os.utime(filename, (new_date, new_date)) files.append(filename) # check all files are present for filename in files: assert os.path.exists(filename), filename # cleanup_directory for all files older then one minute cleanup_directory(self.tmpdir, timestamp_before(minutes=1)) # check old files and dirs are removed for filename in files[::2]: assert not os.path.exists(filename), filename assert not os.path.exists(os.path.dirname(filename)), filename # check new files are still present for filename in files[1::2]: assert os.path.exists(filename), filename
def test_remove_all(self): files = [] new_date = timestamp_before(weeks=1) for n in range(10): fname = 'foo'+str(n) filename = self.mkfile(fname) os.utime(filename, (new_date, new_date)) files.append(filename) for filename in files: assert os.path.exists(filename), filename cleanup_directory(self.tmpdir, timestamp_before()) for filename in files: assert not os.path.exists(filename), filename assert not os.path.exists(os.path.dirname(filename)), filename
def test_remove_all(self): files = [] new_date = timestamp_before(weeks=1) for n in range(10): fname = 'foo' + str(n) filename = self.mkfile(fname) os.utime(filename, (new_date, new_date)) files.append(filename) for filename in files: assert os.path.exists(filename), filename cleanup_directory(self.tmpdir, timestamp_before()) for filename in files: assert not os.path.exists(filename), filename assert not os.path.exists(os.path.dirname(filename)), filename
def test_no_remove(self): dirs = [self.mkdir('dir'+str(n)) for n in range(10)] for d in dirs: assert os.path.exists(d), d cleanup_directory(self.tmpdir, timestamp_before(minutes=1)) for d in dirs: assert os.path.exists(d), d
def test_no_remove(self): dirs = [self.mkdir('dir' + str(n)) for n in range(10)] for d in dirs: assert os.path.exists(d), d cleanup_directory(self.tmpdir, timestamp_before(minutes=1)) for d in dirs: assert os.path.exists(d), d
def before_timestamp_from_options(conf): """ >>> import time >>> t = before_timestamp_from_options({'hours': 4}) >>> time.time() - t - 4 * 60 * 60 < 1 True """ if 'time' in conf: try: return timestamp_from_isodate(conf['time']) except ValueError: raise SeedConfigurationError( "can't parse time '%s'. should be ISO time string" % (conf["time"], )) if 'mtime' in conf: datasource = abspath(conf['mtime']) try: return os.path.getmtime(datasource) except OSError as ex: raise SeedConfigurationError( "can't parse last modified time from file '%s'." % (datasource, ), ex) deltas = {} for delta_type in ('weeks', 'days', 'hours', 'minutes', 'seconds'): deltas[delta_type] = conf.get(delta_type, 0) return timestamp_before(**deltas)
def test_file_handler(self): files = [] file_handler_calls = [] def file_handler(filename): file_handler_calls.append(filename) new_date = timestamp_before(weeks=1) for n in range(10): fname = 'foo'+str(n) filename = self.mkfile(fname) os.utime(filename, (new_date, new_date)) files.append(filename) for filename in files: assert os.path.exists(filename), filename cleanup_directory(self.tmpdir, timestamp_before(), file_handler=file_handler) for filename in files: assert os.path.exists(filename), filename assert set(files) == set(file_handler_calls)
def test_file_handler(self): files = [] file_handler_calls = [] def file_handler(filename): file_handler_calls.append(filename) new_date = timestamp_before(weeks=1) for n in range(10): fname = 'foo' + str(n) filename = self.mkfile(fname) os.utime(filename, (new_date, new_date)) files.append(filename) for filename in files: assert os.path.exists(filename), filename cleanup_directory(self.tmpdir, timestamp_before(), file_handler=file_handler) for filename in files: assert os.path.exists(filename), filename assert set(files) == set(file_handler_calls)
def test_remove_empty_dirs(self): os.makedirs(os.path.join(self.tmpdir, 'foo', 'bar', 'baz')) cleanup_directory(self.tmpdir, timestamp_before(minutes=-1)) assert not os.path.exists(os.path.join(self.tmpdir, 'foo'))
def test_no_directory(self): cleanup_directory(os.path.join(self.tmpdir, 'invalid'), timestamp_before())
try: return timestamp_from_isodate(conf['time']) except ValueError: raise SeedConfigurationError( "can't parse time '%s'. should be ISO time string" % (conf["time"], )) if 'mtime' in conf: datasource = abspath(conf['mtime']) try: return os.path.getmtime(datasource) except OSError, ex: raise SeedConfigurationError( "can't parse last modified time from file '%s'." % (datasource, ), ex) deltas = {} for delta_type in ('weeks', 'days', 'hours', 'minutes', 'seconds'): deltas[delta_type] = conf.get(delta_type, 0) return timestamp_before(**deltas) class LevelsList(object): def __init__(self, levels=None): self.levels = levels def for_grid(self, grid): uniqe_valid_levels = set(l for l in self.levels if 0 <= l <= (grid.levels-1)) return sorted(uniqe_valid_levels) class LevelsRange(object): def __init__(self, level_range=None): self.level_range = level_range def for_grid(self, grid):
except ValueError: raise SeedConfigurationError( "can't parse time '%s'. should be ISO time string" % (conf["time"], )) if 'mtime' in conf: datasource = abspath(conf['mtime']) try: return os.path.getmtime(datasource) except OSError, ex: raise SeedConfigurationError( "can't parse last modified time from file '%s'." % (datasource, ), ex) deltas = {} for delta_type in ('weeks', 'days', 'hours', 'minutes', 'seconds'): deltas[delta_type] = conf.get(delta_type, 0) return timestamp_before(**deltas) class LevelsList(object): def __init__(self, levels=None): self.levels = levels def for_grid(self, grid): uniqe_valid_levels = set(l for l in self.levels if 0 <= l <= (grid.levels - 1)) return sorted(uniqe_valid_levels) class LevelsRange(object): def __init__(self, level_range=None): self.level_range = level_range