123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import os, sys, glob
- from joblib import Parallel, delayed
- from multiprocessing import cpu_count
- from mako.template import Template
- from parser import parse
- def get_filepaths(directory):
- """
- This function will generate the file names in a directory
- tree by walking the tree either top-down or bottom-up. For each
- directory in the tree rooted at directory top (including top itself),
- it yields a 3-tuple (dirpath, dirnames, filenames).
- """
- file_paths = []
-
- for root, directories, files in os.walk(directory):
- for filename in files:
-
- filepath = os.path.join(root, filename)
- file_paths.append(filepath)
- return file_paths
- def get_name_from_path(path, basepath, prefix, postfix):
- f_clean = path[len(basepath):]
- f_clean = f_clean.replace(basepath, "")
- f_clean = f_clean.replace(postfix, "")
- f_clean = f_clean.replace(prefix, "")
- f_clean = f_clean.replace("/", "_")
- f_clean = f_clean.replace("\\", "_")
- f_clean = f_clean.replace(" ", "_")
- f_clean = f_clean.replace(".", "_")
- return f_clean
- if __name__ == '__main__':
- if len(sys.argv) != 3:
- print('Syntax: %s <path_to_c++_header_files> <path_to_python_files>' % sys.argv[0])
- exit(-1)
-
- cpp_base_path = sys.argv[1]
- py_base_path = sys.argv[2]
- cpp_file_paths = get_filepaths(cpp_base_path)
- py_file_paths = get_filepaths(py_base_path)
-
- mapping = {}
- for f in cpp_file_paths:
- if f.endswith(".h"):
- name = get_name_from_path(f, cpp_base_path, "", ".h")
- mapping[name] = f
-
- implemented_names = []
- for f in py_file_paths:
- if f.endswith(".cpp"):
- name = get_name_from_path(f, py_base_path, "py_", ".cpp")
- implemented_names.append(name)
-
- files_to_parse = []
- for n in implemented_names:
- if n not in mapping:
- print("No cpp header file for python function %s found." % n)
- continue
- files_to_parse.append(mapping[n])
-
-
- job_count = cpu_count()
- dicts = Parallel(n_jobs=job_count)(delayed(parse)(path) for path in files_to_parse)
- hpplines = []
- cpplines = []
- for idx, n in enumerate(implemented_names):
- d = dicts[idx]
- contained_elements = sum(map(lambda x: len(x), d.values()))
-
- if contained_elements == 0:
- print("Function %s contains no parseable content in cpp header. Something might be wrong." % n)
- continue
- else:
- names = []
- namespaces = "_".join(d["namespaces"])
- for f in d["functions"]:
- h_string = "extern const char *__doc_" + namespaces + "_" + f.name + ";\n"
- docu_string = "See " + f.name + " for the documentation."
- if f.documentation != "":
- docu_string = f.documentation
- cpp_string = "const char *__doc_" + namespaces + "_" + f.name + " = R\"igl_Qu8mg5v7(" + docu_string + ")igl_Qu8mg5v7\";\n"
- if f.name not in names:
- hpplines.append(h_string)
- cpplines.append(cpp_string)
- names.append(f.name)
-
- path = os.path.dirname(__file__)
- if path != "":
- os.chdir(path)
-
- with open('../py_doc.h', 'w') as fh:
- fh.writelines(hpplines)
- with open('../py_doc.cpp', 'w') as fc:
- fc.writelines(cpplines)
-
- tpl = Template(filename='python_shared.mako')
- rendered = tpl.render(functions=implemented_names)
- with open("../python_shared.cpp", 'w') as fs:
- fs.write(rendered)
|