Browse Source

Fix flaky Python generator

The existing code checked if a directory existed and if it did, it would
not attempt to create it. However, this proved to be flaky on Mac and Linux.
The check would frequently report the directory as not existing, but the
attempt to create the directory would cause an exception.
master
Marius Volkhart 6 years ago
parent
commit
28ffdb84b1
1 changed files with 10 additions and 10 deletions
  1. +10
    -10
      src/generator/template.py

+ 10
- 10
src/generator/template.py View File

@@ -56,16 +56,16 @@ author = "Mike Hamburg" # FUTURE
for name in args.files: for name in args.files:
_,_,name_suffix = name.rpartition(".") _,_,name_suffix = name.rpartition(".")
template0 = open(name,"r").read() template0 = open(name,"r").read()
data = per_map[args.per][args.item] data = per_map[args.per][args.item]


template = template0 template = template0
outname = args.o outname = args.o
guard = args.guard guard = args.guard
if guard is None: guard = outname if guard is None: guard = outname
header_guard = "__" + guard.replace(".","_").replace("/","_").upper() + "__" header_guard = "__" + guard.replace(".","_").replace("/","_").upper() + "__"
# Extract doxygenation # Extract doxygenation
m = re.match(r"^\s*/\*\*([^*]|\*[^/])+\*/[ \t]*\n",template) m = re.match(r"^\s*/\*\*([^*]|\*[^/])+\*/[ \t]*\n",template)
if m: if m:
@@ -73,12 +73,12 @@ for name in args.files:
doc = re.sub("\\s*\*/","",doc) doc = re.sub("\\s*\*/","",doc)
template = template[m.end():] template = template[m.end():]
else: doc = "" else: doc = ""
ns_doc = dedent(doc).strip().rstrip() ns_doc = dedent(doc).strip().rstrip()
ns_doc = redoc(guard, fillin(ns_doc,data), author) ns_doc = redoc(guard, fillin(ns_doc,data), author)
ns_code = fillin(template,data) ns_code = fillin(template,data)
ret = ns_doc + "\n" ret = ns_doc + "\n"
if outname.endswith(".h") or outname.endswith(".hxx"): if outname.endswith(".h") or outname.endswith(".hxx"):
ns_code = dedent("""\n ns_code = dedent("""\n
#ifndef %(header_guard)s #ifndef %(header_guard)s
@@ -87,11 +87,11 @@ for name in args.files:
#endif /* %(header_guard)s */ #endif /* %(header_guard)s */
""") % { "header_guard" : header_guard, "code": ns_code } """) % { "header_guard" : header_guard, "code": ns_code }
ret += ns_code[1:-1] ret += ns_code[1:-1]
if not os.path.exists(os.path.dirname(outname)):
try:
os.makedirs(os.path.dirname(outname)) os.makedirs(os.path.dirname(outname))
except OSError as e:
if e.errno != 17: # errno.EEXIST
raise
with open(outname,"w") as f: with open(outname,"w") as f:
f.write(ret + "\n") f.write(ret + "\n")



Loading…
Cancel
Save