Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame^] | 1 | """Clean up temporary files created by the build command.""" |
| 2 | |
| 3 | # Contributed by Bastian Kleineidam <calvin@cs.uni-sb.de> |
| 4 | |
| 5 | import os |
| 6 | from shutil import rmtree |
| 7 | from packaging.command.cmd import Command |
| 8 | from packaging import logger |
| 9 | |
| 10 | class clean(Command): |
| 11 | |
| 12 | description = "clean up temporary files from 'build' command" |
| 13 | user_options = [ |
| 14 | ('build-base=', 'b', |
| 15 | "base build directory (default: 'build.build-base')"), |
| 16 | ('build-lib=', None, |
| 17 | "build directory for all modules (default: 'build.build-lib')"), |
| 18 | ('build-temp=', 't', |
| 19 | "temporary build directory (default: 'build.build-temp')"), |
| 20 | ('build-scripts=', None, |
| 21 | "build directory for scripts (default: 'build.build-scripts')"), |
| 22 | ('bdist-base=', None, |
| 23 | "temporary directory for built distributions"), |
| 24 | ('all', 'a', |
| 25 | "remove all build output, not just temporary by-products") |
| 26 | ] |
| 27 | |
| 28 | boolean_options = ['all'] |
| 29 | |
| 30 | def initialize_options(self): |
| 31 | self.build_base = None |
| 32 | self.build_lib = None |
| 33 | self.build_temp = None |
| 34 | self.build_scripts = None |
| 35 | self.bdist_base = None |
| 36 | self.all = None |
| 37 | |
| 38 | def finalize_options(self): |
| 39 | self.set_undefined_options('build', 'build_base', 'build_lib', |
| 40 | 'build_scripts', 'build_temp') |
| 41 | self.set_undefined_options('bdist', 'bdist_base') |
| 42 | |
| 43 | def run(self): |
| 44 | # remove the build/temp.<plat> directory (unless it's already |
| 45 | # gone) |
| 46 | if os.path.exists(self.build_temp): |
| 47 | if self.dry_run: |
| 48 | logger.info('removing %s', self.build_temp) |
| 49 | else: |
| 50 | rmtree(self.build_temp) |
| 51 | else: |
| 52 | logger.debug("'%s' does not exist -- can't clean it", |
| 53 | self.build_temp) |
| 54 | |
| 55 | if self.all: |
| 56 | # remove build directories |
| 57 | for directory in (self.build_lib, |
| 58 | self.bdist_base, |
| 59 | self.build_scripts): |
| 60 | if os.path.exists(directory): |
| 61 | if self.dry_run: |
| 62 | logger.info('removing %s', directory) |
| 63 | else: |
| 64 | rmtree(directory) |
| 65 | else: |
| 66 | logger.warning("'%s' does not exist -- can't clean it", |
| 67 | directory) |
| 68 | |
| 69 | # just for the heck of it, try to remove the base build directory: |
| 70 | # we might have emptied it right now, but if not we don't care |
| 71 | if not self.dry_run: |
| 72 | try: |
| 73 | os.rmdir(self.build_base) |
| 74 | logger.info("removing '%s'", self.build_base) |
| 75 | except OSError: |
| 76 | pass |