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