10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""distutils.command.clean
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoImplements the Distutils 'clean' command."""
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# contributed by Bastian Kleineidam <calvin@cs.uni-sb.de>, added 2000-03-18
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__revision__ = "$Id$"
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport os
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom distutils.core import Command
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom distutils.dir_util import remove_tree
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom distutils import log
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass clean(Command):
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    description = "clean up temporary files from 'build' command"
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    user_options = [
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-base=', 'b',
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "base build directory (default: 'build.build-base')"),
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-lib=', None,
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "build directory for all modules (default: 'build.build-lib')"),
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-temp=', 't',
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "temporary build directory (default: 'build.build-temp')"),
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('build-scripts=', None,
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "build directory for scripts (default: 'build.build-scripts')"),
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('bdist-base=', None,
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "temporary directory for built distributions"),
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('all', 'a',
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         "remove all build output, not just temporary by-products")
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ]
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    boolean_options = ['all']
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def initialize_options(self):
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_base = None
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_lib = None
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_temp = None
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.build_scripts = None
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.bdist_base = None
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.all = None
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def finalize_options(self):
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.set_undefined_options('build',
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   ('build_base', 'build_base'),
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   ('build_lib', 'build_lib'),
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   ('build_scripts', 'build_scripts'),
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   ('build_temp', 'build_temp'))
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.set_undefined_options('bdist',
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   ('bdist_base', 'bdist_base'))
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def run(self):
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # remove the build/temp.<plat> directory (unless it's already
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # gone)
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if os.path.exists(self.build_temp):
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            remove_tree(self.build_temp, dry_run=self.dry_run)
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            log.debug("'%s' does not exist -- can't clean it",
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      self.build_temp)
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.all:
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # remove build directories
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for directory in (self.build_lib,
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              self.bdist_base,
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              self.build_scripts):
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if os.path.exists(directory):
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    remove_tree(directory, dry_run=self.dry_run)
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    log.warn("'%s' does not exist -- can't clean it",
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             directory)
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # just for the heck of it, try to remove the base build directory:
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # we might have emptied it right now, but if not we don't care
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not self.dry_run:
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                os.rmdir(self.build_base)
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                log.info("removing '%s'", self.build_base)
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except OSError:
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# class clean
81