gh-97669: Remove outdated example scripts (#97675) (#98167)

Remove outdated example scripts of the Tools/scripts/ directory:

* gprof2html.py
* md5sum.py
* nm2def.py
* pathfix.py
* win_add2path.py

Remove test_gprof2html, test_md5sum and test_pathfix of test_tools.
This commit is contained in:
Victor Stinner 2022-10-11 10:07:57 +02:00 committed by GitHub
parent b399115ef1
commit e0ae9ddffe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 2 additions and 831 deletions

View file

@ -1,35 +0,0 @@
"""Tests for the gprof2html script in the Tools directory."""
import os
import sys
import unittest
from unittest import mock
import tempfile
from test.test_tools import skip_if_missing, import_tool
skip_if_missing()
class Gprof2htmlTests(unittest.TestCase):
def setUp(self):
self.gprof = import_tool('gprof2html')
oldargv = sys.argv
def fixup():
sys.argv = oldargv
self.addCleanup(fixup)
sys.argv = []
def test_gprof(self):
# Issue #14508: this used to fail with a NameError.
with mock.patch.object(self.gprof, 'webbrowser') as wmock, \
tempfile.TemporaryDirectory() as tmpdir:
fn = os.path.join(tmpdir, 'abc')
open(fn, 'wb').close()
sys.argv = ['gprof2html', fn]
self.gprof.main()
self.assertTrue(wmock.open.called)
if __name__ == '__main__':
unittest.main()

View file

@ -1,78 +0,0 @@
"""Tests for the md5sum script in the Tools directory."""
import os
import unittest
from test.support import os_helper
from test.support import hashlib_helper
from test.support.script_helper import assert_python_ok, assert_python_failure
from test.test_tools import scriptsdir, skip_if_missing
skip_if_missing()
@hashlib_helper.requires_hashdigest('md5', openssl=True)
class MD5SumTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.script = os.path.join(scriptsdir, 'md5sum.py')
os.mkdir(os_helper.TESTFN_ASCII)
cls.fodder = os.path.join(os_helper.TESTFN_ASCII, 'md5sum.fodder')
with open(cls.fodder, 'wb') as f:
f.write(b'md5sum\r\ntest file\r\n')
cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d'
cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5'
@classmethod
def tearDownClass(cls):
os_helper.rmtree(os_helper.TESTFN_ASCII)
def test_noargs(self):
rc, out, err = assert_python_ok(self.script)
self.assertEqual(rc, 0)
self.assertTrue(
out.startswith(b'd41d8cd98f00b204e9800998ecf8427e <stdin>'))
self.assertFalse(err)
def test_checksum_fodder(self):
rc, out, err = assert_python_ok(self.script, self.fodder)
self.assertEqual(rc, 0)
self.assertTrue(out.startswith(self.fodder_md5))
for part in self.fodder.split(os.path.sep):
self.assertIn(part.encode(), out)
self.assertFalse(err)
def test_dash_l(self):
rc, out, err = assert_python_ok(self.script, '-l', self.fodder)
self.assertEqual(rc, 0)
self.assertIn(self.fodder_md5, out)
parts = self.fodder.split(os.path.sep)
self.assertIn(parts[-1].encode(), out)
self.assertNotIn(parts[-2].encode(), out)
def test_dash_t(self):
rc, out, err = assert_python_ok(self.script, '-t', self.fodder)
self.assertEqual(rc, 0)
self.assertTrue(out.startswith(self.fodder_textmode_md5))
self.assertNotIn(self.fodder_md5, out)
def test_dash_s(self):
rc, out, err = assert_python_ok(self.script, '-s', '512', self.fodder)
self.assertEqual(rc, 0)
self.assertIn(self.fodder_md5, out)
def test_multiple_files(self):
rc, out, err = assert_python_ok(self.script, self.fodder, self.fodder)
self.assertEqual(rc, 0)
lines = out.splitlines()
self.assertEqual(len(lines), 2)
self.assertEqual(*lines)
def test_usage(self):
rc, out, err = assert_python_failure(self.script, '-h')
self.assertEqual(rc, 2)
self.assertEqual(out, b'')
self.assertGreater(err, b'')
if __name__ == '__main__':
unittest.main()

View file

@ -1,131 +0,0 @@
import os
import subprocess
import sys
import unittest
from test.support import os_helper
from test.test_tools import scriptsdir, skip_if_missing
# need Tools/script/ directory: skip if run on Python installed on the system
skip_if_missing()
class TestPathfixFunctional(unittest.TestCase):
script = os.path.join(scriptsdir, 'pathfix.py')
def setUp(self):
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
def pathfix(self, shebang, pathfix_flags, exitcode=0, stdout='', stderr='',
directory=''):
if directory:
# bpo-38347: Test filename should contain lowercase, uppercase,
# "-", "_" and digits.
filename = os.path.join(directory, 'script-A_1.py')
pathfix_arg = directory
else:
filename = os_helper.TESTFN
pathfix_arg = filename
with open(filename, 'w', encoding='utf8') as f:
f.write(f'{shebang}\n' + 'print("Hello world")\n')
encoding = sys.getfilesystemencoding()
proc = subprocess.run(
[sys.executable, self.script,
*pathfix_flags, '-n', pathfix_arg],
env={**os.environ, 'PYTHONIOENCODING': encoding},
capture_output=True)
if stdout == '' and proc.returncode == 0:
stdout = f'{filename}: updating\n'
self.assertEqual(proc.returncode, exitcode, proc)
self.assertEqual(proc.stdout.decode(encoding), stdout.replace('\n', os.linesep), proc)
self.assertEqual(proc.stderr.decode(encoding), stderr.replace('\n', os.linesep), proc)
with open(filename, 'r', encoding='utf8') as f:
output = f.read()
lines = output.split('\n')
self.assertEqual(lines[1:], ['print("Hello world")', ''])
new_shebang = lines[0]
if proc.returncode != 0:
self.assertEqual(shebang, new_shebang)
return new_shebang
def test_recursive(self):
tmpdir = os_helper.TESTFN + '.d'
self.addCleanup(os_helper.rmtree, tmpdir)
os.mkdir(tmpdir)
expected_stderr = f"recursedown('{os.path.basename(tmpdir)}')\n"
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3'],
directory=tmpdir,
stderr=expected_stderr),
'#! /usr/bin/python3')
def test_pathfix(self):
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3']),
'#! /usr/bin/python3')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python3']),
'#! /usr/bin/python3')
def test_pathfix_keeping_flags(self):
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -R',
['-i', '/usr/bin/python3', '-k']),
'#! /usr/bin/python3 -R')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3', '-k']),
'#! /usr/bin/python3')
def test_pathfix_adding_flag(self):
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3', '-a', 's']),
'#! /usr/bin/python3 -s')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -S',
['-i', '/usr/bin/python3', '-a', 's']),
'#! /usr/bin/python3 -s')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -V',
['-i', '/usr/bin/python3', '-a', 'v', '-k']),
'#! /usr/bin/python3 -vV')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python',
['-i', '/usr/bin/python3', '-a', 'Rs']),
'#! /usr/bin/python3 -Rs')
self.assertEqual(
self.pathfix(
'#! /usr/bin/env python -W default',
['-i', '/usr/bin/python3', '-a', 's', '-k']),
'#! /usr/bin/python3 -sW default')
def test_pathfix_adding_errors(self):
self.pathfix(
'#! /usr/bin/env python -E',
['-i', '/usr/bin/python3', '-a', 'W default', '-k'],
exitcode=2,
stderr="-a option doesn't support whitespaces")
if __name__ == '__main__':
unittest.main()

