clean.py 2.7 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.core import Command
  6. from distutils.dir_util import remove_tree
  7. from distutils import log
  8. class clean(Command):
  9. description = "clean up temporary files from 'build' command"
  10. user_options = [
  11. ('build-base=', 'b',
  12. "base build directory (default: 'build.build-base')"),
  13. ('build-lib=', None,
  14. "build directory for all modules (default: 'build.build-lib')"),
  15. ('build-temp=', 't',
  16. "temporary build directory (default: 'build.build-temp')"),
  17. ('build-scripts=', None,
  18. "build directory for scripts (default: 'build.build-scripts')"),
  19. ('bdist-base=', None,
  20. "temporary directory for built distributions"),
  21. ('all', 'a',
  22. "remove all build output, not just temporary by-products")
  23. ]
  24. boolean_options = ['all']
  25. def initialize_options(self):
  26. self.build_base = None
  27. self.build_lib = None
  28. self.build_temp = None
  29. self.build_scripts = None
  30. self.bdist_base = None
  31. self.all = None
  32. def finalize_options(self):
  33. self.set_undefined_options('build',
  34. ('build_base', 'build_base'),
  35. ('build_lib', 'build_lib'),
  36. ('build_scripts', 'build_scripts'),
  37. ('build_temp', 'build_temp'))
  38. self.set_undefined_options('bdist',
  39. ('bdist_base', 'bdist_base'))
  40. def run(self):
  41. # remove the build/temp.<plat> directory (unless it's already
  42. # gone)
  43. if os.path.exists(self.build_temp):
  44. remove_tree(self.build_temp, dry_run=self.dry_run)
  45. else:
  46. log.debug("'%s' does not exist -- can't clean it",
  47. self.build_temp)
  48. if self.all:
  49. # remove build directories
  50. for directory in (self.build_lib,
  51. self.bdist_base,
  52. self.build_scripts):
  53. if os.path.exists(directory):
  54. remove_tree(directory, dry_run=self.dry_run)
  55. else:
  56. log.warn("'%s' does not exist -- can't clean it",
  57. 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. if not self.dry_run:
  61. try:
  62. os.rmdir(self.build_base)
  63. log.info("removing '%s'", self.build_base)
  64. except OSError:
  65. pass