Home | History | Annotate | Download | only in command
      1 """distutils.command.clean
      2 
      3 Implements the Distutils 'clean' command."""
      4 
      5 # contributed by Bastian Kleineidam <calvin (at] 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.dir_util import remove_tree
     12 from distutils import log
     13 
     14 class clean(Command):
     15 
     16     description = "clean up temporary files from 'build' command"
     17     user_options = [
     18         ('build-base=', 'b',
     19          "base build directory (default: 'build.build-base')"),
     20         ('build-lib=', None,
     21          "build directory for all modules (default: 'build.build-lib')"),
     22         ('build-temp=', 't',
     23          "temporary build directory (default: 'build.build-temp')"),
     24         ('build-scripts=', None,
     25          "build directory for scripts (default: 'build.build-scripts')"),
     26         ('bdist-base=', None,
     27          "temporary directory for built distributions"),
     28         ('all', 'a',
     29          "remove all build output, not just temporary by-products")
     30     ]
     31 
     32     boolean_options = ['all']
     33 
     34     def initialize_options(self):
     35         self.build_base = None
     36         self.build_lib = None
     37         self.build_temp = None
     38         self.build_scripts = None
     39         self.bdist_base = None
     40         self.all = None
     41 
     42     def finalize_options(self):
     43         self.set_undefined_options('build',
     44                                    ('build_base', 'build_base'),
     45                                    ('build_lib', 'build_lib'),
     46                                    ('build_scripts', 'build_scripts'),
     47                                    ('build_temp', 'build_temp'))
     48         self.set_undefined_options('bdist',
     49                                    ('bdist_base', 'bdist_base'))
     50 
     51     def run(self):
     52         # remove the build/temp.<plat> directory (unless it's already
     53         # gone)
     54         if os.path.exists(self.build_temp):
     55             remove_tree(self.build_temp, dry_run=self.dry_run)
     56         else:
     57             log.debug("'%s' does not exist -- can't clean it",
     58                       self.build_temp)
     59 
     60         if self.all:
     61             # remove build directories
     62             for directory in (self.build_lib,
     63                               self.bdist_base,
     64                               self.build_scripts):
     65                 if os.path.exists(directory):
     66                     remove_tree(directory, dry_run=self.dry_run)
     67                 else:
     68                     log.warn("'%s' does not exist -- can't clean it",
     69                              directory)
     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:
     75                 os.rmdir(self.build_base)
     76                 log.info("removing '%s'", self.build_base)
     77             except OSError:
     78                 pass
     79 
     80 # class clean
     81