• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""distutils.command.build_scripts
2
3Implements the Distutils 'build_scripts' command."""
4
5import os, re
6from stat import ST_MODE
7from distutils import sysconfig
8from distutils.core import Command
9from distutils.dep_util import newer
10from distutils.util import convert_path
11from distutils import log
12import tokenize
13
14# check if Python is called on the first line with this expression
15first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$')
16
17class build_scripts(Command):
18
19    description = "\"build\" scripts (copy and fixup #! line)"
20
21    user_options = [
22        ('build-dir=', 'd', "directory to \"build\" (copy) to"),
23        ('force', 'f', "forcibly build everything (ignore file timestamps"),
24        ('executable=', 'e', "specify final destination interpreter path"),
25        ]
26
27    boolean_options = ['force']
28
29
30    def initialize_options(self):
31        self.build_dir = None
32        self.scripts = None
33        self.force = None
34        self.executable = None
35        self.outfiles = None
36
37    def finalize_options(self):
38        self.set_undefined_options('build',
39                                   ('build_scripts', 'build_dir'),
40                                   ('force', 'force'),
41                                   ('executable', 'executable'))
42        self.scripts = self.distribution.scripts
43
44    def get_source_files(self):
45        return self.scripts
46
47    def run(self):
48        if not self.scripts:
49            return
50        self.copy_scripts()
51
52
53    def copy_scripts(self):
54        r"""Copy each script listed in 'self.scripts'; if it's marked as a
55        Python script in the Unix way (first line matches 'first_line_re',
56        ie. starts with "\#!" and contains "python"), then adjust the first
57        line to refer to the current Python interpreter as we copy.
58        """
59        self.mkpath(self.build_dir)
60        outfiles = []
61        updated_files = []
62        for script in self.scripts:
63            adjust = False
64            script = convert_path(script)
65            outfile = os.path.join(self.build_dir, os.path.basename(script))
66            outfiles.append(outfile)
67
68            if not self.force and not newer(script, outfile):
69                log.debug("not copying %s (up-to-date)", script)
70                continue
71
72            # Always open the file, but ignore failures in dry-run mode --
73            # that way, we'll get accurate feedback if we can read the
74            # script.
75            try:
76                f = open(script, "rb")
77            except OSError:
78                if not self.dry_run:
79                    raise
80                f = None
81            else:
82                encoding, lines = tokenize.detect_encoding(f.readline)
83                f.seek(0)
84                first_line = f.readline()
85                if not first_line:
86                    self.warn("%s is an empty file (skipping)" % script)
87                    continue
88
89                match = first_line_re.match(first_line)
90                if match:
91                    adjust = True
92                    post_interp = match.group(1) or b''
93
94            if adjust:
95                log.info("copying and adjusting %s -> %s", script,
96                         self.build_dir)
97                updated_files.append(outfile)
98                if not self.dry_run:
99                    if not sysconfig.python_build:
100                        executable = self.executable
101                    else:
102                        executable = os.path.join(
103                            sysconfig.get_config_var("BINDIR"),
104                           "python%s%s" % (sysconfig.get_config_var("VERSION"),
105                                           sysconfig.get_config_var("EXE")))
106                    executable = os.fsencode(executable)
107                    shebang = b"#!" + executable + post_interp + b"\n"
108                    # Python parser starts to read a script using UTF-8 until
109                    # it gets a #coding:xxx cookie. The shebang has to be the
110                    # first line of a file, the #coding:xxx cookie cannot be
111                    # written before. So the shebang has to be decodable from
112                    # UTF-8.
113                    try:
114                        shebang.decode('utf-8')
115                    except UnicodeDecodeError:
116                        raise ValueError(
117                            "The shebang ({!r}) is not decodable "
118                            "from utf-8".format(shebang))
119                    # If the script is encoded to a custom encoding (use a
120                    # #coding:xxx cookie), the shebang has to be decodable from
121                    # the script encoding too.
122                    try:
123                        shebang.decode(encoding)
124                    except UnicodeDecodeError:
125                        raise ValueError(
126                            "The shebang ({!r}) is not decodable "
127                            "from the script encoding ({})"
128                            .format(shebang, encoding))
129                    with open(outfile, "wb") as outf:
130                        outf.write(shebang)
131                        outf.writelines(f.readlines())
132                if f:
133                    f.close()
134            else:
135                if f:
136                    f.close()
137                updated_files.append(outfile)
138                self.copy_file(script, outfile)
139
140        if os.name == 'posix':
141            for file in outfiles:
142                if self.dry_run:
143                    log.info("changing mode of %s", file)
144                else:
145                    oldmode = os.stat(file)[ST_MODE] & 0o7777
146                    newmode = (oldmode | 0o555) & 0o7777
147                    if newmode != oldmode:
148                        log.info("changing mode of %s from %o to %o",
149                                 file, oldmode, newmode)
150                        os.chmod(file, newmode)
151        # XXX should we modify self.outfiles?
152        return outfiles, updated_files
153