Skip to content

Commit b679b74

Browse files
authored
[3.13] Backport PyManager support to PC/layout script (GH-135096)
1 parent b21d15f commit b679b74

File tree

4 files changed

+316
-2
lines changed

4 files changed

+316
-2
lines changed

PC/layout/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22

33
try:
4-
import layout
4+
import layout # noqa: F401
55
except ImportError:
66
# Failed to import our package, which likely means we were started directly
77
# Add the additional search path needed to locate our module.

PC/layout/main.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
__version__ = "3.8"
99

1010
import argparse
11+
import json
1112
import os
1213
import shutil
1314
import sys
@@ -28,6 +29,7 @@
2829
from .support.options import *
2930
from .support.pip import *
3031
from .support.props import *
32+
from .support.pymanager import *
3133
from .support.nuspec import *
3234

3335
TEST_PYDS_ONLY = FileStemSet("xxlimited", "xxlimited_35", "_ctypes_test", "_test*")
@@ -265,7 +267,12 @@ def _c(d):
265267
if ns.include_dev:
266268
for dest, src in rglob(ns.source / "Include", "**/*.h"):
267269
yield "include/{}".format(dest), src
268-
yield "include/pyconfig.h", ns.build / "pyconfig.h"
270+
# Support for layout of new and old releases.
271+
pc = ns.source / "PC"
272+
if (pc / "pyconfig.h.in").is_file():
273+
yield "include/pyconfig.h", ns.build / "pyconfig.h"
274+
else:
275+
yield "include/pyconfig.h", pc / "pyconfig.h"
269276

270277
for dest, src in get_tcltk_lib(ns):
271278
yield dest, src
@@ -303,6 +310,9 @@ def _c(d):
303310
else:
304311
yield "DLLs/{}".format(ns.include_cat.name), ns.include_cat
305312

313+
if ns.include_install_json or ns.include_install_embed_json or ns.include_install_test_json:
314+
yield "__install__.json", ns.temp / "__install__.json"
315+
306316

307317
def _compile_one_py(src, dest, name, optimize, checked=True):
308318
import py_compile
@@ -394,6 +404,22 @@ def generate_source_files(ns):
394404
log_info("Extracting pip")
395405
extract_pip_files(ns)
396406

407+
if ns.include_install_json:
408+
log_info("Generating __install__.json in {}", ns.temp)
409+
ns.temp.mkdir(parents=True, exist_ok=True)
410+
with open(ns.temp / "__install__.json", "w", encoding="utf-8") as f:
411+
json.dump(calculate_install_json(ns), f, indent=2)
412+
elif ns.include_install_embed_json:
413+
log_info("Generating embeddable __install__.json in {}", ns.temp)
414+
ns.temp.mkdir(parents=True, exist_ok=True)
415+
with open(ns.temp / "__install__.json", "w", encoding="utf-8") as f:
416+
json.dump(calculate_install_json(ns, for_embed=True), f, indent=2)
417+
elif ns.include_install_test_json:
418+
log_info("Generating test __install__.json in {}", ns.temp)
419+
ns.temp.mkdir(parents=True, exist_ok=True)
420+
with open(ns.temp / "__install__.json", "w", encoding="utf-8") as f:
421+
json.dump(calculate_install_json(ns, for_test=True), f, indent=2)
422+
397423

398424
def _create_zip_file(ns):
399425
if not ns.zip:
@@ -627,6 +653,7 @@ def main():
627653
if ns.include_cat and not ns.include_cat.is_absolute():
628654
ns.include_cat = (Path.cwd() / ns.include_cat).resolve()
629655
if not ns.arch:
656+
# TODO: Calculate arch from files in ns.build instead
630657
if sys.winver.endswith("-arm64"):
631658
ns.arch = "arm64"
632659
elif sys.winver.endswith("-32"):

PC/layout/support/options.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def public(f):
3636
"alias": {"help": "aliased python.exe entry-point binaries"},
3737
"alias3": {"help": "aliased python3.exe entry-point binaries"},
3838
"alias3x": {"help": "aliased python3.x.exe entry-point binaries"},
39+
"install-json": {"help": "a PyManager __install__.json file"},
40+
"install-embed-json": {"help": "a PyManager __install__.json file for embeddable distro"},
41+
"install-test-json": {"help": "a PyManager __install__.json for the test distro"},
3942
}
4043

4144

