clean.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """distutils.command.clean
  2. Implements the Distutils 'clean' command."""
  3. # contributed by Bastian Kleineidam <calvin@cs.uni-sb.de>, added 2000-03-18
  4. import os
  5. from distutils._log import log
  6. from typing import ClassVar
  7. from ..core import Command
  8. from ..dir_util import remove_tree
  9. class clean(Command):
  10. description = "clean up temporary files from 'build' command"
  11. user_options = [
  12. ('build-base=', 'b', "base build directory [default: 'build.build-base']"),
  13. (
  14. 'build-lib=',
  15. None,
  16. "build directory for all modules [default: 'build.build-lib']",
  17. ),
  18. ('build-temp=', 't', "temporary build directory [default: 'build.build-temp']"),
  19. (
  20. 'build-scripts=',
  21. None,
  22. "build directory for scripts [default: 'build.build-scripts']",
  23. ),
  24. ('bdist-base=', None, "temporary directory for built distributions"),
  25. ('all', 'a', "remove all build output, not just temporary by-products"),
  26. ]
  27. boolean_options: ClassVar[list[str]] = ['all']
  28. def initialize_options(self):
  29. self.build_base = None
  30. self.build_lib = None
  31. self.build_temp = None
  32. self.build_scripts = None
  33. self.bdist_base = None
  34. self.all = None
  35. def finalize_options(self):
  36. self.set_undefined_options(
  37. 'build',
  38. ('build_base', 'build_base'),
  39. ('build_lib', 'build_lib'),
  40. ('build_scripts', 'build_scripts'),
  41. ('build_temp', 'build_temp'),
  42. )
  43. self.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
  44. def run(self):
  45. # remove the build/temp.<plat> directory (unless it's already
  46. # gone)
  47. if os.path.exists(self.build_temp):
  48. remove_tree(self.build_temp)
  49. else:
  50. log.debug("'%s' does not exist -- can't clean it", self.build_temp)
  51. if self.all:
  52. # remove build directories
  53. for directory in (self.build_lib, self.bdist_base, self.build_scripts):
  54. if os.path.exists(directory):
  55. remove_tree(directory)
  56. else:
  57. log.warning("'%s' does not exist -- can't clean it", directory)
  58. # just for the heck of it, try to remove the base build directory:
  59. # we might have emptied it right now, but if not we don't care
  60. try:
  61. os.rmdir(self.build_base)
  62. log.info("removing '%s'", self.build_base)
  63. except OSError:
  64. pass