def clean_files(session, dry_run): filecacher = FileCacher() files = set(file[0] for file in filecacher.list()) logger.info("A total number of %d files are present in the file store", len(files)) for cls in [Attachment, Executable, File, Manager, PrintJob, Statement, Testcase, UserTest, UserTestExecutable, UserTestFile, UserTestManager, UserTestResult]: for col in ["input", "output", "digest"]: if hasattr(cls, col): found_digests = set() digests = session.query(cls).all() digests = [getattr(obj, col) for obj in digests] found_digests |= set(digests) found_digests.discard(FileCacher.TOMBSTONE_DIGEST) logger.info("Found %d digests while scanning %s.%s", len(found_digests), cls.__name__, col) files -= found_digests logger.info("%d digests are orphan.", len(files)) total_size = 0 for orphan in files: total_size += filecacher.get_size(orphan) logger.info("Orphan files take %s bytes of disk space", "{:,}".format(total_size)) if not dry_run: for count, orphan in enumerate(files): filecacher.delete(orphan) if count % 100 == 0: logger.info("%d files deleted from the file store", count) logger.info("All orphan files have been deleted")
def clean_files(session, dry_run): filecacher = FileCacher() files = set(file[0] for file in filecacher.list()) logger.info("A total number of %d files are present in the file store", len(files)) for cls in [ Attachment, Executable, File, Manager, PrintJob, Statement, Testcase, UserTest, UserTestExecutable, UserTestFile, UserTestManager, UserTestResult ]: for col in ["input", "output", "digest"]: if hasattr(cls, col): found_digests = set() digests = session.query(cls).all() digests = [getattr(obj, col) for obj in digests] found_digests |= set(digests) found_digests.discard(FileCacher.TOMBSTONE_DIGEST) logger.info("Found %d digests while scanning %s.%s", len(found_digests), cls.__name__, col) files -= found_digests logger.info("%d digests are orphan.", len(files)) total_size = 0 for orphan in files: total_size += filecacher.get_size(orphan) logger.info("Orphan files take %s bytes of disk space", "{:,}".format(total_size)) if not dry_run: for count, orphan in enumerate(files): filecacher.delete(orphan) if count % 100 == 0: logger.info("%d files deleted from the file store", count) logger.info("All orphan files have been deleted")
def clean_files(session, dry_run): filecacher = FileCacher() files = set(file[0] for file in filecacher.list()) logger.info("A total number of %d files are present in the file store", len(files)) found_digests = enumerate_files(session) logger.info("Found %d digests while scanning", len(found_digests)) files -= found_digests logger.info("%d digests are orphan.", len(files)) total_size = 0 for orphan in files: total_size += filecacher.get_size(orphan) logger.info("Orphan files take %s bytes of disk space", "{:,}".format(total_size)) if not dry_run: for count, orphan in enumerate(files): filecacher.delete(orphan) if count % 100 == 0: logger.info("%d files deleted from the file store", count) logger.info("All orphan files have been deleted")