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

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

#countcontent
1n/a"""Build scripts (copy to build dir and fix up shebang line)."""
2n/a
3n/aimport os
4n/aimport re
5n/aimport sysconfig
6n/afrom tokenize import detect_encoding
7n/a
8n/afrom packaging.command.cmd import Command
9n/afrom packaging.util import convert_path, newer
10n/afrom packaging import logger
11n/afrom packaging.compat import Mixin2to3
12n/a
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, Mixin2to3):
18n/a
19n/a description = "build scripts (copy and fix up shebang 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 self.use_2to3 = False
37n/a self.convert_2to3_doctests = None
38n/a self.use_2to3_fixers = None
39n/a
40n/a def finalize_options(self):
41n/a self.set_undefined_options('build',
42n/a ('build_scripts', 'build_dir'),
43n/a 'use_2to3', 'use_2to3_fixers',
44n/a 'convert_2to3_doctests', 'force',
45n/a 'executable')
46n/a self.scripts = self.distribution.scripts
47n/a
48n/a def get_source_files(self):
49n/a return self.scripts
50n/a
51n/a def run(self):
52n/a if not self.scripts:
53n/a return
54n/a copied_files = self.copy_scripts()
55n/a if self.use_2to3 and copied_files:
56n/a self._run_2to3(copied_files, fixers=self.use_2to3_fixers)
57n/a
58n/a def copy_scripts(self):
59n/a """Copy each script listed in 'self.scripts'; if it's marked as a
60n/a Python script in the Unix way (first line matches 'first_line_re',
61n/a ie. starts with "\#!" and contains "python"), then adjust the first
62n/a line to refer to the current Python interpreter as we copy.
63n/a """
64n/a self.mkpath(self.build_dir)
65n/a outfiles = []
66n/a for script in self.scripts:
67n/a adjust = False
68n/a script = convert_path(script)
69n/a outfile = os.path.join(self.build_dir, os.path.basename(script))
70n/a outfiles.append(outfile)
71n/a
72n/a if not self.force and not newer(script, outfile):
73n/a logger.debug("not copying %s (up-to-date)", script)
74n/a continue
75n/a
76n/a # Always open the file, but ignore failures in dry-run mode --
77n/a # that way, we'll get accurate feedback if we can read the
78n/a # script.
79n/a try:
80n/a f = open(script, "rb")
81n/a except IOError:
82n/a if not self.dry_run:
83n/a raise
84n/a f = None
85n/a else:
86n/a encoding, lines = detect_encoding(f.readline)
87n/a f.seek(0)
88n/a first_line = f.readline()
89n/a if not first_line:
90n/a logger.warning('%s: %s is an empty file (skipping)',
91n/a self.get_command_name(), script)
92n/a continue
93n/a
94n/a match = first_line_re.match(first_line)
95n/a if match:
96n/a adjust = True
97n/a post_interp = match.group(1) or b''
98n/a
99n/a if adjust:
100n/a logger.info("copying and adjusting %s -> %s", script,
101n/a self.build_dir)
102n/a if not self.dry_run:
103n/a if not sysconfig.is_python_build():
104n/a executable = self.executable
105n/a else:
106n/a executable = os.path.join(
107n/a sysconfig.get_config_var("BINDIR"),
108n/a "python%s%s" % (sysconfig.get_config_var("VERSION"),
109n/a sysconfig.get_config_var("EXE")))
110n/a executable = os.fsencode(executable)
111n/a shebang = b"#!" + executable + post_interp + b"\n"
112n/a # Python parser starts to read a script using UTF-8 until
113n/a # it gets a #coding:xxx cookie. The shebang has to be the
114n/a # first line of a file, the #coding:xxx cookie cannot be
115n/a # written before. So the shebang has to be decodable from
116n/a # UTF-8.
117n/a try:
118n/a shebang.decode('utf-8')
119n/a except UnicodeDecodeError:
120n/a raise ValueError(
121n/a "The shebang ({!r}) is not decodable "
122n/a "from utf-8".format(shebang))
123n/a # If the script is encoded to a custom encoding (use a
124n/a # #coding:xxx cookie), the shebang has to be decodable from
125n/a # the script encoding too.
126n/a try:
127n/a shebang.decode(encoding)
128n/a except UnicodeDecodeError:
129n/a raise ValueError(
130n/a "The shebang ({!r}) is not decodable "
131n/a "from the script encoding ({})"
132n/a .format(shebang, encoding))
133n/a with open(outfile, "wb") as outf:
134n/a outf.write(shebang)
135n/a outf.writelines(f.readlines())
136n/a if f:
137n/a f.close()
138n/a else:
139n/a if f:
140n/a f.close()
141n/a self.copy_file(script, outfile)
142n/a
143n/a if os.name == 'posix':
144n/a for file in outfiles:
145n/a if self.dry_run:
146n/a logger.info("changing mode of %s", file)
147n/a else:
148n/a oldmode = os.stat(file).st_mode & 0o7777
149n/a newmode = (oldmode | 0o555) & 0o7777
150n/a if newmode != oldmode:
151n/a logger.info("changing mode of %s from %o to %o",
152n/a file, oldmode, newmode)
153n/a os.chmod(file, newmode)
154n/a return outfiles