| 1 |
|
| 2 |
|
| 3 |
import argparse |
| 4 |
import glob |
| 5 |
import os |
| 6 |
|
| 7 |
if __name__ != "__main__": |
| 8 |
raise ImportError(f"{__name__} should not be used as a module.") |
| 9 |
|
| 10 |
|
| 11 |
def main(): |
| 12 |
parser = argparse.ArgumentParser(description="Cleanup old cache files") |
| 13 |
parser.add_argument("timestamp", type=int, help="Unix timestamp cutoff") |
| 14 |
parser.add_argument("directory", help="Path to cache directory") |
| 15 |
args = parser.parse_args() |
| 16 |
|
| 17 |
ret = 0 |
| 18 |
|
| 19 |
|
| 20 |
if os.path.exists("redundant.txt"): |
| 21 |
with open("redundant.txt") as redundant: |
| 22 |
for item in map(str.strip, redundant): |
| 23 |
if os.path.isfile(item): |
| 24 |
try: |
| 25 |
os.remove(item) |
| 26 |
except OSError: |
| 27 |
print(f'Failed to handle "{item}"; skipping.') |
| 28 |
ret += 1 |
| 29 |
|
| 30 |
for file in glob.glob(os.path.join(args.directory, "*", "*")): |
| 31 |
try: |
| 32 |
if os.path.getatime(file) < args.timestamp: |
| 33 |
os.remove(file) |
| 34 |
except OSError: |
| 35 |
print(f'Failed to handle "{file}"; skipping.') |
| 36 |
ret += 1 |
| 37 |
|
| 38 |
return ret |
| 39 |
|
| 40 |
|
| 41 |
try: |
| 42 |
raise SystemExit(main()) |
| 43 |
except KeyboardInterrupt: |
| 44 |
import signal |
| 45 |
|
| 46 |
signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 47 |
os.kill(os.getpid(), signal.SIGINT) |