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