clean.py 2.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
"""distutils.command.clean

Implements the Distutils 'clean' command."""

# contributed by Bastian Kleineidam <calvin@cs.uni-sb.de>, added 2000-03-18

__revision__ = "$Id$"

import os
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 = [
Greg Ward's avatar
Greg Ward committed
18 19
        ('build-base=', 'b',
         "base build directory (default: 'build.build-base')"),
20
        ('build-lib=', None,
Greg Ward's avatar
Greg Ward committed
21 22 23
         "build directory for all modules (default: 'build.build-lib')"),
        ('build-temp=', 't',
         "temporary build directory (default: 'build.build-temp')"),
24 25
        ('build-scripts=', None,
         "build directory for scripts (default: 'build.build-scripts')"),
26
        ('bdist-base=', None,
27
         "temporary directory for built distributions"),
28 29 30 31
        ('all', 'a',
         "remove all build output, not just temporary by-products")
    ]

32 33
    boolean_options = ['all']

34 35 36 37
    def initialize_options(self):
        self.build_base = None
        self.build_lib = None
        self.build_temp = None
38
        self.build_scripts = None
39
        self.bdist_base = None
40 41 42 43
        self.all = None

    def finalize_options(self):
        self.set_undefined_options('build',
44 45
                                   ('build_base', 'build_base'),
                                   ('build_lib', 'build_lib'),
46
                                   ('build_scripts', 'build_scripts'),
47 48 49
                                   ('build_temp', 'build_temp'))
        self.set_undefined_options('bdist',
                                   ('bdist_base', 'bdist_base'))
50 51

    def run(self):
Greg Ward's avatar
Greg Ward committed
52 53
        # remove the build/temp.<plat> directory (unless it's already
        # gone)
54
        if os.path.exists(self.build_temp):
55
            remove_tree(self.build_temp, dry_run=self.dry_run)
56
        else:
57 58
            log.debug("'%s' does not exist -- can't clean it",
                      self.build_temp)
59

60
        if self.all:
61 62 63
            # remove build directories
            for directory in (self.build_lib,
                              self.bdist_base,
Greg Ward's avatar
Greg Ward committed
64
                              self.build_scripts):
65
                if os.path.exists(directory):
66
                    remove_tree(directory, dry_run=self.dry_run)
67
                else:
68 69
                    log.warn("'%s' does not exist -- can't clean it",
                             directory)
Greg Ward's avatar
Greg Ward committed
70 71 72 73 74

        # just for the heck of it, try to remove the base build directory:
        # we might have emptied it right now, but if not we don't care
        if not self.dry_run:
            try:
75
                os.rmdir(self.build_base)
76
                log.info("removing '%s'", self.build_base)
Greg Ward's avatar
Greg Ward committed
77 78
            except OSError:
                pass
79 80

# class clean