@@ -95,6 +98,34 @@ def public(f):
9598
"precompile",
9699
],
97100
},
101+
"pymanager": {
102+
"help": "PyManager package",
103+
"options": [
104+
"stable",
105+
"pip",
106+
"tcltk",
107+
"idle",
108+
"venv",
109+
"dev",
110+
"html-doc",
111+
"install-json",
112+
],
113+
},
114+
"pymanager-test": {
115+
"help": "PyManager test package",
116+
"options": [
117+
"stable",
118+
"pip",
119+
"tcltk",
120+
"idle",
121+
"venv",
122+
"dev",
123+
"html-doc",
124+
"symbols",
125+
"tests",
126+
"install-test-json",
127+
],
128+
},
98129
}
99130

100131

PC/layout/support/pymanager.py

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
from .constants import *
2+
3+
URL_BASE = "https://www.python.org/ftp/python/"
4+
5+
XYZ_VERSION = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}"
6+
WIN32_VERSION = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}.{VER_FIELD4}"
7+
FULL_VERSION = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}{VER_SUFFIX}"
8+
9+
10+
def _not_empty(n, key=None):
11+
result = []
12+
for i in n:
13+
if key:
14+
i_l = i[key]
15+
else:
16+
i_l = i
17+
if not i_l:
18+
continue
19+
result.append(i)
20+
return result
21+
22+
23+
def calculate_install_json(ns, *, for_embed=False, for_test=False):
24+
TARGET = "python.exe"
25+
TARGETW = "pythonw.exe"
26+
27+
SYS_ARCH = {
28+
"win32": "32bit",
29+
"amd64": "64bit",
30+
"arm64": "64bit", # Unfortunate, but this is how it's spec'd
31+
}[ns.arch]
32+
TAG_ARCH = {
33+
"win32": "-32",
34+
"amd64": "-64",
35+
"arm64": "-arm64",
36+
}[ns.arch]
37+
38+
COMPANY = "PythonCore"
39+
DISPLAY_NAME = "Python"
40+
TAG_SUFFIX = ""
41+
ALIAS_PREFIX = "python"
42+
ALIAS_WPREFIX = "pythonw"
43+
FILE_PREFIX = "python-"
44+
FILE_SUFFIX = f"-{ns.arch}"
45+
DISPLAY_TAGS = [{
46+
"win32": "32-bit",
47+
"amd64": "",
48+
"arm64": "ARM64",
49+
}[ns.arch]]
50+
51+
if for_test:
52+
# Packages with the test suite come under a different Company
53+
COMPANY = "PythonTest"
54+
DISPLAY_TAGS.append("with tests")
55+
FILE_SUFFIX = f"-test-{ns.arch}"
56+
if for_embed:
57+
# Embeddable distro comes under a different Company
58+
COMPANY = "PythonEmbed"
59+
TARGETW = None
60+
ALIAS_PREFIX = None
61+
ALIAS_WPREFIX = None
62+
DISPLAY_TAGS.append("embeddable")
63+
# Deliberately name the file differently from the existing distro
64+
# so we can republish old versions without replacing files.
65+
FILE_SUFFIX = f"-embeddable-{ns.arch}"
66+
if ns.include_freethreaded:
67+
# Free-threaded distro comes with a tag suffix
68+
TAG_SUFFIX = "t"
69+
TARGET = f"python{VER_MAJOR}.{VER_MINOR}t.exe"
70+
TARGETW = f"pythonw{VER_MAJOR}.{VER_MINOR}t.exe"
71+
DISPLAY_TAGS.append("free-threaded")
72+
FILE_SUFFIX = f"t-{ns.arch}"
73+
74+
FULL_TAG = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}{VER_SUFFIX}{TAG_SUFFIX}"
75+
FULL_ARCH_TAG = f"{FULL_TAG}{TAG_ARCH}"
76+
XY_TAG = f"{VER_MAJOR}.{VER_MINOR}{TAG_SUFFIX}"
77+
XY_ARCH_TAG = f"{XY_TAG}{TAG_ARCH}"
78+
X_TAG = f"{VER_MAJOR}{TAG_SUFFIX}"
79+
X_ARCH_TAG = f"{X_TAG}{TAG_ARCH}"
80+
81+
# Tag used in runtime ID (for side-by-side install/updates)
82+
ID_TAG = XY_ARCH_TAG
83+
# Tag shown in 'py list' output
84+
DISPLAY_TAG = f"{XY_TAG}-dev{TAG_ARCH}" if VER_SUFFIX else XY_ARCH_TAG
85+
# Tag used for PEP 514 registration
86+
SYS_WINVER = XY_TAG + (TAG_ARCH if TAG_ARCH != '-64' else '')
87+
88+
DISPLAY_SUFFIX = ", ".join(i for i in DISPLAY_TAGS if i)
89+
if DISPLAY_SUFFIX:
90+
DISPLAY_SUFFIX = f" ({DISPLAY_SUFFIX})"
91+
DISPLAY_VERSION = f"{XYZ_VERSION}{VER_SUFFIX}{DISPLAY_SUFFIX}"
92+
93+
STD_RUN_FOR = []
94+
STD_ALIAS = []
95+
STD_PEP514 = []
96+
STD_START = []
97+
STD_UNINSTALL = []
98+
99+
# The list of 'py install <TAG>' tags that will match this runtime.
100+
# Architecture should always be included here because PyManager will add it.
101+
INSTALL_TAGS = [
102+
FULL_ARCH_TAG,
103+
XY_ARCH_TAG,
104+
X_ARCH_TAG,
105+
# X_TAG and XY_TAG doesn't include VER_SUFFIX, so create -dev versions
106+
f"{XY_TAG}-dev{TAG_ARCH}" if XY_TAG and VER_SUFFIX else "",
107+
f"{X_TAG}-dev{TAG_ARCH}" if X_TAG and VER_SUFFIX else "",
108+
]
109+
110+
# Generate run-for entries for each target.
111+
# Again, include architecture because PyManager will add it.
112+
for base in [
113+
{"target": TARGET},
114+
{"target": TARGETW, "windowed": 1},
115+
]:
116+
if not base["target"]:
117+
continue
118+
STD_RUN_FOR.append({**base, "tag": FULL_ARCH_TAG})
119+
if XY_TAG:
120+
STD_RUN_FOR.append({**base, "tag": XY_ARCH_TAG})
121+
if X_TAG:
122+
STD_RUN_FOR.append({**base, "tag": X_ARCH_TAG})
123+
if VER_SUFFIX:
124+
STD_RUN_FOR.extend([
125+
{**base, "tag": f"{XY_TAG}-dev{TAG_ARCH}" if XY_TAG else ""},
126+
{**base, "tag": f"{X_TAG}-dev{TAG_ARCH}" if X_TAG else ""},
127+
])
128+
129+
# Generate alias entries for each target. We need both arch and non-arch
130+
# versions as well as windowed/non-windowed versions to make sure that all
131+
# necessary aliases are created.
132+
for prefix, base in (
133+
(ALIAS_PREFIX, {"target": TARGET}),
134+
(ALIAS_WPREFIX, {"target": TARGETW, "windowed": 1}),
135+
):
136+
if not prefix:
137+
continue
138+
if not base["target"]:
139+
continue
140+
if XY_TAG:
141+
STD_ALIAS.extend([
142+
{**base, "name": f"{prefix}{XY_TAG}.exe"},
143+
{**base, "name": f"{prefix}{XY_ARCH_TAG}.exe"},
144+
])
145+
if X_TAG:
146+
STD_ALIAS.extend([
147+
{**base, "name": f"{prefix}{X_TAG}.exe"},
148+
{**base, "name": f"{prefix}{X_ARCH_TAG}.exe"},
149+
])
150+
151+
if SYS_WINVER:
152+
STD_PEP514.append({
153+
"kind": "pep514",
154+
"Key": rf"{COMPANY}\{SYS_WINVER}",
155+
"DisplayName": f"{DISPLAY_NAME} {DISPLAY_VERSION}",
156+
"SupportUrl": "https://www.python.org/",
157+
"SysArchitecture": SYS_ARCH,
158+
"SysVersion": VER_DOT,
159+
"Version": FULL_VERSION,
160+
"InstallPath": {
161+
"_": "%PREFIX%",
162+
"ExecutablePath": f"%PREFIX%{TARGET}",
163+
# WindowedExecutablePath is added below
164+
},
165+
"Help": {
166+
"Online Python Documentation": {
167+
"_": f"https://docs.python.org/{VER_DOT}/"
168+
},
169+
},
170+
})
171+
172+
STD_START.append({
173+
"kind": "start",
174+
"Name": f"{DISPLAY_NAME} {VER_DOT}{DISPLAY_SUFFIX}",
175+
"Items": [
176+
{
177+
"Name": f"{DISPLAY_NAME} {VER_DOT}{DISPLAY_SUFFIX}",
178+
"Target": f"%PREFIX%{TARGET}",
179+
"Icon": f"%PREFIX%{TARGET}",
180+
},
181+
{
182+
"Name": f"{DISPLAY_NAME} {VER_DOT} Online Documentation",
183+
"Icon": r"%SystemRoot%\System32\SHELL32.dll",
184+
"IconIndex": 13,
185+
"Target": f"https://docs.python.org/{VER_DOT}/",
186+
},
187+
# IDLE and local documentation items are added below
188+
],
189+
})
190+
191+
if TARGETW and STD_PEP514:
192+
STD_PEP514[0]["InstallPath"]["WindowedExecutablePath"] = f"%PREFIX%{TARGETW}"
193+
194+
if ns.include_idle:
195+
STD_START[0]["Items"].append({
196+
"Name": f"IDLE (Python {VER_DOT}{DISPLAY_SUFFIX})",
197+
"Target": f"%PREFIX%{TARGETW or TARGET}",
198+
"Arguments": r'"%PREFIX%Lib\idlelib\idle.pyw"',
199+
"Icon": r"%PREFIX%Lib\idlelib\Icons\idle.ico",
200+
"IconIndex": 0,
201+
})
202+
STD_START[0]["Items"].append({
203+
"Name": f"PyDoc (Python {VER_DOT}{DISPLAY_SUFFIX})",
204+
"Target": f"%PREFIX%{TARGET}",
205+
"Arguments": "-m pydoc -b",
206+
"Icon": r"%PREFIX%Lib\idlelib\Icons\idle.ico",
207+
"IconIndex": 0,
208+
})
209+
if STD_PEP514:
210+
STD_PEP514[0]["InstallPath"]["IdlePath"] = f"%PREFIX%Lib\\idlelib\\idle.pyw"
211+
212+
if ns.include_html_doc:
213+
STD_PEP514[0]["Help"]["Main Python Documentation"] = {
214+
"_": rf"%PREFIX%Doc\html\index.html",
215+
}
216+
STD_START[0]["Items"].append({
217+
"Name": f"{DISPLAY_NAME} {VER_DOT} Manuals{DISPLAY_SUFFIX}",
218+
"Target": r"%PREFIX%Doc\html\index.html",
219+
})
220+
elif ns.include_chm:
221+
STD_PEP514[0]["Help"]["Main Python Documentation"] = {
222+
"_": rf"%PREFIX%Doc\{PYTHON_CHM_NAME}",
223+
}
224+
STD_START[0]["Items"].append({
225+
"Name": f"{DISPLAY_NAME} {VER_DOT} Manuals{DISPLAY_SUFFIX}",
226+
"Target": "%WINDIR%hhc.exe",
227+
"Arguments": rf"%PREFIX%Doc\{PYTHON_CHM_NAME}",
228+
})
229+
230+
STD_UNINSTALL.append({
231+
"kind": "uninstall",
232+
# Other settings will pick up sensible defaults
233+
"Publisher": "Python Software Foundation",
234+
"HelpLink": f"https://docs.python.org/{VER_DOT}/",
235+
})
236+
237+
data = {
238+
"schema": 1,
239+
"id": f"{COMPANY.lower()}-{ID_TAG}",
240+
"sort-version": FULL_VERSION,
241+
"company": COMPANY,
242+
"tag": DISPLAY_TAG,
243+
"install-for": _not_empty(INSTALL_TAGS),
244+
"run-for": _not_empty(STD_RUN_FOR, "tag"),
245+
"alias": _not_empty(STD_ALIAS, "name"),
246+
"shortcuts": [
247+
*STD_PEP514,
248+
*STD_START,
249+
*STD_UNINSTALL,
250+
],
251+
"display-name": f"{DISPLAY_NAME} {DISPLAY_VERSION}",
252+
"executable": rf".\{TARGET}",
253+
"url": f"{URL_BASE}{XYZ_VERSION}/{FILE_PREFIX}{FULL_VERSION}{FILE_SUFFIX}.zip"
254+
}
255+
256+
return data

0 commit comments

Comments
 (0)