__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. Delete file with the oldest modification date
  3. until a minimum of --free-bytes are available on the respective disk.
  4. """
  5. import argparse
  6. import datetime
  7. import logging
  8. import os
  9. import re
  10. import shutil
  11. # https://en.wikipedia.org/wiki/Template:Quantities_of_bytes
  12. _DATA_SIZE_UNIT_BYTE_CONVERSION_FACTOR = {
  13. "B": 1,
  14. "kB": 10**3,
  15. "KB": 10**3,
  16. "MB": 10**6,
  17. "GB": 10**9,
  18. "TB": 10**12,
  19. "KiB": 2**10,
  20. "MiB": 2**20,
  21. "GiB": 2**30,
  22. "TiB": 2**40,
  23. }
  24. def _data_size_to_bytes(size_with_unit: str) -> int:
  25. match = re.match(r"^([\d\.]+)\s*([A-Za-z]+)?$", size_with_unit)
  26. if not match:
  27. raise ValueError(f"Unable to parse data size {size_with_unit!r}")
  28. unit_symbol = match.group(2)
  29. if unit_symbol:
  30. try:
  31. byte_conversion_factor = _DATA_SIZE_UNIT_BYTE_CONVERSION_FACTOR[unit_symbol]
  32. except KeyError as exc:
  33. raise ValueError(f"Unknown data size unit symbol {unit_symbol!r}") from exc
  34. else:
  35. byte_conversion_factor = 1
  36. byte_size = float(match.group(1)) * byte_conversion_factor
  37. return int(round(byte_size, 0))
  38. def _main() -> None:
  39. argparser = argparse.ArgumentParser(description=__doc__)
  40. argparser.add_argument("-d", "--debug", action="store_true")
  41. argparser.add_argument(
  42. "--delete-re",
  43. action="store",
  44. help="Only delete files matching regexp. examples: .*mp4$",
  45. default=".*",
  46. )
  47. argparser.add_argument(
  48. "--free-bytes",
  49. type=_data_size_to_bytes,
  50. required=True,
  51. help="examples: 1024, 1024B, 4KiB, 4KB, 2TB",
  52. )
  53. argparser.add_argument("root_dir_path", metavar="ROOT_DIR")
  54. args = argparser.parse_args()
  55. logging.basicConfig(
  56. level=logging.DEBUG if args.debug else logging.INFO,
  57. format="%(asctime)s:%(levelname)s:%(message)s",
  58. datefmt="%Y-%m-%dT%H:%M:%S%z",
  59. )
  60. logging.debug("Required free bytes: %d", args.free_bytes)
  61. disk_usage = shutil.disk_usage(args.root_dir_path)
  62. logging.debug(disk_usage)
  63. if disk_usage.free >= args.free_bytes:
  64. logging.debug("Requirement already fulfilled")
  65. return
  66. file_paths = [
  67. os.path.join(dirpath, filename)
  68. for dirpath, _, filenames in os.walk(args.root_dir_path)
  69. for filename in filenames
  70. ]
  71. delete_re = re.compile(args.delete_re)
  72. file_mtime_paths = [
  73. (os.stat(p).st_mtime, p) for p in file_paths if delete_re.match(p)
  74. ]
  75. file_mtime_paths.sort()
  76. removed_files_counter = 0
  77. last_mtime = None
  78. for file_mtime, file_path in file_mtime_paths:
  79. if shutil.disk_usage(args.root_dir_path).free >= args.free_bytes:
  80. break
  81. os.remove(file_path)
  82. logging.debug("Removed file %s", file_path)
  83. removed_files_counter += 1
  84. last_mtime = file_mtime
  85. if removed_files_counter == 0:
  86. logging.warning("No files to remove")
  87. else:
  88. assert last_mtime is not None # for mypy
  89. logging.info(
  90. "Removed %d file(s) with modification date <= %sZ",
  91. removed_files_counter,
  92. datetime.datetime.utcfromtimestamp(last_mtime).isoformat("T"),
  93. )