clean.py 2.7 KB

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