mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-07-28 15:41:52 +03:00
generator.py: Allow source and build dirs override
Soon generator.py is going to be ran from a build directory which is different than the source directory. Allow specifying these directories on the cmd line. And while at it, introduce new "c+py" output mode in which both C and Python files are generated. While this is a fallback mode if no output mode is selected, we need this new mode so that aforementioned directories can be specified. Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
committed by
Daniel P. Berrangé
parent
533399d186
commit
865df8b846
38
generator.py
38
generator.py
@ -31,6 +31,8 @@ quiet = True
|
||||
#######################################################################
|
||||
debug = 0
|
||||
onlyOverrides = False
|
||||
sourceDir = "."
|
||||
buildDir = "build"
|
||||
|
||||
libvirt_headers = [
|
||||
"libvirt",
|
||||
@ -49,6 +51,11 @@ libvirt_headers = [
|
||||
"libvirt-stream",
|
||||
]
|
||||
|
||||
def openSourceFile(file: str, mode: str = "r", optional: bool = False):
|
||||
path = os.path.join(sourceDir, file)
|
||||
if optional and not os.path.exists(path):
|
||||
return None
|
||||
return open(path, mode)
|
||||
|
||||
def parse(data: IO[str]) -> None:
|
||||
target = docParser()
|
||||
@ -853,7 +860,7 @@ def load_apis(module: str, api_xml: str):
|
||||
|
||||
try:
|
||||
onlyOverrides = True
|
||||
with open(override_api_xml) as stream:
|
||||
with openSourceFile(override_api_xml) as stream:
|
||||
parse(stream)
|
||||
except IOError as msg:
|
||||
print(override_api_xml, ":", msg)
|
||||
@ -869,9 +876,9 @@ def emit_c_code(module: str) -> None:
|
||||
|
||||
nb_wrap = 0
|
||||
|
||||
header_file = "build/%s.h" % module
|
||||
export_file = "build/%s-export.c" % module
|
||||
wrapper_file = "build/%s.c" % module
|
||||
header_file = "%s/%s.h" % (buildDir, module)
|
||||
export_file = "%s/%s-export.c" % (buildDir, module)
|
||||
wrapper_file = "%s/%s.c" % (buildDir, module)
|
||||
|
||||
include = open(header_file, "w")
|
||||
include.write("/* Generated by generator.py */\n\n")
|
||||
@ -885,7 +892,7 @@ def emit_c_code(module: str) -> None:
|
||||
wrapper.write("#include <Python.h>\n")
|
||||
wrapper.write("#include <libvirt/%s.h>\n" % (module,))
|
||||
wrapper.write("#include \"typewrappers.h\"\n")
|
||||
wrapper.write("#include \"build/%s.h\"\n\n" % (module,))
|
||||
wrapper.write("#include \"%s/%s.h\"\n\n" % (buildDir, module))
|
||||
|
||||
for function in sorted(functions):
|
||||
if print_function_wrapper(package, function, wrapper, export, include):
|
||||
@ -1313,14 +1320,12 @@ def emit_py_code(module: str) -> None:
|
||||
info = (0, func, name, ret, args, file, mod)
|
||||
function_classes['None'].append(info)
|
||||
|
||||
classes_file = "build/%s.py" % package
|
||||
classes_file = "%s/%s.py" % (buildDir, package)
|
||||
extra_file = "%s-override.py" % module
|
||||
extra = None
|
||||
extra = openSourceFile(extra_file, "r", True)
|
||||
|
||||
classes = open(classes_file, "w")
|
||||
|
||||
if os.path.exists(extra_file):
|
||||
extra = open(extra_file, "r")
|
||||
classes.write("#\n")
|
||||
classes.write("# WARNING WARNING WARNING WARNING\n")
|
||||
classes.write("#\n")
|
||||
@ -1629,8 +1634,8 @@ def emit_py_code(module: str) -> None:
|
||||
classes.write("\n")
|
||||
# Append "<classname>.py" to class def, iff it exists
|
||||
class_override = "%s-override-%s.py" % (module, classname)
|
||||
if os.path.exists(class_override):
|
||||
extra = open(class_override, "r")
|
||||
extra = openSourceFile(class_override, "r", True)
|
||||
if extra:
|
||||
classes.write(" #\n")
|
||||
classes.write(" # %s methods from %s.py (hand coded)\n" % (classname, classname))
|
||||
classes.write(" #\n")
|
||||
@ -1776,6 +1781,11 @@ if sys.argv[1] not in ["libvirt", "libvirt-lxc", "libvirt-qemu"]:
|
||||
print("ERROR: unknown module %s" % sys.argv[1])
|
||||
sys.exit(1)
|
||||
|
||||
if len(sys.argv) == 6:
|
||||
buildDir = sys.argv[5]
|
||||
if len(sys.argv) >= 5:
|
||||
sourceDir = sys.argv[4]
|
||||
|
||||
load_apis(sys.argv[1], sys.argv[2])
|
||||
|
||||
if validate_functions() < 0:
|
||||
@ -1786,12 +1796,12 @@ if not os.path.exists("build"):
|
||||
os.mkdir("build")
|
||||
|
||||
output = None
|
||||
if len(sys.argv) == 4:
|
||||
if len(sys.argv) >= 4:
|
||||
output = sys.argv[3]
|
||||
if output == "c" or output is None:
|
||||
if output == "c" or output == "c+py" or output is None:
|
||||
emit_c_code(sys.argv[1])
|
||||
|
||||
if output == "py" or output is None:
|
||||
if output == "py" or output == "c+py" or output is None:
|
||||
emit_py_code(sys.argv[1])
|
||||
|
||||
sys.exit(0)
|
||||
|
Reference in New Issue
Block a user