View file

@ -19,15 +19,13 @@ class TestSundryScripts(unittest.TestCase):
# added for a script it should be added to the allowlist below.
# scripts that have independent tests.
allowlist = ['reindent', 'pdeps', 'gprof2html', 'md5sum']
allowlist = ['reindent']
# scripts that can't be imported without running
denylist = ['make_ctype']
# scripts that use windows-only modules
windows_only = ['win_add2path']
# denylisted for other reasons
other = ['2to3']
skiplist = denylist + allowlist + windows_only + other
skiplist = denylist + allowlist + other
def test_sundry(self):
old_modules = import_helper.modules_setup()
@ -45,11 +43,6 @@ def test_sundry(self):
# Unload all modules loaded in this test
import_helper.modules_cleanup(*old_modules)
@unittest.skipIf(sys.platform != "win32", "Windows-only test")
def test_sundry_windows(self):
for name in self.windows_only:
import_tool(name)
if __name__ == '__main__':
unittest.main()

View file

@ -1334,9 +1334,7 @@
<Compile Include="test\test_tkinter\test_widgets.py" />
<Compile Include="test\test_tkinter\widget_tests.py" />
<Compile Include="test\test_tokenize.py" />
<Compile Include="test\test_tools\test_gprof2html.py" />
<Compile Include="test\test_tools\test_i18n.py" />
<Compile Include="test\test_tools\test_md5sum.py" />
<Compile Include="test\test_tools\test_reindent.py" />
<Compile Include="test\test_tools\test_sundry.py" />
<Compile Include="test\test_tools\test_unparse.py" />

