add safety profiles + improve helptext + speed

This commit is contained in:
ed
2022-06-16 10:21:44 +02:00
parent 438384425a
commit eb3fa5aa6b
5 changed files with 225 additions and 26 deletions

View File

@@ -21,37 +21,52 @@ def uh(top):
if os.path.exists(top + "/uh"):
return
libs = "typing|types|collections\.abc"
ptn = re.compile(r"^(\s*)(from (?:{0}) import |import (?:{0})\b).*".format(libs))
# pr("building support for your python ver")
pr("unhinting")
files = []
for (dp, _, fns) in os.walk(top):
for fn in fns:
if not fn.endswith(".py"):
continue
pr(".")
fp = os.path.join(dp, fn)
cs = strip_file_to_string(fp, no_ast=True, to_empty=True)
files.append(fp)
# remove expensive imports too
lns = []
for ln in cs.split("\n"):
m = ptn.match(ln)
if m:
ln = m.group(1) + "raise Exception()"
try:
import multiprocessing as mp
lns.append(ln)
cs = "\n".join(lns)
with open(fp, "wb") as f:
f.write(cs.encode("utf-8"))
with mp.Pool(os.cpu_count()) as pool:
pool.map(uh1, files)
except Exception as ex:
print("\nnon-mp fallback due to {}\n".format(ex))
for fp in files:
uh1(fp)
pr("k\n\n")
with open(top + "/uh", "wb") as f:
f.write(b"a")
def uh1(fp):
pr(".")
cs = strip_file_to_string(fp, no_ast=True, to_empty=True)
libs = "typing|types|collections\.abc"
ptn = re.compile(r"^(\s*)(from (?:{0}) import |import (?:{0})\b).*".format(libs))
# remove expensive imports too
lns = []
for ln in cs.split("\n"):
m = ptn.match(ln)
if m:
ln = m.group(1) + "raise Exception()"
lns.append(ln)
cs = "\n".join(lns)
with open(fp, "wb") as f:
f.write(cs.encode("utf-8"))
if __name__ == "__main__":
uh(".")

View File

@@ -3,6 +3,7 @@
from __future__ import print_function, unicode_literals
import io
import os
import sys
import tokenize
@@ -10,6 +11,7 @@ import tokenize
def uncomment(fpath):
"""modified https://stackoverflow.com/a/62074206"""
print(".", end="", flush=True)
with open(fpath, "rb") as f:
orig = f.read().decode("utf-8")
@@ -66,9 +68,15 @@ def uncomment(fpath):
def main():
print("uncommenting", end="", flush=True)
for f in sys.argv[1:]:
print(".", end="", flush=True)
uncomment(f)
try:
import multiprocessing as mp
with mp.Pool(os.cpu_count()) as pool:
pool.map(uncomment, sys.argv[1:])
except Exception as ex:
print("\nnon-mp fallback due to {}\n".format(ex))
for f in sys.argv[1:]:
uncomment(f)
print("k")