clean.py 2.9 KB

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