View file

@ -4,20 +4,11 @@ useful while building, extending or managing Python.
2to3 Main script for running the 2to3 conversion tool
abitype.py Converts a C file to use the PEP 384 type definition API
combinerefs.py A helper for analyzing PYTHONDUMPREFS output
diff.py Print file diffs in context, unified, or ndiff formats
gprof2html.py Transform gprof(1) output into useful HTML
idle3 Main program to start IDLE
md5sum.py Print MD5 checksums of argument files
ndiff.py Intelligent diff between text files (Tim Peters)
nm2def.py Create a template for PC/python_nt.def (Marc Lemburg)
parseentities.py Utility for parsing HTML entity definitions
parse_html5_entities.py Utility for parsing HTML5 entity definitions
patchcheck.py Perform common checks and cleanup before committing
pathfix.py Change #!/usr/local/bin/python into something else
ptags.py Create vi tags file for Python modules
pydoc3 Python documentation browser
reindent.py Change .py files to use 4-space indents
run_tests.py Run the test suite with more sensible default options
stable_abi.py Stable ABI checks and file generators.
untabify.py Replace tabs with spaces in argument files
win_add2path.py Add Python to the search path on Windows

View file

@ -1,87 +0,0 @@
#! /usr/bin/env python3
"""Transform gprof(1) output into useful HTML."""
import html
import os
import re
import sys
import webbrowser
header = """\
<html>
<head>
<title>gprof output (%s)</title>
</head>
<body>
<pre>
"""
trailer = """\
</pre>
</body>
</html>
"""
def add_escapes(filename):
with open(filename, encoding="utf-8") as fp:
for line in fp:
yield html.escape(line)
def gprof2html(input, output, filename):
output.write(header % filename)
for line in input:
output.write(line)
if line.startswith(" time"):
break
labels = {}
for line in input:
m = re.match(r"(.* )(\w+)\n", line)
if not m:
output.write(line)
break
stuff, fname = m.group(1, 2)
labels[fname] = fname
output.write('%s<a name="flat:%s" href="#call:%s">%s</a>\n' %
(stuff, fname, fname, fname))
for line in input:
output.write(line)
if line.startswith("index % time"):
break
for line in input:
m = re.match(r"(.* )(\w+)(( &lt;cycle.*&gt;)? \[\d+\])\n", line)
if not m:
output.write(line)
if line.startswith("Index by function name"):
break
continue
prefix, fname, suffix = m.group(1, 2, 3)
if fname not in labels:
output.write(line)
continue
if line.startswith("["):
output.write('%s<a name="call:%s" href="#flat:%s">%s</a>%s\n' %
(prefix, fname, fname, fname, suffix))
else:
output.write('%s<a href="#call:%s">%s</a>%s\n' %
(prefix, fname, fname, suffix))
for line in input:
for part in re.findall(r"(\w+(?:\.c)?|\W+)", line):
if part in labels:
part = '<a href="#call:%s">%s</a>' % (part, part)
output.write(part)
output.write(trailer)
def main():
filename = "gprof.out"
if sys.argv[1:]:
filename = sys.argv[1]
outputfilename = filename + ".html"
input = add_escapes(filename)
with open(outputfilename, "w", encoding="utf-8") as output:
gprof2html(input, output, filename)
webbrowser.open("file:" + os.path.abspath(outputfilename))
if __name__ == '__main__':
main()

