build_scripts.py 3.59 KB
Newer Older
1 2 3 4 5 6 7 8
"""distutils.command.build_scripts

Implements the Distutils 'build_scripts' command."""

# created 2000/05/23, Bastian Kleineidam

__revision__ = "$Id$"

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

15
# check if Python is called on the first line with this expression
16
first_line_re = re.compile(r'^#!.*python(\s+.*)?$')
17 18 19

class build_scripts (Command):

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

    user_options = [
        ('build-dir=', 'd', "directory to \"build\" (copy) to"),
        ('force', 'f', "forcibly build everything (ignore file timestamps"),
        ]

27 28
    boolean_options = ['force']

29 30 31 32 33 34 35 36

    def initialize_options (self):
        self.build_dir = None
        self.scripts = None
        self.force = None
        self.outfiles = None

    def finalize_options (self):
37 38 39
        self.set_undefined_options('build',
                                   ('build_scripts', 'build_dir'),
                                   ('force', 'force'))
40 41 42 43 44 45
        self.scripts = self.distribution.scripts


    def run (self):
        if not self.scripts:
            return
46 47
        self.copy_scripts()

48

49 50 51 52
    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
53
        line to refer to the current Python interpreter as we copy.
54
        """
55
        self.mkpath(self.build_dir)
56 57
        for script in self.scripts:
            adjust = 0
58
            script = convert_path(script)
59
            outfile = os.path.join(self.build_dir, os.path.basename(script))
60 61

            if not self.force and not newer(script, outfile):
62
                self.announce("not copying %s (up-to-date)" % script)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
                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)
78
                    continue
79 80 81 82 83 84 85 86 87 88 89

                match = first_line_re.match(first_line)
                if match:
                    adjust = 1
                    post_interp = match.group(1)

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

    # copy_scripts ()

# class build_scripts