Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (80 sloc) 2.91 KB
import os
import inspect
from pprint import pprint
_thispath = os.path.dirname(inspect.getfile(inspect.currentframe()))
DESTPATH = os.path.join(_thispath, "../../driver/normalizer/pyast/pyast.go")
CONTENT = """
// Package pyast defines constants from Python 2 and 3 AST.
package pyast
// GENERATED BY python-driver/native/gogen/%s
// DO NOT EDIT
// Python 2+3 AST node types.
// This includes all classes extending from _ast.AST for both Python 2 and 3.
// See:
// https://docs.python.org/3.6/library/ast.html#abstract-grammar
// https://docs.python.org/2.7/library/ast.html#abstract-grammar
const (
{constants}
)
""" % os.path.basename(__file__)
# Shut-up placeholder for code checkers, the real function will be a exec() of the getsyms_code string
def get_ast_symbols(): return set()
# This is defined as an str so we can also pass it to the Python2 interpreter
# to get Python2's AST symbols
generate_str = """
def get_ast_symbols():
import _ast
astsyms = []
for symstr in dir(_ast):
if symstr == 'AST':
continue
sym = getattr(_ast, symstr)
if isinstance(sym, type) and issubclass(sym, _ast.AST):
astsyms.append(symstr)
return set(astsyms)
""".lstrip()
exec(generate_str)
generate3 = get_ast_symbols
WRITESYMS_CODE = """
py2_syms = get_ast_symbols()
for sym in py2_syms:
print(sym)
"""
def generate2():
"""
Call an external Python 2 program to retrieve the AST symbols of that
language version
:return:
"""
import subprocess as sp
import tempfile, shutil, sys, traceback
tempdir = tempfile.mkdtemp()
tempfile = os.path.join(tempdir, "py2_ast_code.py")
py2_proc_out = ""
try:
with open(tempfile, 'w') as py2code:
py2code.write(generate_str + WRITESYMS_CODE)
py2_proc_out = sp.check_output(["python2", tempfile]).decode()
finally:
try:
shutil.rmtree(tempdir)
except:
print("Warning: error trying to delete the temporal directory:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
return set(py2_proc_out.splitlines())
def generate23(outpath):
allsyms = list( generate2().union(generate3()) )
# add the types that we add to improve the raw AST information
allsyms.extend(["StringLiteral", "NoneLiteral", "NumLiteral", "ByteLiteral", "BoolLiteral",
"PreviousNoops", "NoopLine", "SameLineNoops", "RemainderNoops",
"noop_line"])
allsyms.sort()
maxlen = max((len(i) for i in allsyms))
decl_list = ['\t{symbol:<{maxlen}} = "{symbol}"'.format(symbol=i, maxlen=maxlen) for i in allsyms]
symlist_str = '\n'.join(decl_list)
final_content = CONTENT.format(constants=symlist_str).lstrip()
with open(outpath, 'w') as destfile:
print(final_content, file=destfile)
if __name__ == '__main__':
generate23(DESTPATH)
print("Go symbol file generated at: %s" % DESTPATH)
You can’t perform that action at this time.