blob: 1844ffefd37ca5947a6bd7a4f3931649689efd6b [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
Martin v. Löwis5a6601c2004-11-10 22:23:15 +00007# This module should be kept compatible with Python 2.1.
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00008
Greg Ward06537a52000-03-18 15:37:26 +00009__revision__ = "$Id$"
10
11import os
12from distutils.core import Command
Greg Wardab3a0f32000-08-05 01:31:54 +000013from distutils.dir_util import remove_tree
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000014from distutils import log
Greg Ward06537a52000-03-18 15:37:26 +000015
16class clean (Command):
17
Greg Ward11fc7e42000-03-18 17:33:18 +000018 description = "clean up output of 'build' command"
Greg Ward06537a52000-03-18 15:37:26 +000019 user_options = [
Greg Ward11fc7e42000-03-18 17:33:18 +000020 ('build-base=', 'b',
21 "base build directory (default: 'build.build-base')"),
Greg Ward06537a52000-03-18 15:37:26 +000022 ('build-lib=', None,
Greg Ward11fc7e42000-03-18 17:33:18 +000023 "build directory for all modules (default: 'build.build-lib')"),
24 ('build-temp=', 't',
25 "temporary build directory (default: 'build.build-temp')"),
Greg Ward7ac743b2000-09-12 00:07:49 +000026 ('build-scripts=', None,
27 "build directory for scripts (default: 'build.build-scripts')"),
Gregory P. Smitha04d8072000-05-13 02:30:15 +000028 ('bdist-base=', None,
Gregory P. Smitha1902682000-05-13 01:56:55 +000029 "temporary directory for built distributions"),
Greg Ward06537a52000-03-18 15:37:26 +000030 ('all', 'a',
31 "remove all build output, not just temporary by-products")
32 ]
33
Greg Ward99b032e2000-09-25 01:41:15 +000034 boolean_options = ['all']
35
Greg Ward06537a52000-03-18 15:37:26 +000036 def initialize_options(self):
37 self.build_base = None
38 self.build_lib = None
39 self.build_temp = None
Greg Ward7ac743b2000-09-12 00:07:49 +000040 self.build_scripts = None
Gregory P. Smitha04d8072000-05-13 02:30:15 +000041 self.bdist_base = None
Greg Ward06537a52000-03-18 15:37:26 +000042 self.all = None
43
44 def finalize_options(self):
45 self.set_undefined_options('build',
Gregory P. Smitha1902682000-05-13 01:56:55 +000046 ('build_base', 'build_base'),
47 ('build_lib', 'build_lib'),
Greg Ward7ac743b2000-09-12 00:07:49 +000048 ('build_scripts', 'build_scripts'),
Gregory P. Smitha04d8072000-05-13 02:30:15 +000049 ('build_temp', 'build_temp'))
50 self.set_undefined_options('bdist',
51 ('bdist_base', 'bdist_base'))
Greg Ward06537a52000-03-18 15:37:26 +000052
53 def run(self):
Greg Ward11fc7e42000-03-18 17:33:18 +000054 # remove the build/temp.<plat> directory (unless it's already
55 # gone)
Greg Wardcb1f4c42000-09-30 18:27:54 +000056 if os.path.exists(self.build_temp):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000057 remove_tree(self.build_temp, dry_run=self.dry_run)
Greg Ward0eeebfd2000-05-28 23:47:00 +000058 else:
Jeremy Hyltona6832332002-06-06 14:54:56 +000059 log.debug("'%s' does not exist -- can't clean it",
60 self.build_temp)
Greg Ward0eeebfd2000-05-28 23:47:00 +000061
Greg Ward06537a52000-03-18 15:37:26 +000062 if self.all:
Greg Ward7ac743b2000-09-12 00:07:49 +000063 # remove build directories
64 for directory in (self.build_lib,
65 self.bdist_base,
Greg Wardfa9ff762000-10-14 04:06:40 +000066 self.build_scripts):
Greg Wardcb1f4c42000-09-30 18:27:54 +000067 if os.path.exists(directory):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000068 remove_tree(directory, dry_run=self.dry_run)
Greg Ward7ac743b2000-09-12 00:07:49 +000069 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000070 log.warn("'%s' does not exist -- can't clean it",
71 directory)
Greg Ward11fc7e42000-03-18 17:33:18 +000072
73 # just for the heck of it, try to remove the base build directory:
74 # we might have emptied it right now, but if not we don't care
75 if not self.dry_run:
76 try:
Greg Wardcb1f4c42000-09-30 18:27:54 +000077 os.rmdir(self.build_base)
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000078 log.info("removing '%s'", self.build_base)
Greg Ward11fc7e42000-03-18 17:33:18 +000079 except OSError:
80 pass
Greg Wardfcd974e2000-05-25 01:10:04 +000081
82# class clean