clean.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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'],
  9. cwd=root, shell=True)
  10. dnames.remove('node_modules')
  11. subprocess.check_call('python -m pip uninstall -y jupyterlab'.split(), cwd=here)
  12. def resolvePattern(pat):
  13. """handle a leading `#` or `@` in a pattern
  14. """
  15. pat = pat.strip()
  16. if not pat or pat.startswith('#'):
  17. return []
  18. elif pat.startswith('@'):
  19. raw = pat[1:]
  20. return [
  21. raw,
  22. f'!packages/**/{raw}',
  23. f'!**/node_modules/**/{raw}'
  24. ]
  25. else:
  26. return [pat]
  27. # get the exclude patterns listed in .cleanignore
  28. with open(os.path.join(here, '.cleanignore')) as f:
  29. git_clean_exclude = [f'--exclude={pat}'
  30. for line in f
  31. for pat in resolvePattern(line)]
  32. git_clean_command = ['git', 'clean', '-dfx'] + git_clean_exclude
  33. subprocess.check_call(git_clean_command, cwd=here)