ยปCore Development>Code coverage>Tools/msi/make_zip.py

Python code coverage for Tools/msi/make_zip.py

#countcontent
1n/aimport argparse
2n/aimport py_compile
3n/aimport re
4n/aimport sys
5n/aimport shutil
6n/aimport stat
7n/aimport os
8n/aimport tempfile
9n/a
10n/afrom itertools import chain
11n/afrom pathlib import Path
12n/afrom zipfile import ZipFile, ZIP_DEFLATED
13n/aimport subprocess
14n/a
15n/aTKTCL_RE = re.compile(r'^(_?tk|tcl).+\.(pyd|dll)', re.IGNORECASE)
16n/aDEBUG_RE = re.compile(r'_d\.(pyd|dll|exe|pdb|lib)$', re.IGNORECASE)
17n/aPYTHON_DLL_RE = re.compile(r'python\d\d?\.dll$', re.IGNORECASE)
18n/a
19n/aDEBUG_FILES = {
20n/a '_ctypes_test',
21n/a '_testbuffer',
22n/a '_testcapi',
23n/a '_testconsole',
24n/a '_testimportmultiple',
25n/a '_testmultiphase',
26n/a 'xxlimited',
27n/a 'python3_dstub',
28n/a}
29n/a
30n/aEXCLUDE_FROM_LIBRARY = {
31n/a '__pycache__',
32n/a 'idlelib',
33n/a 'pydoc_data',
34n/a 'site-packages',
35n/a 'tkinter',
36n/a 'turtledemo',
37n/a}
38n/a
39n/aEXCLUDE_FROM_EMBEDDABLE_LIBRARY = {
40n/a 'ensurepip',
41n/a 'venv',
42n/a}
43n/a
44n/aEXCLUDE_FILE_FROM_LIBRARY = {
45n/a 'bdist_wininst.py',
46n/a}
47n/a
48n/aEXCLUDE_FILE_FROM_LIBS = {
49n/a 'liblzma',
50n/a 'ssleay',
51n/a 'libeay',
52n/a 'python3stub',
53n/a}
54n/a
55n/aEXCLUDED_FILES = {
56n/a 'pyshellext',
57n/a}
58n/a
59n/adef is_not_debug(p):
60n/a if DEBUG_RE.search(p.name):
61n/a return False
62n/a
63n/a if TKTCL_RE.search(p.name):
64n/a return False
65n/a
66n/a return p.stem.lower() not in DEBUG_FILES and p.stem.lower() not in EXCLUDED_FILES
67n/a
68n/adef is_not_debug_or_python(p):
69n/a return is_not_debug(p) and not PYTHON_DLL_RE.search(p.name)
70n/a
71n/adef include_in_lib(p):
72n/a name = p.name.lower()
73n/a if p.is_dir():
74n/a if name in EXCLUDE_FROM_LIBRARY:
75n/a return False
76n/a if name == 'test' and p.parts[-2].lower() == 'lib':
77n/a return False
78n/a if name in {'test', 'tests'} and p.parts[-3].lower() == 'lib':
79n/a return False
80n/a return True
81n/a
82n/a if name in EXCLUDE_FILE_FROM_LIBRARY:
83n/a return False
84n/a
85n/a suffix = p.suffix.lower()
86n/a return suffix not in {'.pyc', '.pyo', '.exe'}
87n/a
88n/adef include_in_embeddable_lib(p):
89n/a if p.is_dir() and p.name.lower() in EXCLUDE_FROM_EMBEDDABLE_LIBRARY:
90n/a return False
91n/a
92n/a return include_in_lib(p)
93n/a
94n/adef include_in_libs(p):
95n/a if not is_not_debug(p):
96n/a return False
97n/a
98n/a return p.stem.lower() not in EXCLUDE_FILE_FROM_LIBS
99n/a
100n/adef include_in_tools(p):
101n/a if p.is_dir() and p.name.lower() in {'scripts', 'i18n', 'pynche', 'demo', 'parser'}:
102n/a return True
103n/a
104n/a return p.suffix.lower() in {'.py', '.pyw', '.txt'}
105n/a
106n/aBASE_NAME = 'python{0.major}{0.minor}'.format(sys.version_info)
107n/a
108n/aFULL_LAYOUT = [
109n/a ('/', 'PCBuild/$arch', 'python.exe', is_not_debug),
110n/a ('/', 'PCBuild/$arch', 'pythonw.exe', is_not_debug),
111n/a ('/', 'PCBuild/$arch', 'python{}.dll'.format(sys.version_info.major), is_not_debug),
112n/a ('/', 'PCBuild/$arch', '{}.dll'.format(BASE_NAME), is_not_debug),
113n/a ('DLLs/', 'PCBuild/$arch', '*.pyd', is_not_debug),
114n/a ('DLLs/', 'PCBuild/$arch', '*.dll', is_not_debug_or_python),
115n/a ('include/', 'include', '*.h', None),
116n/a ('include/', 'PC', 'pyconfig.h', None),
117n/a ('Lib/', 'Lib', '**/*', include_in_lib),
118n/a ('libs/', 'PCBuild/$arch', '*.lib', include_in_libs),
119n/a ('Tools/', 'Tools', '**/*', include_in_tools),
120n/a]
121n/a
122n/aEMBED_LAYOUT = [
123n/a ('/', 'PCBuild/$arch', 'python*.exe', is_not_debug),
124n/a ('/', 'PCBuild/$arch', '*.pyd', is_not_debug),
125n/a ('/', 'PCBuild/$arch', '*.dll', is_not_debug),
126n/a ('{}.zip'.format(BASE_NAME), 'Lib', '**/*', include_in_embeddable_lib),
127n/a]
128n/a
129n/aif os.getenv('DOC_FILENAME'):
130n/a FULL_LAYOUT.append(('Doc/', 'Doc/build/htmlhelp', os.getenv('DOC_FILENAME'), None))
131n/aif os.getenv('VCREDIST_PATH'):
132n/a FULL_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None))
133n/a EMBED_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None))
134n/a
135n/adef copy_to_layout(target, rel_sources):
136n/a count = 0
137n/a
138n/a if target.suffix.lower() == '.zip':
139n/a if target.exists():
140n/a target.unlink()
141n/a
142n/a with ZipFile(str(target), 'w', ZIP_DEFLATED) as f:
143n/a with tempfile.TemporaryDirectory() as tmpdir:
144n/a for s, rel in rel_sources:
145n/a if rel.suffix.lower() == '.py':
146n/a pyc = Path(tmpdir) / rel.with_suffix('.pyc').name
147n/a try:
148n/a py_compile.compile(str(s), str(pyc), str(rel), doraise=True, optimize=2)
149n/a except py_compile.PyCompileError:
150n/a f.write(str(s), str(rel))
151n/a else:
152n/a f.write(str(pyc), str(rel.with_suffix('.pyc')))
153n/a else:
154n/a f.write(str(s), str(rel))
155n/a count += 1
156n/a
157n/a else:
158n/a for s, rel in rel_sources:
159n/a dest = target / rel
160n/a try:
161n/a dest.parent.mkdir(parents=True)
162n/a except FileExistsError:
163n/a pass
164n/a if dest.is_file():
165n/a dest.chmod(stat.S_IWRITE)
166n/a shutil.copy(str(s), str(dest))
167n/a if dest.is_file():
168n/a dest.chmod(stat.S_IWRITE)
169n/a count += 1
170n/a
171n/a return count
172n/a
173n/adef rglob(root, pattern, condition):
174n/a dirs = [root]
175n/a recurse = pattern[:3] in {'**/', '**\\'}
176n/a while dirs:
177n/a d = dirs.pop(0)
178n/a for f in d.glob(pattern[3:] if recurse else pattern):
179n/a if recurse and f.is_dir() and (not condition or condition(f)):
180n/a dirs.append(f)
181n/a elif f.is_file() and (not condition or condition(f)):
182n/a yield f, f.relative_to(root)
183n/a
184n/adef main():
185n/a parser = argparse.ArgumentParser()
186n/a parser.add_argument('-s', '--source', metavar='dir', help='The directory containing the repository root', type=Path)
187n/a parser.add_argument('-o', '--out', metavar='file', help='The name of the output archive', type=Path, default=None)
188n/a parser.add_argument('-t', '--temp', metavar='dir', help='A directory to temporarily extract files into', type=Path, default=None)
189n/a parser.add_argument('-e', '--embed', help='Create an embedding layout', action='store_true', default=False)
190n/a parser.add_argument('-a', '--arch', help='Specify the architecture to use (win32/amd64)', type=str, default="win32")
191n/a ns = parser.parse_args()
192n/a
193n/a source = ns.source or (Path(__file__).resolve().parent.parent.parent)
194n/a out = ns.out
195n/a arch = ns.arch
196n/a assert isinstance(source, Path)
197n/a assert not out or isinstance(out, Path)
198n/a assert isinstance(arch, str)
199n/a
200n/a if ns.temp:
201n/a temp = ns.temp
202n/a delete_temp = False
203n/a else:
204n/a temp = Path(tempfile.mkdtemp())
205n/a delete_temp = True
206n/a
207n/a if out:
208n/a try:
209n/a out.parent.mkdir(parents=True)
210n/a except FileExistsError:
211n/a pass
212n/a try:
213n/a temp.mkdir(parents=True)
214n/a except FileExistsError:
215n/a pass
216n/a
217n/a layout = EMBED_LAYOUT if ns.embed else FULL_LAYOUT
218n/a
219n/a try:
220n/a for t, s, p, c in layout:
221n/a fs = source / s.replace("$arch", arch)
222n/a files = rglob(fs, p, c)
223n/a extra_files = []
224n/a if s == 'Lib' and p == '**/*':
225n/a extra_files.append((
226n/a source / 'tools' / 'msi' / 'distutils.command.bdist_wininst.py',
227n/a Path('distutils') / 'command' / 'bdist_wininst.py'
228n/a ))
229n/a copied = copy_to_layout(temp / t.rstrip('/'), chain(files, extra_files))
230n/a print('Copied {} files'.format(copied))
231n/a
232n/a if ns.embed:
233n/a with open(str(temp / (BASE_NAME + '._pth')), 'w') as f:
234n/a print(BASE_NAME + '.zip', file=f)
235n/a print('.', file=f)
236n/a print('', file=f)
237n/a print('# Uncomment to run site.main() automatically', file=f)
238n/a print('#import site', file=f)
239n/a
240n/a if out:
241n/a total = copy_to_layout(out, rglob(temp, '**/*', None))
242n/a print('Wrote {} files to {}'.format(total, out))
243n/a finally:
244n/a if delete_temp:
245n/a shutil.rmtree(temp, True)
246n/a
247n/a
248n/aif __name__ == "__main__":
249n/a sys.exit(int(main() or 0))