Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 1 | """distutils.command.clean |
| 2 | |
| 3 | Implements the Distutils 'clean' command.""" |
| 4 | |
| 5 | # contributed by Bastian Kleineidam <calvin@cs.uni-sb.de>, added 2000-03-18 |
| 6 | |
| 7 | __revision__ = "$Id$" |
| 8 | |
| 9 | import os |
| 10 | from distutils.core import Command |
Greg Ward | ab3a0f3 | 2000-08-05 01:31:54 +0000 | [diff] [blame] | 11 | from distutils.dir_util import remove_tree |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 12 | from distutils import log |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 13 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 14 | class clean(Command): |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 15 | |
Martin v. Löwis | 8ed338a | 2005-03-03 08:12:27 +0000 | [diff] [blame] | 16 | description = "clean up temporary files from 'build' command" |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 17 | user_options = [ |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 18 | ('build-base=', 'b', |
| 19 | "base build directory (default: 'build.build-base')"), |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 20 | ('build-lib=', None, |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 21 | "build directory for all modules (default: 'build.build-lib')"), |
| 22 | ('build-temp=', 't', |
| 23 | "temporary build directory (default: 'build.build-temp')"), |
Greg Ward | 7ac743b | 2000-09-12 00:07:49 +0000 | [diff] [blame] | 24 | ('build-scripts=', None, |
| 25 | "build directory for scripts (default: 'build.build-scripts')"), |
Gregory P. Smith | a04d807 | 2000-05-13 02:30:15 +0000 | [diff] [blame] | 26 | ('bdist-base=', None, |
Gregory P. Smith | a190268 | 2000-05-13 01:56:55 +0000 | [diff] [blame] | 27 | "temporary directory for built distributions"), |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 28 | ('all', 'a', |
| 29 | "remove all build output, not just temporary by-products") |
| 30 | ] |
| 31 | |
Greg Ward | 99b032e | 2000-09-25 01:41:15 +0000 | [diff] [blame] | 32 | boolean_options = ['all'] |
| 33 | |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 34 | def initialize_options(self): |
| 35 | self.build_base = None |
| 36 | self.build_lib = None |
| 37 | self.build_temp = None |
Greg Ward | 7ac743b | 2000-09-12 00:07:49 +0000 | [diff] [blame] | 38 | self.build_scripts = None |
Gregory P. Smith | a04d807 | 2000-05-13 02:30:15 +0000 | [diff] [blame] | 39 | self.bdist_base = None |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 40 | self.all = None |
| 41 | |
| 42 | def finalize_options(self): |
| 43 | self.set_undefined_options('build', |
Gregory P. Smith | a190268 | 2000-05-13 01:56:55 +0000 | [diff] [blame] | 44 | ('build_base', 'build_base'), |
| 45 | ('build_lib', 'build_lib'), |
Greg Ward | 7ac743b | 2000-09-12 00:07:49 +0000 | [diff] [blame] | 46 | ('build_scripts', 'build_scripts'), |
Gregory P. Smith | a04d807 | 2000-05-13 02:30:15 +0000 | [diff] [blame] | 47 | ('build_temp', 'build_temp')) |
| 48 | self.set_undefined_options('bdist', |
| 49 | ('bdist_base', 'bdist_base')) |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 50 | |
| 51 | def run(self): |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 52 | # remove the build/temp.<plat> directory (unless it's already |
| 53 | # gone) |
Greg Ward | cb1f4c4 | 2000-09-30 18:27:54 +0000 | [diff] [blame] | 54 | if os.path.exists(self.build_temp): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 55 | remove_tree(self.build_temp, dry_run=self.dry_run) |
Greg Ward | 0eeebfd | 2000-05-28 23:47:00 +0000 | [diff] [blame] | 56 | else: |
Jeremy Hylton | a683233 | 2002-06-06 14:54:56 +0000 | [diff] [blame] | 57 | log.debug("'%s' does not exist -- can't clean it", |
| 58 | self.build_temp) |
Greg Ward | 0eeebfd | 2000-05-28 23:47:00 +0000 | [diff] [blame] | 59 | |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 60 | if self.all: |
Greg Ward | 7ac743b | 2000-09-12 00:07:49 +0000 | [diff] [blame] | 61 | # remove build directories |
| 62 | for directory in (self.build_lib, |
| 63 | self.bdist_base, |
Greg Ward | fa9ff76 | 2000-10-14 04:06:40 +0000 | [diff] [blame] | 64 | self.build_scripts): |
Greg Ward | cb1f4c4 | 2000-09-30 18:27:54 +0000 | [diff] [blame] | 65 | if os.path.exists(directory): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 66 | remove_tree(directory, dry_run=self.dry_run) |
Greg Ward | 7ac743b | 2000-09-12 00:07:49 +0000 | [diff] [blame] | 67 | else: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 68 | log.warn("'%s' does not exist -- can't clean it", |
| 69 | directory) |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 70 | |
| 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 Ward | cb1f4c4 | 2000-09-30 18:27:54 +0000 | [diff] [blame] | 75 | os.rmdir(self.build_base) |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 76 | log.info("removing '%s'", self.build_base) |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 77 | except OSError: |
| 78 | pass |