- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
#!/usr/bin/env python
import jinja2
import contextlib
import os.path
import shutil
import subprocess
import sys
import tempfile
@contextlib.contextmanager
def rm_dir(d):
yield d
shutil.rmtree(d)
def main():
if len(sys.argv) < 2:
return
if os.path.basename(sys.argv[1]) != "compile":
subprocess.call(sys.argv[1:])
return
command = sys.argv[1:]
preprocessed_files = []
with rm_dir(tempfile.mkdtemp()) as tmpdir:
print "Temp dir is", tmpdir
while len(command) > 0 and not command[-1].startswith("-") and command[-1].endswith(".go"):
tmpfile = os.path.join(tmpdir, str(len(command)) + ".go")
with open(command[-1]) as input:
with open(tmpfile, "wb") as preprocessed:
print "Preprocess", command[-1], "into", tmpfile
preprocessed.write(jinja2.Template(input.read()).render())
preprocessed_files.append(tmpfile)
command.pop()
subprocess.call(command + preprocessed_files)
if __name__ == "__main__":
main()
Прикручиваем шаблонизатор jinja к го, чтобы копипастить с комфортом. Использовать так: