blob: 0cb270166211fe2b24b6ec636f632a77a5ca6b8f [file] [log] [blame]
Greg Ward06537a52000-03-18 15:37:26 +00001"""distutils.command.clean
2
3Implements the Distutils 'clean' command."""
4
5# contributed by Bastian Kleineidam <calvin@cs.uni-sb.de>, added 2000-03-18
6
Greg Ward06537a52000-03-18 15:37:26 +00007import os
8from distutils.core import Command
Greg Wardab3a0f32000-08-05 01:31:54 +00009from distutils.dir_util import remove_tree
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000010from distutils import log
Greg Ward06537a52000-03-18 15:37:26 +000011
Collin Winter5b7e9d72007-08-30 03:52:21 +000012class clean(Command):
Greg Ward06537a52000-03-18 15:37:26 +000013
Martin v. Löwis8ed338a2005-03-03 08:12:27 +000014 description = "clean up temporary files from 'build' command"
Greg Ward06537a52000-03-18 15:37:26 +000015 user_options = [
Greg Ward11fc7e42000-03-18 17:33:18 +000016 ('build-base=', 'b',
17 "base build directory (default: 'build.build-base')"),
Greg Ward06537a52000-03-18 15:37:26 +000018 ('build-lib=', None,
Greg Ward11fc7e42000-03-18 17:33:18 +000019 "build directory for all modules (default: 'build.build-lib')"),
20 ('build-temp=', 't',
21 "temporary build directory (default: 'build.build-temp')"),
Greg Ward7ac743b2000-09-12 00:07:49 +000022 ('build-scripts=', None,
23 "build directory for scripts (default: 'build.build-scripts')"),
Gregory P. Smitha04d8072000-05-13 02:30:15 +000024 ('bdist-base=', None,
Gregory P. Smitha1902682000-05-13 01:56:55 +000025 "temporary directory for built distributions"),
Greg Ward06537a52000-03-18 15:37:26 +000026 ('all', 'a',
27 "remove all build output, not just temporary by-products")
28 ]
29
Greg Ward99b032e2000-09-25 01:41:15 +000030 boolean_options = ['all']
31
Greg Ward06537a52000-03-18 15:37:26 +000032 def initialize_options(self):
33 self.build_base = None
34 self.build_lib = None
35 self.build_temp = None
Greg Ward7ac743b2000-09-12 00:07:49 +000036 self.build_scripts = None
Gregory P. Smitha04d8072000-05-13 02:30:15 +000037 self.bdist_base = None
Greg Ward06537a52000-03-18 15:37:26 +000038 self.all = None
39
40 def finalize_options(self):
41 self.set_undefined_options('build',
Gregory P. Smitha1902682000-05-13 01:56:55 +000042 ('build_base', 'build_base'),
43 ('build_lib', 'build_lib'),
Greg Ward7ac743b2000-09-12 00:07:49 +000044 ('build_scripts', 'build_scripts'),
Gregory P. Smitha04d8072000-05-13 02:30:15 +000045 ('build_temp', 'build_temp'))
46 self.set_undefined_options('bdist',
47 ('bdist_base', 'bdist_base'))
Greg Ward06537a52000-03-18 15:37:26 +000048
49 def run(self):
Greg Ward11fc7e42000-03-18 17:33:18 +000050 # remove the build/temp.<plat> directory (unless it's already
51 # gone)
Greg Wardcb1f4c42000-09-30 18:27:54 +000052 if os.path.exists(self.build_temp):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000053 remove_tree(self.build_temp, dry_run=self.dry_run)
Greg Ward0eeebfd2000-05-28 23:47:00 +000054 else:
Jeremy Hyltona6832332002-06-06 14:54:56 +000055 log.debug("'%s' does not exist -- can't clean it",
56 self.build_temp)
Greg Ward0eeebfd2000-05-28 23:47:00 +000057
Greg Ward06537a52000-03-18 15:37:26 +000058 if self.all:
Greg Ward7ac743b2000-09-12 00:07:49 +000059 # remove build directories
60 for directory in (self.build_lib,
61 self.bdist_base,
Greg Wardfa9ff762000-10-14 04:06:40 +000062 self.build_scripts):
Greg Wardcb1f4c42000-09-30 18:27:54 +000063 if os.path.exists(directory):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000064 remove_tree(directory, dry_run=self.dry_run)
Greg Ward7ac743b2000-09-12 00:07:49 +000065 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000066 log.warn("'%s' does not exist -- can't clean it",
67 directory)
Greg Ward11fc7e42000-03-18 17:33:18 +000068
69 # just for the heck of it, try to remove the base build directory:
70 # we might have emptied it right now, but if not we don't care
71 if not self.dry_run:
72 try:
Greg Wardcb1f4c42000-09-30 18:27:54 +000073 os.rmdir(self.build_base)
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000074 log.info("removing '%s'", self.build_base)
Greg Ward11fc7e42000-03-18 17:33:18 +000075 except OSError:
76 pass