build_scripts.py 4.42 KB
Newer Older
1 2 3 4
"""distutils.command.build_scripts

Implements the Distutils 'build_scripts' command."""

5
# This module should be kept compatible with Python 2.1.
6

7 8
__revision__ = "$Id$"

9
import sys, os, re
10
from stat import ST_MODE
Michael W. Hudson's avatar
Michael W. Hudson committed
11
from distutils import sysconfig
12
from distutils.core import Command
13
from distutils.dep_util import newer
14
from distutils.util import convert_path
15
from distutils import log
16

17
# check if Python is called on the first line with this expression
18
first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
19 20 21

class build_scripts (Command):

22
    description = "\"build\" scripts (copy and fixup #! line)"
23 24 25 26

    user_options = [
        ('build-dir=', 'd', "directory to \"build\" (copy) to"),
        ('force', 'f', "forcibly build everything (ignore file timestamps"),
27
        ('executable=', 'e', "specify final destination interpreter path"),
28 29
        ]

30 31
    boolean_options = ['force']

32 33 34 35 36

    def initialize_options (self):
        self.build_dir = None
        self.scripts = None
        self.force = None
37
        self.executable = None
38 39 40
        self.outfiles = None

    def finalize_options (self):
41 42
        self.set_undefined_options('build',
                                   ('build_scripts', 'build_dir'),
43 44
                                   ('force', 'force'),
                                   ('executable', 'executable'))
45 46
        self.scripts = self.distribution.scripts

47 48
    def get_source_files(self):
        return self.scripts
49 50 51 52

    def run (self):
        if not self.scripts:
            return
53 54
        self.copy_scripts()

55

56 57 58 59
    def copy_scripts (self):
        """Copy each script listed in 'self.scripts'; if it's marked as a
        Python script in the Unix way (first line matches 'first_line_re',
        ie. starts with "\#!" and contains "python"), then adjust the first
60
        line to refer to the current Python interpreter as we copy.
61
        """
62
        self.mkpath(self.build_dir)
63
        outfiles = []
64 65
        for script in self.scripts:
            adjust = 0
66
            script = convert_path(script)
67
            outfile = os.path.join(self.build_dir, os.path.basename(script))
68
            outfiles.append(outfile)
69 70

            if not self.force and not newer(script, outfile):
71
                log.debug("not copying %s (up-to-date)", script)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                continue

            # Always open the file, but ignore failures in dry-run mode --
            # that way, we'll get accurate feedback if we can read the
            # script.
            try:
                f = open(script, "r")
            except IOError:
                if not self.dry_run:
                    raise
                f = None
            else:
                first_line = f.readline()
                if not first_line:
                    self.warn("%s is an empty file (skipping)" % script)
87
                    continue
88 89 90 91

                match = first_line_re.match(first_line)
                if match:
                    adjust = 1
92
                    post_interp = match.group(1) or ''
93 94

            if adjust:
95 96
                log.info("copying and adjusting %s -> %s", script,
                         self.build_dir)
97 98
                if not self.dry_run:
                    outf = open(outfile, "w")
Michael W. Hudson's avatar
Michael W. Hudson committed
99
                    if not sysconfig.python_build:
100
                        outf.write("#!%s%s\n" %
101
                                   (self.executable,
Michael W. Hudson's avatar
Michael W. Hudson committed
102 103
                                    post_interp))
                    else:
104
                        outf.write("#!%s%s\n" %
Michael W. Hudson's avatar
Michael W. Hudson committed
105 106 107 108
                                   (os.path.join(
                            sysconfig.get_config_var("BINDIR"),
                            "python" + sysconfig.get_config_var("EXE")),
                                    post_interp))
109 110 111 112 113 114 115 116
                    outf.writelines(f.readlines())
                    outf.close()
                if f:
                    f.close()
            else:
                f.close()
                self.copy_file(script, outfile)

117 118 119 120 121
        if os.name == 'posix':
            for file in outfiles:
                if self.dry_run:
                    log.info("changing mode of %s", file)
                else:
122 123 124 125 126 127
                    oldmode = os.stat(file)[ST_MODE] & 07777
                    newmode = (oldmode | 0555) & 07777
                    if newmode != oldmode:
                        log.info("changing mode of %s from %o to %o",
                                 file, oldmode, newmode)
                        os.chmod(file, newmode)
128

129 130 131
    # copy_scripts ()

# class build_scripts