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 |
| 11 | from distutils.util import remove_tree |
| 12 | |
| 13 | class clean (Command): |
| 14 | |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 15 | description = "clean up output of 'build' command" |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 16 | user_options = [ |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 17 | ('build-base=', 'b', |
| 18 | "base build directory (default: 'build.build-base')"), |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 19 | ('build-lib=', None, |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 20 | "build directory for all modules (default: 'build.build-lib')"), |
| 21 | ('build-temp=', 't', |
| 22 | "temporary build directory (default: 'build.build-temp')"), |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 23 | ('all', 'a', |
| 24 | "remove all build output, not just temporary by-products") |
| 25 | ] |
| 26 | |
| 27 | def initialize_options(self): |
| 28 | self.build_base = None |
| 29 | self.build_lib = None |
| 30 | self.build_temp = None |
| 31 | self.all = None |
| 32 | |
| 33 | def finalize_options(self): |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 34 | if self.build_lib and not os.path.exists (self.build_lib): |
| 35 | self.warn ("'%s' does not exist -- can't clean it" % |
| 36 | self.build_lib) |
| 37 | if self.build_temp and not os.path.exists (self.build_temp): |
| 38 | self.warn ("'%s' does not exist -- can't clean it" % |
| 39 | self.build_temp) |
| 40 | |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 41 | self.set_undefined_options('build', |
| 42 | ('build_base', 'build_base'), |
| 43 | ('build_lib', 'build_lib'), |
| 44 | ('build_temp', 'build_temp')) |
| 45 | |
| 46 | def run(self): |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 47 | # remove the build/temp.<plat> directory (unless it's already |
| 48 | # gone) |
| 49 | if os.path.exists (self.build_temp): |
| 50 | remove_tree (self.build_temp, self.verbose, self.dry_run) |
Greg Ward | 06537a5 | 2000-03-18 15:37:26 +0000 | [diff] [blame] | 51 | |
| 52 | if self.all: |
Greg Ward | 11fc7e4 | 2000-03-18 17:33:18 +0000 | [diff] [blame] | 53 | # remove the module build directory (unless already gone) |
| 54 | if os.path.exists (self.build_lib): |
| 55 | remove_tree (self.build_lib, self.verbose, self.dry_run) |
| 56 | |
| 57 | # just for the heck of it, try to remove the base build directory: |
| 58 | # we might have emptied it right now, but if not we don't care |
| 59 | if not self.dry_run: |
| 60 | try: |
| 61 | os.rmdir (self.build_base) |
| 62 | self.announce ("removing '%s'" % self.build_base) |
| 63 | except OSError: |
| 64 | pass |