-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathassociate.py
More file actions
430 lines (393 loc) · 12.2 KB
/
associate.py
File metadata and controls
430 lines (393 loc) · 12.2 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# -*- coding: utf-8 -*-
#
# Copyright © 2012 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see winpython/__init__.py for details)
"""
Register a Python distribution
Created on Tue Aug 21 21:46:30 2012
"""
import sys
import os
from pathlib import Path
import platform
import importlib
# import subprocess
# Local imports
import winreg
from winpython import utils
KEY_C = r"Software\Classes\%s"
KEY_C0 = KEY_C % r"Python.%sFile\shell"
KEY_C1 = KEY_C % r"Python.%sFile\shell\%s"
KEY_C2 = KEY_C1 + r"\command"
KEY_DROP0 = KEY_C % r"Python.%sFile\shellex"
KEY_DROP1 = KEY_C % r"Python.%sFile\shellex\DropHandler"
KEY_I = KEY_C % r"Python.%sFile\DefaultIcon"
KEY_D = KEY_C % r"Python.%sFile"
EWI = "Edit with IDLE"
EWS = "Edit with Spyder"
KEY_S = r"Software\Python"
KEY_S0 = KEY_S + r"\WinPython" # was PythonCore before PEP-0514
KEY_S1 = KEY_S0 + r"\%s"
def _remove_start_menu_folder(target, current=True):
"remove menu Folder for target WinPython"
import importlib.util
win32com_exists = importlib.util.find_spec('win32com') is not None
# we return nothing if no win32com package
if win32com_exists:
utils.remove_winpython_start_menu_folder(current=current)
def _get_shortcut_data(target, current=True):
"get windows menu access, if win32com exists otherwise nothing"
import importlib.util
win32com_exists = importlib.util.find_spec('win32com') is not None
# we return nothing if no win32com package
if not win32com_exists:
return []
wpgroup = utils.create_winpython_start_menu_folder(current=current)
wpdir = str(Path(target).parent)
data = []
for name in os.listdir(wpdir):
bname, ext = Path(name).stem, Path(name).suffix
if ext == ".exe":
data.append(
(
str(Path(wpdir) / name),
bname,
str(Path(wpgroup) / bname),
)
)
return data
def register(target, current=True, verbose=True):
"""Register a Python distribution in Windows registry"""
root = winreg.HKEY_CURRENT_USER if current else winreg.HKEY_LOCAL_MACHINE
# Creating Registry entries
if verbose:
print(f'Creating WinPython registry entries for {target}')
# Extensions
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C % ".py"),
"",
0,
winreg.REG_SZ,
"Python.File",
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C % ".pyw"),
"",
0,
winreg.REG_SZ,
"Python.NoConFile",
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C % ".pyc"),
"",
0,
winreg.REG_SZ,
"Python.CompiledFile",
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C % ".pyo"),
"",
0,
winreg.REG_SZ,
"Python.CompiledFile",
)
# MIME types
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C % ".py"),
"Content Type",
0,
winreg.REG_SZ,
"text/plain",
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C % ".pyw"),
"Content Type",
0,
winreg.REG_SZ,
"text/plain",
)
# Verbs
python = str((Path(target) / "python.exe").resolve())
pythonw = str((Path(target) / "pythonw.exe").resolve())
spyder = str((Path(target).parent / "Spyder.exe").resolve())
if not Path(spyder).is_file():
spyder = f'{pythonw}" "{target}\Scripts\spyder'
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C2 % ("", "open")),
"",
0,
winreg.REG_SZ,
'"%s" "%%1" %%*' % python,
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C2 % ("NoCon", "open")),
"",
0,
winreg.REG_SZ,
'"%s" "%%1" %%*' % pythonw,
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C2 % ("Compiled", "open")),
"",
0,
winreg.REG_SZ,
'"%s" "%%1" %%*' % python,
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C2 % ("", EWI)),
"",
0,
winreg.REG_SZ,
'"%s" "%s\Lib\idlelib\idle.pyw" -n -e "%%1"' % (pythonw, target),
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C2 % ("NoCon", EWI)),
"",
0,
winreg.REG_SZ,
'"%s" "%s\Lib\idlelib\idle.pyw" -n -e "%%1"' % (pythonw, target),
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C2 % ("", EWS)),
"",
0,
winreg.REG_SZ,
'"%s" "%%1"' % spyder,
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_C2 % ("NoCon", EWS)),
"",
0,
winreg.REG_SZ,
'"%s" "%%1"' % spyder,
)
# Drop support
handler = "{60254CA5-953B-11CF-8C96-00AA00B8708C}"
for ftype in ("", "NoCon", "Compiled"):
winreg.SetValueEx(
winreg.CreateKey(root, KEY_DROP1 % ftype),
"",
0,
winreg.REG_SZ,
handler,
)
# Icons
dlls = str(Path(target) / "DLLs")
winreg.SetValueEx(
winreg.CreateKey(root, KEY_I % ""),
"",
0,
winreg.REG_SZ,
r"%s\py.ico" % dlls,
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_I % "NoCon"),
"",
0,
winreg.REG_SZ,
r"%s\py.ico" % dlls,
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_I % "Compiled"),
"",
0,
winreg.REG_SZ,
r"%s\pyc.ico" % dlls,
)
# Descriptions
winreg.SetValueEx(
winreg.CreateKey(root, KEY_D % ""),
"",
0,
winreg.REG_SZ,
"Python File",
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_D % "NoCon"),
"",
0,
winreg.REG_SZ,
"Python File (no console)",
)
winreg.SetValueEx(
winreg.CreateKey(root, KEY_D % "Compiled"),
"",
0,
winreg.REG_SZ,
"Compiled Python File",
)
# PythonCore entries
python_infos = utils.get_python_infos(target) # ('3.11', 64)
short_version = python_infos[0] # 3.11 from ('3.11', 64)
long_version = utils.get_python_long_version(target) # 3.11.5
key_core = (KEY_S1 % short_version) + r"\%s" # Winpython\3.11
# PEP-0514 additions, with standard Python practice
SupportUrl="https://winpython.github.io"
SysArchitecture = platform.architecture()[0] # '64bit'
SysVersion = '.'.join(platform.python_version_tuple()[:2]) # '3.11'
Version = platform.python_version() # '3.11.5'
# But keep consistent with past possibilities until more alignement
SysArchitecture = f'{python_infos[1]}bit' # '64bit'
SysVersion = short_version
Version = long_version
DisplayName = f'Python {Version} ({SysArchitecture})'
key_short = (KEY_S1 % short_version) # WinPython\3.11
key_keys={'DisplayName':DisplayName,
'SupportUrl':SupportUrl,
'SysVersion':SysVersion,
'SysArchitecture':SysArchitecture,
'Version':Version}
regkey = winreg.CreateKey(root, key_short)
# see https://www.programcreek.com/python/example/106949/winreg.CreateKey
# winreg.SetValueEx(key, '', reg.REG_SZ, '')
for k, v in key_keys.items():
winreg.SetValueEx(
regkey,
k,
0,
winreg.REG_SZ,
v,
)
winreg.CloseKey(regkey)
# pep-0514 additions at InstallPathLevel
ExecutablePath = python
WindowedExecutablePath = pythonw
key_short = key_core % "InstallPath" # WinPython\3.11\InstallPath
key_keys={'ExecutablePath':ExecutablePath,
'WindowedExecutablePath':WindowedExecutablePath}
regkey = winreg.CreateKey(root, key_core % "InstallPath")
winreg.SetValueEx(
regkey,
"",
0,
winreg.REG_SZ,
target + '\\',
)
for k, v in key_keys.items():
winreg.SetValueEx(
regkey,
k,
0,
winreg.REG_SZ,
v,
)
winreg.CloseKey(regkey)
winreg.SetValueEx(
winreg.CreateKey(root, key_core % r"InstallPath\InstallGroup"),
"",
0,
winreg.REG_SZ,
"Python %s" % short_version,
)
winreg.SetValueEx(
winreg.CreateKey(root, key_core % "Modules"),
"",
0,
winreg.REG_SZ,
"",
)
winreg.SetValueEx(
winreg.CreateKey(root, key_core % "PythonPath"),
"",
0,
winreg.REG_SZ,
r"%s\Lib;%s\DLLs" % (target, target),
)
winreg.SetValueEx(
winreg.CreateKey(
root,
key_core % r"Help\Main Python Documentation",
),
"",
0,
winreg.REG_SZ,
r"%s\Doc\python%s.chm" % (target, long_version),
)
# Create start menu entries for all WinPython launchers
spec = importlib.util.find_spec('pythoncom')
if verbose and spec is None:
print(f"Can't create WinPython menu as pywin32 package is not installed")
if verbose and spec is not None:
print(f'Creating WinPython menu for all icons in {target}')
for path, desc, fname in _get_shortcut_data(target, current=current):
utils.create_shortcut(path, desc, fname, verbose=verbose)
def unregister(target, current=True, verbose=True):
"""Unregister a Python distribution in Windows registry"""
# Removing Registry entries
if verbose:
print(f'Removing WinPython registry entries for {target}')
root = winreg.HKEY_CURRENT_USER if current else winreg.HKEY_LOCAL_MACHINE
short_version = utils.get_python_infos(target)[0]
key_core = (KEY_S1 % short_version) + r"\%s"
for key in (
# Drop support
KEY_DROP1 % "",
KEY_DROP1 % "NoCon",
KEY_DROP1 % "Compiled",
KEY_DROP0 % "",
KEY_DROP0 % "NoCon",
KEY_DROP0 % "Compiled",
# Icons
KEY_I % "NoCon",
KEY_I % "Compiled",
KEY_I % "",
# Edit with IDLE
KEY_C2 % ("", EWI),
KEY_C2 % ("NoCon", EWI),
KEY_C1 % ("", EWI),
KEY_C1 % ("NoCon", EWI),
# Edit with Spyder
KEY_C2 % ("", EWS),
KEY_C2 % ("NoCon", EWS),
KEY_C1 % ("", EWS),
KEY_C1 % ("NoCon", EWS),
# Verbs
KEY_C2 % ("", "open"),
KEY_C2 % ("NoCon", "open"),
KEY_C2 % ("Compiled", "open"),
KEY_C1 % ("", "open"),
KEY_C1 % ("NoCon", "open"),
KEY_C1 % ("Compiled", "open"),
KEY_C0 % "",
KEY_C0 % "NoCon",
KEY_C0 % "Compiled",
# Descriptions
KEY_D % "NoCon",
KEY_D % "Compiled",
KEY_D % "",
# PythonCore
key_core % r"InstallPath\InstallGroup",
key_core % "InstallPath",
key_core % "Modules",
key_core % "PythonPath",
key_core % r"Help\Main Python Documentation",
key_core % "Help",
KEY_S1 % short_version,
KEY_S0,
KEY_S,
):
try:
if verbose:
print(key)
winreg.DeleteKey(root, key)
except WindowsError:
rootkey = "HKEY_CURRENT_USER" if current else "HKEY_LOCAL_MACHINE"
if verbose:
print(
r"Unable to remove %s\%s" % (rootkey, key),
file=sys.stderr,
)
# remove menu shortcuts
spec = importlib.util.find_spec('pythoncom')
if verbose and spec is None:
print(f"Can't remove WinPython menu as pywin32 package is not installed")
if verbose and spec is not None:
print(f'Removing WinPython menu for all icons in {target}')
_remove_start_menu_folder(target, current=current)
#for path, desc, fname in _get_shortcut_data(target, current=current):
# if Path(fname).exists():
# os.remove(fname)
if __name__ == "__main__":
register(sys.prefix)
unregister(sys.prefix)