move output file opening to backend
Different backends have different requirements.
diff --git a/lang_c.py b/lang_c.py
index fc016f2..9cfc2fa 100644
--- a/lang_c.py
+++ b/lang_c.py
@@ -39,6 +39,7 @@
import c_gen.c_show_gen as c_show_gen
import c_gen.c_validator_gen as c_validator_gen
import c_gen.util
+import loxi_utils.loxi_utils as loxi_utils
def static(out, name):
c_gen.util.render_template(out, os.path.basename(name))
@@ -110,3 +111,8 @@
'locitest/src/main.c': static,
'locitest/Makefile': static,
}
+
+def generate():
+ for (name, fn) in targets.items():
+ with loxi_utils.open_output(name) as outfile:
+ fn(outfile, os.path.basename(name))
diff --git a/lang_java.py b/lang_java.py
index be94e82..7b992d7 100644
--- a/lang_java.py
+++ b/lang_java.py
@@ -42,9 +42,16 @@
"""
+import os
+import loxi_utils.loxi_utils as loxi_utils
import java_gen.codegen as java_codegen
targets = {
'openflowj/README': java_codegen.gen_all_java
}
+
+def generate():
+ for (name, fn) in targets.items():
+ with loxi_utils.open_output(name) as outfile:
+ fn(outfile, os.path.basename(name))
diff --git a/lang_python.py b/lang_python.py
index 639cf1a..2dd57b5 100644
--- a/lang_python.py
+++ b/lang_python.py
@@ -61,6 +61,8 @@
"ofp.OFPP_NONE".
"""
+import os
+import loxi_utils.loxi_utils as loxi_utils
import py_gen
import py_gen.util
import py_gen.codegen
@@ -99,3 +101,8 @@
for module in modules[version]:
filename = '%s/%s/%s.py' % (prefix, subdir, module)
targets[filename] = make_gen(module, version)
+
+def generate():
+ for (name, fn) in targets.items():
+ with loxi_utils.open_output(name) as outfile:
+ fn(outfile, os.path.basename(name))
diff --git a/lang_wireshark.py b/lang_wireshark.py
index 9f73543..509c9cd 100644
--- a/lang_wireshark.py
+++ b/lang_wireshark.py
@@ -36,8 +36,15 @@
loaded automatically by Wireshark.
"""
+import os
+import loxi_utils.loxi_utils as loxi_utils
import wireshark_gen
targets = {
'wireshark/openflow.lua' : wireshark_gen.generate
}
+
+def generate():
+ for (name, fn) in targets.items():
+ with loxi_utils.open_output(name) as outfile:
+ fn(outfile, os.path.basename(name))
diff --git a/loxi_utils/loxi_utils.py b/loxi_utils/loxi_utils.py
index 601a65d..059c363 100644
--- a/loxi_utils/loxi_utils.py
+++ b/loxi_utils/loxi_utils.py
@@ -34,6 +34,7 @@
"""
import sys
+import os
import of_g
import tenjin
from generic_utils import find, memoize
@@ -543,3 +544,16 @@
context.update(kwargs)
template = self.get_template(template_name, context, globals)
return template.render(context, globals, _buf=locals["_buf"])
+
+def open_output(name):
+ """
+ Open an output file for writing
+
+ 'name' may include slashes. Subdirectories will be automatically created.
+ """
+ print "Writing %s" % name
+ path = os.path.join(of_g.options.install_dir, name)
+ dirpath = os.path.dirname(path)
+ if not os.path.exists(dirpath):
+ os.makedirs(dirpath)
+ return open(path, "w")
diff --git a/loxigen.py b/loxigen.py
index 05dd6b5..7b78f63 100755
--- a/loxigen.py
+++ b/loxigen.py
@@ -630,12 +630,7 @@
"""
Create the files for the language target
"""
- for (name, fn) in lang_module.targets.items():
- path = of_g.options.install_dir + '/' + name
- os.system("mkdir -p %s" % os.path.dirname(path))
- with open(path, "w") as outfile:
- fn(outfile, os.path.basename(name))
- print("Wrote contents for " + name)
+ lang_module.generate()
if __name__ == '__main__':
of_g.loxigen_log_file = open("loxigen.log", "w")