File: pymanager.py

package info (click to toggle)
python3.13 3.13.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,256 kB
  • sloc: python: 703,743; ansic: 653,888; xml: 31,250; sh: 5,844; cpp: 4,326; makefile: 1,981; objc: 787; lisp: 502; javascript: 213; asm: 75; csh: 12
file content (256 lines) | stat: -rw-r--r-- 8,787 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from .constants import *

URL_BASE = "https://www.python.org/ftp/python/"

XYZ_VERSION = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}"
WIN32_VERSION = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}.{VER_FIELD4}"
FULL_VERSION = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}{VER_SUFFIX}"


def _not_empty(n, key=None):
    result = []
    for i in n:
        if key:
            i_l = i[key]
        else:
            i_l = i
        if not i_l:
            continue
        result.append(i)
    return result


def calculate_install_json(ns, *, for_embed=False, for_test=False):
    TARGET = "python.exe"
    TARGETW = "pythonw.exe"

    SYS_ARCH = {
        "win32": "32bit",
        "amd64": "64bit",
        "arm64": "64bit", # Unfortunate, but this is how it's spec'd
    }[ns.arch]
    TAG_ARCH = {
        "win32": "-32",
        "amd64": "-64",
        "arm64": "-arm64",
    }[ns.arch]

    COMPANY = "PythonCore"
    DISPLAY_NAME = "Python"
    TAG_SUFFIX = ""
    ALIAS_PREFIX = "python"
    ALIAS_WPREFIX = "pythonw"
    FILE_PREFIX = "python-"
    FILE_SUFFIX = f"-{ns.arch}"
    DISPLAY_TAGS = [{
        "win32": "32-bit",
        "amd64": "",
        "arm64": "ARM64",
    }[ns.arch]]

    if for_test:
        # Packages with the test suite come under a different Company
        COMPANY = "PythonTest"
        DISPLAY_TAGS.append("with tests")
        FILE_SUFFIX = f"-test-{ns.arch}"
    if for_embed:
        # Embeddable distro comes under a different Company
        COMPANY = "PythonEmbed"
        TARGETW = None
        ALIAS_PREFIX = None
        ALIAS_WPREFIX = None
        DISPLAY_TAGS.append("embeddable")
        # Deliberately name the file differently from the existing distro
        # so we can republish old versions without replacing files.
        FILE_SUFFIX = f"-embeddable-{ns.arch}"
    if ns.include_freethreaded:
        # Free-threaded distro comes with a tag suffix
        TAG_SUFFIX = "t"
        TARGET = f"python{VER_MAJOR}.{VER_MINOR}t.exe"
        TARGETW = f"pythonw{VER_MAJOR}.{VER_MINOR}t.exe"
        DISPLAY_TAGS.append("free-threaded")
        FILE_SUFFIX = f"t-{ns.arch}"

    FULL_TAG = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}{VER_SUFFIX}{TAG_SUFFIX}"
    FULL_ARCH_TAG = f"{FULL_TAG}{TAG_ARCH}"
    XY_TAG = f"{VER_MAJOR}.{VER_MINOR}{TAG_SUFFIX}"
    XY_ARCH_TAG = f"{XY_TAG}{TAG_ARCH}"
    X_TAG = f"{VER_MAJOR}{TAG_SUFFIX}"
    X_ARCH_TAG = f"{X_TAG}{TAG_ARCH}"

    # Tag used in runtime ID (for side-by-side install/updates)
    ID_TAG = XY_ARCH_TAG
    # Tag shown in 'py list' output
    DISPLAY_TAG = f"{XY_TAG}-dev{TAG_ARCH}" if VER_SUFFIX else XY_ARCH_TAG
    # Tag used for PEP 514 registration
    SYS_WINVER = XY_TAG + (TAG_ARCH if TAG_ARCH != '-64' else '')

    DISPLAY_SUFFIX = ", ".join(i for i in DISPLAY_TAGS if i)
    if DISPLAY_SUFFIX:
        DISPLAY_SUFFIX = f" ({DISPLAY_SUFFIX})"
    DISPLAY_VERSION = f"{XYZ_VERSION}{VER_SUFFIX}{DISPLAY_SUFFIX}"

    STD_RUN_FOR = []
    STD_ALIAS = []
    STD_PEP514 = []
    STD_START = []
    STD_UNINSTALL = []

    # The list of 'py install <TAG>' tags that will match this runtime.
    # Architecture should always be included here because PyManager will add it.
    INSTALL_TAGS = [
        FULL_ARCH_TAG,
        XY_ARCH_TAG,
        X_ARCH_TAG,
        # X_TAG and XY_TAG doesn't include VER_SUFFIX, so create -dev versions
        f"{XY_TAG}-dev{TAG_ARCH}" if XY_TAG and VER_SUFFIX else "",
        f"{X_TAG}-dev{TAG_ARCH}" if X_TAG and VER_SUFFIX else "",
    ]

    # Generate run-for entries for each target.
    # Again, include architecture because PyManager will add it.
    for base in [
        {"target": TARGET},
        {"target": TARGETW, "windowed": 1},
    ]:
        if not base["target"]:
            continue
        STD_RUN_FOR.append({**base, "tag": FULL_ARCH_TAG})
        if XY_TAG:
            STD_RUN_FOR.append({**base, "tag": XY_ARCH_TAG})
        if X_TAG:
            STD_RUN_FOR.append({**base, "tag": X_ARCH_TAG})
        if VER_SUFFIX:
            STD_RUN_FOR.extend([
                {**base, "tag": f"{XY_TAG}-dev{TAG_ARCH}" if XY_TAG else ""},
                {**base, "tag": f"{X_TAG}-dev{TAG_ARCH}" if X_TAG else ""},
            ])

    # Generate alias entries for each target. We need both arch and non-arch
    # versions as well as windowed/non-windowed versions to make sure that all
    # necessary aliases are created.
    for prefix, base in (
        (ALIAS_PREFIX, {"target": TARGET}),
        (ALIAS_WPREFIX, {"target": TARGETW, "windowed": 1}),
    ):
        if not prefix:
            continue
        if not base["target"]:
            continue
        if XY_TAG:
            STD_ALIAS.extend([
                {**base, "name": f"{prefix}{XY_TAG}.exe"},
                {**base, "name": f"{prefix}{XY_ARCH_TAG}.exe"},
            ])
        if X_TAG:
            STD_ALIAS.extend([
                {**base, "name": f"{prefix}{X_TAG}.exe"},
                {**base, "name": f"{prefix}{X_ARCH_TAG}.exe"},
            ])

    if SYS_WINVER:
        STD_PEP514.append({
            "kind": "pep514",
            "Key": rf"{COMPANY}\{SYS_WINVER}",
            "DisplayName": f"{DISPLAY_NAME} {DISPLAY_VERSION}",
            "SupportUrl": "https://www.python.org/",
            "SysArchitecture": SYS_ARCH,
            "SysVersion": VER_DOT,
            "Version": FULL_VERSION,
            "InstallPath": {
                "_": "%PREFIX%",
                "ExecutablePath": f"%PREFIX%{TARGET}",
                # WindowedExecutablePath is added below
            },
            "Help": {
                "Online Python Documentation": {
                    "_": f"https://docs.python.org/{VER_DOT}/"
                },
            },
        })

    STD_START.append({
        "kind": "start",
        "Name": f"{DISPLAY_NAME} {VER_DOT}{DISPLAY_SUFFIX}",
        "Items": [
            {
                "Name": f"{DISPLAY_NAME} {VER_DOT}{DISPLAY_SUFFIX}",
                "Target": f"%PREFIX%{TARGET}",
                "Icon": f"%PREFIX%{TARGET}",
            },
            {
                "Name": f"{DISPLAY_NAME} {VER_DOT} Online Documentation",
                "Icon": r"%SystemRoot%\System32\SHELL32.dll",
                "IconIndex": 13,
                "Target": f"https://docs.python.org/{VER_DOT}/",
            },
            # IDLE and local documentation items are added below
        ],
    })

    if TARGETW and STD_PEP514:
        STD_PEP514[0]["InstallPath"]["WindowedExecutablePath"] = f"%PREFIX%{TARGETW}"

    if ns.include_idle:
        STD_START[0]["Items"].append({
            "Name": f"IDLE (Python {VER_DOT}{DISPLAY_SUFFIX})",
            "Target": f"%PREFIX%{TARGETW or TARGET}",
            "Arguments": r'"%PREFIX%Lib\idlelib\idle.pyw"',
            "Icon": r"%PREFIX%Lib\idlelib\Icons\idle.ico",
            "IconIndex": 0,
        })
        STD_START[0]["Items"].append({
            "Name": f"PyDoc (Python {VER_DOT}{DISPLAY_SUFFIX})",
            "Target": f"%PREFIX%{TARGET}",
            "Arguments": "-m pydoc -b",
            "Icon": r"%PREFIX%Lib\idlelib\Icons\idle.ico",
            "IconIndex": 0,
        })
        if STD_PEP514:
            STD_PEP514[0]["InstallPath"]["IdlePath"] = f"%PREFIX%Lib\\idlelib\\idle.pyw"

    if ns.include_html_doc:
        STD_PEP514[0]["Help"]["Main Python Documentation"] = {
            "_": rf"%PREFIX%Doc\html\index.html",
        }
        STD_START[0]["Items"].append({
            "Name": f"{DISPLAY_NAME} {VER_DOT} Manuals{DISPLAY_SUFFIX}",
            "Target": r"%PREFIX%Doc\html\index.html",
        })
    elif ns.include_chm:
        STD_PEP514[0]["Help"]["Main Python Documentation"] = {
            "_": rf"%PREFIX%Doc\{PYTHON_CHM_NAME}",
        }
        STD_START[0]["Items"].append({
            "Name": f"{DISPLAY_NAME} {VER_DOT} Manuals{DISPLAY_SUFFIX}",
            "Target": "%WINDIR%hhc.exe",
            "Arguments": rf"%PREFIX%Doc\{PYTHON_CHM_NAME}",
        })

    STD_UNINSTALL.append({
        "kind": "uninstall",
        # Other settings will pick up sensible defaults
        "Publisher": "Python Software Foundation",
        "HelpLink": f"https://docs.python.org/{VER_DOT}/",
    })

    data = {
        "schema": 1,
        "id": f"{COMPANY.lower()}-{ID_TAG}",
        "sort-version": FULL_VERSION,
        "company": COMPANY,
        "tag": DISPLAY_TAG,
        "install-for": _not_empty(INSTALL_TAGS),
        "run-for": _not_empty(STD_RUN_FOR, "tag"),
        "alias": _not_empty(STD_ALIAS, "name"),
        "shortcuts": [
            *STD_PEP514,
            *STD_START,
            *STD_UNINSTALL,
        ],
        "display-name": f"{DISPLAY_NAME} {DISPLAY_VERSION}",
        "executable": rf".\{TARGET}",
        "url": f"{URL_BASE}{XYZ_VERSION}/{FILE_PREFIX}{FULL_VERSION}{FILE_SUFFIX}.zip"
    }

    return data