ยปCore Development>Code coverage>Lib/distutils/command/build_scripts.py

Python code coverage for Lib/distutils/command/build_scripts.py

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