clean.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import glob
  2. import json
  3. import os
  4. from pathlib import Path
  5. import subprocess
  6. from ...utils.deps import CACHE_DIR
  7. from ...utils.fs import remove_file, remove_tree
  8. from ...utils.log import log
  9. __all__ = ['execute']
  10. def _unpack_args(args):
  11. '''Unpack command line arguments
  12. '''
  13. return (args.tags, args.all)
  14. def execute(images, all_):
  15. '''Clean cached image(s)
  16. '''
  17. cache = Path(CACHE_DIR)
  18. if not images:
  19. images = [image[9:] for image in sorted(os.listdir(cache /
  20. 'share/images'))]
  21. for image in images:
  22. try:
  23. image, tag = image.rsplit(':', 1)
  24. except ValueError:
  25. tag = None
  26. if not image.replace('_', '').isalnum():
  27. raise ValueError(f'bad image tag ({image})')
  28. path = cache / f'share/images/manylinux{image}'
  29. if not path.exists():
  30. raise ValueError(f'no such image ({image})')
  31. if tag is None:
  32. if not all_:
  33. path = path / 'extracted'
  34. remove_tree(str(path))
  35. else:
  36. tag_file = path / f'tags/{tag}.json'
  37. if not tag_file.exists():
  38. raise ValueError(f'no such image ({image}:{tag})')
  39. if all_:
  40. with tag_file.open() as f:
  41. layers = json.load(f)["layers"]
  42. for layer in layers:
  43. layer = path / f'layers/{layer}.tar.gz'
  44. if layer.exists():
  45. remove_file(str(layer))
  46. remove_file(str(tag_file))
  47. else:
  48. path = cache / f'share/images/{image}/extracted/{tag}'
  49. if path.exists():
  50. remove_tree(str(path))