circuitpython/py/genlast.py

91 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import sys
import re
import os
import itertools
from concurrent.futures import ProcessPoolExecutor
import multiprocessing
import threading
import subprocess
from makeqstrdefs import qstr_unescape, QSTRING_BLOCK_LIST
re_line = re.compile(r"#[line]*\s(\d+)\s\"([^\"]+)\"", re.DOTALL)
re_qstr = re.compile(r"\bMP_QSTR_[_a-zA-Z0-9]+", re.DOTALL)
2021-03-15 09:57:36 -04:00
re_translate = re.compile(r"translate\(\"((?:(?=(\\?))\2.)*?)\"\)", re.DOTALL)
def write_out(fname, output_dir, output):
if output:
for m, r in [("/", "__"), ("\\", "__"), (":", "@"), ("..", "@@")]:
fname = fname.replace(m, r)
with open(output_dir + "/" + fname + ".qstr", "w") as f:
f.write("\n".join(output) + "\n")
2021-03-15 09:57:36 -04:00
def process_file(fname, output_dir, content):
2021-03-15 09:57:36 -04:00
content = content.decode("utf-8", errors="ignore")
output = []
for match in re_qstr.findall(content):
2021-03-15 09:57:36 -04:00
name = match.replace("MP_QSTR_", "")
if name not in QSTRING_BLOCK_LIST:
2021-03-15 09:57:36 -04:00
output.append("Q(" + qstr_unescape(name) + ")")
for match in re_translate.findall(content):
output.append('TRANSLATE("' + match[0] + '")')
write_out(fname, output_dir, output)
def checkoutput1(args):
2021-03-15 09:57:36 -04:00
info = subprocess.run(args, check=True, stdout=subprocess.PIPE, input="")
return info.stdout
2021-03-15 09:57:36 -04:00
def preprocess(command, output_dir, fn):
try:
output = checkoutput1(command + [fn])
process_file(fn, output_dir, output)
except Exception as e:
print(e, file=sys.stderr)
raise
2021-03-15 09:57:36 -04:00
def maybe_preprocess(command, output_dir, fn):
2020-11-20 23:33:39 -05:00
# Preprocess the source file if it contains "MP_QSTR", "translate",
# or if it uses enum.h (which generates "MP_QSTR" strings.
if subprocess.call(["grep", "-lqE", r"(MP_QSTR|translate|enum\.h)", fn]) == 0:
preprocess(command, output_dir, fn)
2021-03-15 09:57:36 -04:00
if __name__ == "__main__":
2021-03-15 09:57:36 -04:00
idx1 = sys.argv.index("--")
idx2 = sys.argv.index("--", idx1 + 1)
output_dir = sys.argv[1]
check = sys.argv[2:idx1]
2021-03-15 09:57:36 -04:00
always = sys.argv[idx1 + 1 : idx2]
command = sys.argv[idx2 + 1 :]
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
# Mac and Windows use 'spawn'. Uncomment this during testing to catch spawn-specific problems on Linux.
2021-03-15 09:57:36 -04:00
# multiprocessing.set_start_method("spawn")
executor = ProcessPoolExecutor(max_workers=multiprocessing.cpu_count() + 1)
results = []
try:
results.extend(
executor.map(
maybe_preprocess, itertools.repeat(command), itertools.repeat(output_dir), check
)
)
results.extend(
executor.map(
preprocess, itertools.repeat(command), itertools.repeat(output_dir), always
)
)
except subprocess.CalledProcessError:
raise SystemExit(1)
executor.shutdown()