| 1 |
import subprocess |
| 2 |
|
| 3 |
|
| 4 |
def _render_template(template, environment): |
| 5 |
try: |
| 6 |
return template.render(**environment) |
| 7 |
except Exception: |
| 8 |
# In the event there's an error, this imports some helpers from mako |
| 9 |
# to print a useful stack trace and prints it, then exits with |
| 10 |
# status 1, if python is run with debug; otherwise it just raises |
| 11 |
# the exception |
| 12 |
import sys |
| 13 |
from mako import exceptions |
| 14 |
|
| 15 |
print(exceptions.text_error_template().render(), file=sys.stderr) |
| 16 |
sys.exit(1) |
| 17 |
|
| 18 |
|
| 19 |
def write_template(out_file, template, environment): |
| 20 |
with open(out_file, "w", encoding="utf-8") as f: |
| 21 |
f.write(_render_template(template, environment)) |
| 22 |
|
| 23 |
|
| 24 |
def write_template_rs(out_file, template, environment): |
| 25 |
contents = _render_template(template, environment) |
| 26 |
|
| 27 |
try: |
| 28 |
contents = subprocess.check_output( |
| 29 |
["rustfmt"], |
| 30 |
input=contents, |
| 31 |
text=True, |
| 32 |
) |
| 33 |
except (subprocess.CalledProcessError, FileNotFoundError, PermissionError): |
| 34 |
pass |
| 35 |
|
| 36 |
with open(out_file, "w", encoding="utf-8") as f: |
| 37 |
f.write(contents) |