View file

@ -1,93 +0,0 @@
#! /usr/bin/env python3
"""Python utility to print MD5 checksums of argument files.
"""
bufsize = 8096
fnfilter = None
rmode = 'rb'
usage = """
usage: md5sum.py [-b] [-t] [-l] [-s bufsize] [file ...]
-b : read files in binary mode (default)
-t : read files in text mode (you almost certainly don't want this!)
-l : print last pathname component only
-s bufsize: read buffer size (default %d)
file ... : files to sum; '-' or no files means stdin
""" % bufsize
import io
import sys
import os
import getopt
from hashlib import md5
def sum(*files):
sts = 0
if files and isinstance(files[-1], io.IOBase):
out, files = files[-1], files[:-1]
else:
out = sys.stdout
if len(files) == 1 and not isinstance(files[0], str):
files = files[0]
for f in files:
if isinstance(f, str):
if f == '-':
sts = printsumfp(sys.stdin, '<stdin>', out) or sts
else:
sts = printsum(f, out) or sts
else:
sts = sum(f, out) or sts
return sts
def printsum(filename, out=sys.stdout):
try:
fp = open(filename, rmode)
except IOError as msg:
sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg))
return 1
with fp:
if fnfilter:
filename = fnfilter(filename)
sts = printsumfp(fp, filename, out)
return sts
def printsumfp(fp, filename, out=sys.stdout):
m = md5()
try:
while 1:
data = fp.read(bufsize)
if not data:
break
if isinstance(data, str):
data = data.encode(fp.encoding)
m.update(data)
except IOError as msg:
sys.stderr.write('%s: I/O error: %s\n' % (filename, msg))
return 1
out.write('%s %s\n' % (m.hexdigest(), filename))
return 0
def main(args = sys.argv[1:], out=sys.stdout):
global fnfilter, rmode, bufsize
try:
opts, args = getopt.getopt(args, 'blts:')
except getopt.error as msg:
sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage))
return 2
for o, a in opts:
if o == '-l':
fnfilter = os.path.basename
elif o == '-b':
rmode = 'rb'
elif o == '-t':
rmode = 'r'
elif o == '-s':
bufsize = int(a)
if not args:
args = ['-']
return sum(args, out)
if __name__ == '__main__' or __name__ == sys.argv[0]:
sys.exit(main(sys.argv[1:], sys.stdout))

View file

@ -1,104 +0,0 @@
#! /usr/bin/env python3
"""nm2def.py
Helpers to extract symbols from Unix libs and auto-generate
Windows definition files from them. Depends on nm(1). Tested
on Linux and Solaris only (-p option to nm is for Solaris only).
By Marc-Andre Lemburg, Aug 1998.
Additional notes: the output of nm is supposed to look like this:
acceler.o:
000001fd T PyGrammar_AddAccelerators
U PyGrammar_FindDFA
00000237 T PyGrammar_RemoveAccelerators
U _IO_stderr_
U exit
U fprintf
U free
U malloc
U printf
grammar1.o:
00000000 T PyGrammar_FindDFA
00000034 T PyGrammar_LabelRepr
U _PyParser_TokenNames
U abort
U printf
U sprintf
...
Even if this isn't the default output of your nm, there is generally an
option to produce this format (since it is the original v7 Unix format).
"""
import os, sys
PYTHONLIB = 'libpython%d.%d.a' % sys.version_info[:2]
PC_PYTHONLIB = 'Python%d%d.dll' % sys.version_info[:2]
NM = 'nm -p -g %s' # For Linux, use "nm -g %s"
def symbols(lib=PYTHONLIB,types=('T','C','D')):
with os.popen(NM % lib) as pipe:
lines = pipe.readlines()
lines = [s.strip() for s in lines]
symbols = {}
for line in lines:
if len(line) == 0 or ':' in line:
continue
items = line.split()
if len(items) != 3:
continue
address, type, name = items
if type not in types:
continue
symbols[name] = address,type
return symbols
def export_list(symbols):
data = []
code = []
for name,(addr,type) in symbols.items():
if type in ('C','D'):
data.append('\t'+name)
else:
code.append('\t'+name)
data.sort()
data.append('')
code.sort()
return ' DATA\n'.join(data)+'\n'+'\n'.join(code)
# Definition file template
DEF_TEMPLATE = """\
EXPORTS
%s
"""
# Special symbols that have to be included even though they don't
# pass the filter
SPECIALS = (
)
def filter_Python(symbols,specials=SPECIALS):
for name in list(symbols.keys()):
if name[:2] == 'Py' or name[:3] == '_Py':
pass
elif name not in specials:
del symbols[name]
def main():
s = symbols(PYTHONLIB)
filter_Python(s)
exports = export_list(s)
f = sys.stdout # open('PC/python_nt.def','w')
f.write(DEF_TEMPLATE % (exports))
# f.close()
if __name__ == '__main__':
main()

