clean.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. import subprocess
  3. here = os.path.abspath(os.path.dirname(__file__))
  4. # Workaround for https://github.com/git-for-windows/git/issues/607
  5. if os.name == "nt":
  6. for (root, dnames, files) in os.walk(here):
  7. if "node_modules" in dnames:
  8. subprocess.check_call(["rmdir", "/s", "/q", "node_modules"], cwd=root, shell=True)
  9. dnames.remove("node_modules")
  10. subprocess.check_call("python -m pip uninstall -y jupyterlab".split(), cwd=here)
  11. def resolvePattern(pat):
  12. """handle a leading `#` or `@` in a pattern"""
  13. pat = pat.strip()
  14. if not pat or pat.startswith("#"):
  15. return []
  16. elif pat.startswith("@"):
  17. raw = pat[1:]
  18. return [raw, f"!packages/**/{raw}", f"!**/node_modules/**/{raw}"]
  19. else:
  20. return [pat]
  21. # get the exclude patterns listed in .cleanignore
  22. with open(os.path.join(here, ".cleanignore")) as f:
  23. git_clean_exclude = [f"--exclude={pat}" for line in f for pat in resolvePattern(line)]
  24. git_clean_command = ["git", "clean", "-dfx"] + git_clean_exclude
  25. subprocess.check_call(git_clean_command, cwd=here)