ยปCore Development>Code coverage>Tools/scripts/win_add2path.py

Python code coverage for Tools/scripts/win_add2path.py

#countcontent
1n/a"""Add Python to the search path on Windows
2n/a
3n/aThis is a simple script to add Python to the Windows search path. It
4n/amodifies the current user (HKCU) tree of the registry.
5n/a
6n/aCopyright (c) 2008 by Christian Heimes <christian@cheimes.de>
7n/aLicensed to PSF under a Contributor Agreement.
8n/a"""
9n/a
10n/aimport sys
11n/aimport site
12n/aimport os
13n/aimport winreg
14n/a
15n/aHKCU = winreg.HKEY_CURRENT_USER
16n/aENV = "Environment"
17n/aPATH = "PATH"
18n/aDEFAULT = "%PATH%"
19n/a
20n/adef modify():
21n/a pythonpath = os.path.dirname(os.path.normpath(sys.executable))
22n/a scripts = os.path.join(pythonpath, "Scripts")
23n/a appdata = os.environ["APPDATA"]
24n/a if hasattr(site, "USER_SITE"):
25n/a usersite = site.USER_SITE.replace(appdata, "%APPDATA%")
26n/a userpath = os.path.dirname(usersite)
27n/a userscripts = os.path.join(userpath, "Scripts")
28n/a else:
29n/a userscripts = None
30n/a
31n/a with winreg.CreateKey(HKCU, ENV) as key:
32n/a try:
33n/a envpath = winreg.QueryValueEx(key, PATH)[0]
34n/a except OSError:
35n/a envpath = DEFAULT
36n/a
37n/a paths = [envpath]
38n/a for path in (pythonpath, scripts, userscripts):
39n/a if path and path not in envpath and os.path.isdir(path):
40n/a paths.append(path)
41n/a
42n/a envpath = os.pathsep.join(paths)
43n/a winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath)
44n/a return paths, envpath
45n/a
46n/adef main():
47n/a paths, envpath = modify()
48n/a if len(paths) > 1:
49n/a print("Path(s) added:")
50n/a print('\n'.join(paths[1:]))
51n/a else:
52n/a print("No path was added")
53n/a print("\nPATH is now:\n%s\n" % envpath)
54n/a print("Expanded:")
55n/a print(winreg.ExpandEnvironmentStrings(envpath))
56n/a
57n/aif __name__ == '__main__':
58n/a main()