__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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-path-regex",
  43. metavar="REGULAR_EXPRESSION",
  44. type=re.compile, # type: ignore
  45. help="Only delete files with path matching regular expression (at any position)."
  46. " Paths will not be resolved or made absolute before check."
  47. r" Examples: \.mp4$ or ^/tmp/\d or ^rel/ative/ (default: no filter)",
  48. default="",
  49. )
  50. argparser.add_argument(
  51. "--free-bytes",
  52. type=_data_size_to_bytes,
  53. required=True,
  54. help="examples: 1024, 1024B, 4KiB, 4KB, 2TB",
  55. )
  56. argparser.add_argument("root_dir_path", metavar="ROOT_DIR")
  57. args = argparser.parse_args()
  58. logging.basicConfig(
  59. level=logging.DEBUG if args.debug else logging.INFO,
  60. format="%(asctime)s:%(levelname)s:%(message)s",
  61. datefmt="%Y-%m-%dT%H:%M:%S%z",
  62. )
  63. logging.debug("Required free bytes: %d", args.free_bytes)
  64. disk_usage = shutil.disk_usage(args.root_dir_path)
  65. logging.debug(disk_usage)
  66. if disk_usage.free >= args.free_bytes:
  67. logging.debug("Requirement already fulfilled")
  68. return
  69. file_paths = [
  70. os.path.join(dirpath, filename)
  71. for dirpath, _, filenames in os.walk(args.root_dir_path)
  72. for filename in filenames
  73. ]
  74. file_mtime_paths = [
  75. (os.stat(p).st_mtime, p) for p in file_paths if args.delete_path_regex.search(p)
  76. ]
  77. file_mtime_paths.sort()
  78. removed_files_counter = 0
  79. last_mtime = None
  80. for file_mtime, file_path in file_mtime_paths:
  81. if shutil.disk_usage(args.root_dir_path).free >= args.free_bytes:
  82. break
  83. os.remove(file_path)
  84. logging.debug("Removed file %s", file_path)
  85. removed_files_counter += 1
  86. last_mtime = file_mtime
  87. if removed_files_counter == 0:
  88. logging.warning("No files to remove")
  89. else:
  90. assert last_mtime is not None # for mypy
  91. logging.info(
  92. "Removed %d file(s) with modification date <= %sZ",
  93. removed_files_counter,
  94. datetime.datetime.utcfromtimestamp(last_mtime).isoformat("T"),
  95. )