You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import shutil
  5. import sys
  6. from doodbalib import CLEAN, ODOO_DIR, PRIVATE_DIR, SRC_DIR, addons_config, logger
  7. if not CLEAN:
  8. logger.warning("Not cleaning garbage")
  9. sys.exit()
  10. # Get the enabled paths
  11. repos_addons = {}
  12. for addon, repo in addons_config(filtered=False):
  13. repo_path = os.path.realpath(os.path.join(SRC_DIR, repo))
  14. repos_addons.setdefault(repo_path, set())
  15. repos_addons[repo_path].add(addon)
  16. logger.debug("Addon paths enabled: %s", repos_addons)
  17. # Traverse src dir and remove anything not explicitly enabled
  18. for directory, subdirectories, subfiles in os.walk(SRC_DIR):
  19. logger.debug("Checking for cleanup directory %s", directory)
  20. # Skip main src directory
  21. if directory == SRC_DIR:
  22. continue
  23. # Always skip private/*
  24. if directory == PRIVATE_DIR:
  25. subdirectories[:] = []
  26. continue
  27. # Inside the odoo dir, skip all but addons dir
  28. if directory == ODOO_DIR:
  29. subdirectories[:] = ["addons"]
  30. continue
  31. try:
  32. # Get addons enalbed in current directory
  33. enabled_addons = repos_addons[directory]
  34. except KeyError:
  35. # This isn't a repo; is there anything inside to preserve?
  36. directory += os.path.sep
  37. if any(repo.startswith(directory) for repo in repos_addons):
  38. # Then, let's walk in; we'll remove later if needed
  39. continue
  40. else:
  41. # This is an addons repo; do not walk into the enabled ones
  42. for addon in enabled_addons:
  43. subdirectories.remove(addon)
  44. continue
  45. # Remove every other directory
  46. logger.info("Removing directory %s", directory)
  47. shutil.rmtree(directory)