View file

@ -1,225 +0,0 @@
#!/usr/bin/env python3
# Change the #! line (shebang) occurring in Python scripts. The new interpreter
# pathname must be given with a -i option.
#
# Command line arguments are files or directories to be processed.
# Directories are searched recursively for files whose name looks
# like a python module.
# Symbolic links are always ignored (except as explicit directory
# arguments).
# The original file is kept as a back-up (with a "~" attached to its name),
# -n flag can be used to disable this.
# Sometimes you may find shebangs with flags such as `#! /usr/bin/env python -si`.
# Normally, pathfix overwrites the entire line, including the flags.
# To change interpreter and keep flags from the original shebang line, use -k.
# If you want to keep flags and add to them one single literal flag, use option -a.
# Undoubtedly you can do this using find and sed or perl, but this is
# a nice example of Python code that recurses down a directory tree
# and uses regular expressions. Also note several subtleties like
# preserving the file's mode and avoiding to even write a temp file
# when no changes are needed for a file.
#
# NB: by changing only the function fixfile() you can turn this
# into a program for a different change to Python programs...
import sys
import os
from stat import *
import getopt
err = sys.stderr.write
dbg = err
rep = sys.stdout.write
new_interpreter = None
preserve_timestamps = False
create_backup = True
keep_flags = False
add_flags = b''
def main():
global new_interpreter
global preserve_timestamps
global create_backup
global keep_flags
global add_flags
usage = ('usage: %s -i /interpreter -p -n -k -a file-or-directory ...\n' %
sys.argv[0])
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:a:kpn')
except getopt.error as msg:
err(str(msg) + '\n')
err(usage)
sys.exit(2)
for o, a in opts:
if o == '-i':
new_interpreter = a.encode()
if o == '-p':
preserve_timestamps = True
if o == '-n':
create_backup = False
if o == '-k':
keep_flags = True
if o == '-a':
add_flags = a.encode()
if b' ' in add_flags:
err("-a option doesn't support whitespaces")
sys.exit(2)
if not new_interpreter or not new_interpreter.startswith(b'/') or \
not args:
err('-i option or file-or-directory missing\n')
err(usage)
sys.exit(2)
bad = 0
for arg in args:
if os.path.isdir(arg):
if recursedown(arg): bad = 1
elif os.path.islink(arg):
err(arg + ': will not process symbolic links\n')
bad = 1
else:
if fix(arg): bad = 1
sys.exit(bad)
def ispython(name):
return name.endswith('.py')
def recursedown(dirname):
dbg('recursedown(%r)\n' % (dirname,))
bad = 0
try:
names = os.listdir(dirname)
except OSError as msg:
err('%s: cannot list directory: %r\n' % (dirname, msg))
return 1
names.sort()
subdirs = []
for name in names:
if name in (os.curdir, os.pardir): continue
fullname = os.path.join(dirname, name)
if os.path.islink(fullname): pass
elif os.path.isdir(fullname):
subdirs.append(fullname)
elif ispython(name):
if fix(fullname): bad = 1
for fullname in subdirs:
if recursedown(fullname): bad = 1
return bad
def fix(filename):
## dbg('fix(%r)\n' % (filename,))
try:
f = open(filename, 'rb')
except IOError as msg:
err('%s: cannot open: %r\n' % (filename, msg))
return 1
with f:
line = f.readline()
fixed = fixline(line)
if line == fixed:
rep(filename+': no change\n')
return
head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail)
try:
g = open(tempname, 'wb')
except IOError as msg:
err('%s: cannot create: %r\n' % (tempname, msg))
return 1
with g:
rep(filename + ': updating\n')
g.write(fixed)
BUFSIZE = 8*1024
while 1:
buf = f.read(BUFSIZE)
if not buf: break
g.write(buf)
# Finishing touch -- move files
mtime = None
atime = None
# First copy the file's mode to the temp file
try:
statbuf = os.stat(filename)
mtime = statbuf.st_mtime
atime = statbuf.st_atime
os.chmod(tempname, statbuf[ST_MODE] & 0o7777)
except OSError as msg:
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
# Then make a backup of the original file as filename~
if create_backup:
try:
os.rename(filename, filename + '~')
except OSError as msg:
err('%s: warning: backup failed (%r)\n' % (filename, msg))
else:
try:
os.remove(filename)
except OSError as msg:
err('%s: warning: removing failed (%r)\n' % (filename, msg))
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except OSError as msg:
err('%s: rename failed (%r)\n' % (filename, msg))
return 1
if preserve_timestamps:
if atime and mtime:
try:
os.utime(filename, (atime, mtime))
except OSError as msg:
err('%s: reset of timestamp failed (%r)\n' % (filename, msg))
return 1
# Return success
return 0
def parse_shebang(shebangline):
shebangline = shebangline.rstrip(b'\n')
start = shebangline.find(b' -')
if start == -1:
return b''
return shebangline[start:]
def populate_flags(shebangline):
old_flags = b''
if keep_flags:
old_flags = parse_shebang(shebangline)
if old_flags:
old_flags = old_flags[2:]
if not (old_flags or add_flags):
return b''
# On Linux, the entire string following the interpreter name
# is passed as a single argument to the interpreter.
# e.g. "#! /usr/bin/python3 -W Error -s" runs "/usr/bin/python3 "-W Error -s"
# so shebang should have single '-' where flags are given and
# flag might need argument for that reasons adding new flags is
# between '-' and original flags
# e.g. #! /usr/bin/python3 -sW Error
return b' -' + add_flags + old_flags
def fixline(line):
if not line.startswith(b'#!'):
return line
if b"python" not in line:
return line
flags = populate_flags(line)
return b'#! ' + new_interpreter + flags + b'\n'
if __name__ == '__main__':
main()

