blob: 8fddeb453a9fe281f06736e216e0261145d355c1 [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
7__revision__ = "$Id$"
8
9import os
10from distutils.core import Command
Greg Wardab3a0f32000-08-05 01:31:54 +000011from distutils.dir_util import remove_tree
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000012from distutils import log
Greg Ward06537a52000-03-18 15:37:26 +000013
14class clean (Command):
15
Greg Ward11fc7e42000-03-18 17:33:18 +000016 description = "clean up output of 'build' command"
Greg Ward06537a52000-03-18 15:37:26 +000017 user_options = [
Greg Ward11fc7e42000-03-18 17:33:18 +000018 ('build-base=', 'b',
19 "base build directory (default: 'build.build-base')"),
Greg Ward06537a52000-03-18 15:37:26 +000020 ('build-lib=', None,
Greg Ward11fc7e42000-03-18 17:33:18 +000021 "build directory for all modules (default: 'build.build-lib')"),
22 ('build-temp=', 't',
23 "temporary build directory (default: 'build.build-temp')"),
Greg Ward7ac743b2000-09-12 00:07:49 +000024 ('build-scripts=', None,
25 "build directory for scripts (default: 'build.build-scripts')"),
Gregory P. Smitha04d8072000-05-13 02:30:15 +000026 ('bdist-base=', None,
Gregory P. Smitha1902682000-05-13 01:56:55 +000027 "temporary directory for built distributions"),
Greg Ward06537a52000-03-18 15:37:26 +000028 ('all', 'a',
29 "remove all build output, not just temporary by-products")
30 ]
31
Greg Ward99b032e2000-09-25 01:41:15 +000032 boolean_options = ['all']
33
Greg Ward06537a52000-03-18 15:37:26 +000034 def initialize_options(self):
35 self.build_base = None
36 self.build_lib = None
37 self.build_temp = None
Greg Ward7ac743b2000-09-12 00:07:49 +000038 self.build_scripts = None
Gregory P. Smitha04d8072000-05-13 02:30:15 +000039 self.bdist_base = None
Greg Ward06537a52000-03-18 15:37:26 +000040 self.all = None
41
42 def finalize_options(self):
43 self.set_undefined_options('build',
Gregory P. Smitha1902682000-05-13 01:56:55 +000044 ('build_base', 'build_base'),
45 ('build_lib', 'build_lib'),
Greg Ward7ac743b2000-09-12 00:07:49 +000046 ('build_scripts', 'build_scripts'),
Gregory P. Smitha04d8072000-05-13 02:30:15 +000047 ('build_temp', 'build_temp'))
48 self.set_undefined_options('bdist',
49 ('bdist_base', 'bdist_base'))
Greg Ward06537a52000-03-18 15:37:26 +000050
51 def run(self):
Greg Ward11fc7e42000-03-18 17:33:18 +000052 # remove the build/temp.<plat> directory (unless it's already
53 # gone)
Greg Wardcb1f4c42000-09-30 18:27:54 +000054 if os.path.exists(self.build_temp):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000055 remove_tree(self.build_temp, dry_run=self.dry_run)
Greg Ward0eeebfd2000-05-28 23:47:00 +000056 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000057 log.warn("'%s' does not exist -- can't clean it",
58 self.build_temp)
Greg Ward0eeebfd2000-05-28 23:47:00 +000059
Greg Ward06537a52000-03-18 15:37:26 +000060 if self.all:
Greg Ward7ac743b2000-09-12 00:07:49 +000061 # remove build directories
62 for directory in (self.build_lib,
63 self.bdist_base,
Greg Wardfa9ff762000-10-14 04:06:40 +000064 self.build_scripts):
Greg Wardcb1f4c42000-09-30 18:27:54 +000065 if os.path.exists(directory):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000066 remove_tree(directory, dry_run=self.dry_run)
Greg Ward7ac743b2000-09-12 00:07:49 +000067 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000068 log.warn("'%s' does not exist -- can't clean it",
69 directory)
Greg Ward11fc7e42000-03-18 17:33:18 +000070
71 # just for the heck of it, try to remove the base build directory:
72 # we might have emptied it right now, but if not we don't care
73 if not self.dry_run:
74 try:
Greg Wardcb1f4c42000-09-30 18:27:54 +000075 os.rmdir(self.build_base)
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000076 log.info("removing '%s'", self.build_base)
Greg Ward11fc7e42000-03-18 17:33:18 +000077 except OSError:
78 pass
Greg Wardfcd974e2000-05-25 01:10:04 +000079
80# class clean