def testNegation(self): paths = [ gc.Path("/foo", 4), gc.Path("/foo", 5), gc.Path("/foo", 6), gc.Path("/foo", 9) ] mod = gc.negation(gc.mod_export_version(2)) self.assertEquals(mod(paths), [gc.Path("/foo", 5), gc.Path("/foo", 9)]) mod = gc.negation(gc.mod_export_version(3)) self.assertEquals(mod(paths), [gc.Path("/foo", 4), gc.Path("/foo", 5)])
def garbage_collect_exports(export_dir_base, exports_to_keep): """Deletes older exports, retaining only a given number of the most recent. Export subdirectories are assumed to be named with monotonically increasing integers; the most recent are taken to be those with the largest values. Args: export_dir_base: the base directory under which each export is in a versioned subdirectory. exports_to_keep: the number of recent exports to retain. """ if exports_to_keep is None: return keep_filter = gc.largest_export_versions(exports_to_keep) delete_filter = gc.negation(keep_filter) # Export dir must not end with / or it will break the re match below. if export_dir_base.endswith('/'): export_dir_base = export_dir_base[:-1] # create a simple parser that pulls the export_version from the directory. def parser(path): match = re.match('^' + export_dir_base + '/(\\d{13})$', path.path) if not match: return None return path._replace(export_version=int(match.group(1))) for p in delete_filter(gc.get_paths(export_dir_base, parser=parser)): gfile.DeleteRecursively(p.path)
def garbage_collect_exports(export_dir_base, exports_to_keep): """Deletes older exports, retaining only a given number of the most recent. Export subdirectories are assumed to be named with monotonically increasing integers; the most recent are taken to be those with the largest values. Args: export_dir_base: the base directory under which each export is in a versioned subdirectory. exports_to_keep: the number of recent exports to retain. """ if exports_to_keep is None: return keep_filter = gc.largest_export_versions(exports_to_keep) delete_filter = gc.negation(keep_filter) # create a simple parser that pulls the export_version from the directory. def parser(path): filename = os.path.basename(path.path) if not (len(filename) == 10 and filename.isdigit()): return None return path._replace(export_version=int(filename)) for p in delete_filter(gc.get_paths(export_dir_base, parser=parser)): gfile.DeleteRecursively(p.path)
def garbage_collect_exports(export_dir_base, exports_to_keep): """Deletes older exports, retaining only a given number of the most recent. Export subdirectories are assumed to be named with monotonically increasing integers; the most recent are taken to be those with the largest values. Args: export_dir_base: the base directory under which each export is in a versioned subdirectory. exports_to_keep: the number of recent exports to retain. """ if exports_to_keep is None: return keep_filter = gc.largest_export_versions(exports_to_keep) delete_filter = gc.negation(keep_filter) for p in delete_filter(gc.get_paths(export_dir_base, parser=_export_version_parser)): gfile.DeleteRecursively(p.path)