View file

@ -1,58 +0,0 @@
"""Add Python to the search path on Windows
This is a simple script to add Python to the Windows search path. It
modifies the current user (HKCU) tree of the registry.
Copyright (c) 2008 by Christian Heimes <christian@cheimes.de>
Licensed to PSF under a Contributor Agreement.
"""
import sys
import site
import os
import winreg
HKCU = winreg.HKEY_CURRENT_USER
ENV = "Environment"
PATH = "PATH"
DEFAULT = "%PATH%"
def modify():
pythonpath = os.path.dirname(os.path.normpath(sys.executable))
scripts = os.path.join(pythonpath, "Scripts")
appdata = os.environ["APPDATA"]
if hasattr(site, "USER_SITE"):
usersite = site.USER_SITE.replace(appdata, "%APPDATA%")
userpath = os.path.dirname(usersite)
userscripts = os.path.join(userpath, "Scripts")
else:
userscripts = None
with winreg.CreateKey(HKCU, ENV) as key:
try:
envpath = winreg.QueryValueEx(key, PATH)[0]
except OSError:
envpath = DEFAULT
paths = [envpath]
for path in (pythonpath, scripts, userscripts):
if path and path not in envpath and os.path.isdir(path):
paths.append(path)
envpath = os.pathsep.join(paths)
winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath)
return paths, envpath
def main():
paths, envpath = modify()
if len(paths) > 1:
print("Path(s) added:")
print('\n'.join(paths[1:]))
else:
print("No path was added")
print("\nPATH is now:\n%s\n" % envpath)
print("Expanded:")
print(winreg.ExpandEnvironmentStrings(envpath))
if __name__ == '__main__':
main()