Switch to python and jinja
This commit is contained in:
parent
9d4346900f
commit
52fd2a9670
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
out/
|
||||
local.env
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
#!/usr/bin/env -S perl -s
|
||||
|
||||
my %envvars = ();
|
||||
|
||||
if (!$env) {
|
||||
print "Usage:\n";
|
||||
print "\t$0 -env=/path/to/env [template0] [template1] [templateN]\n";
|
||||
print "or reading template contents from STDIN:\n";
|
||||
print "\t$0 -env=/path/to/env\n";
|
||||
exit -1
|
||||
}
|
||||
|
||||
open (my $fh, "<", $env) or die $!;
|
||||
while(my $line = <$fh>) {
|
||||
my ($key, $value) = $line =~ /^([^={}]+)=([^={}]+?)\n?$/;
|
||||
|
||||
if (!$key || !value) {
|
||||
next;
|
||||
}
|
||||
|
||||
$envvars{$key} = $value;
|
||||
}
|
||||
|
||||
while (<>) {
|
||||
for $key (keys %envvars) {
|
||||
my $value = $envvars{$key};
|
||||
s/{{$key}}/$value/g;
|
||||
}
|
||||
|
||||
print $_;
|
||||
}
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Jinja2==3.0.3
|
||||
MarkupSafe==2.0.1
|
||||
85
run.py
Executable file
85
run.py
Executable file
@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Recursively read for all files
|
||||
template_directory = './templates/'
|
||||
|
||||
# Written to when generation is done
|
||||
out_directory = './out/'
|
||||
|
||||
# Applied in order, later files override earlier files
|
||||
env_files = ['./vars.env', './local.env']
|
||||
|
||||
import jinja2
|
||||
import re
|
||||
import os
|
||||
|
||||
def main():
|
||||
key_value_store = create_key_value_store()
|
||||
template_files = find_all_templates()
|
||||
out_files = convert_templates_to_out_file(template_files)
|
||||
|
||||
convert_all_templates(template_files, key_value_store, out_files)
|
||||
|
||||
def create_key_value_store():
|
||||
out = {}
|
||||
|
||||
for env_file in env_files:
|
||||
with open(env_file) as file:
|
||||
data = file.read()
|
||||
|
||||
for line in data.split('\n'):
|
||||
m = re.match(r'^([^={}]+)=([^={}\n]+)', line)
|
||||
if m is None:
|
||||
continue
|
||||
|
||||
out[m.group(1)] = m.group(2)
|
||||
|
||||
return out
|
||||
|
||||
def find_all_templates():
|
||||
files = []
|
||||
|
||||
dirlist = [template_directory]
|
||||
|
||||
while len(dirlist) > 0:
|
||||
for (dirpath, dirnames, filenames) in os.walk(dirlist.pop()):
|
||||
dirlist.extend(dirnames)
|
||||
files.extend(map(lambda n: os.path.join(*n), zip([dirpath] * len(filenames), filenames)))
|
||||
|
||||
return files
|
||||
|
||||
def convert_templates_to_out_file(template_files):
|
||||
out_files = []
|
||||
|
||||
real_template_dir = os.path.realpath(template_directory)
|
||||
real_out_dir = os.path.realpath(out_directory)
|
||||
|
||||
for template_file in template_files:
|
||||
real_template_file = os.path.realpath(template_file)
|
||||
out_files += [real_template_file.replace(real_template_dir, real_out_dir)]
|
||||
|
||||
return out_files
|
||||
|
||||
def convert_all_templates(template_files, key_value_store, out_files):
|
||||
env = jinja2.Environment()
|
||||
|
||||
for (template, out) in zip(template_files, out_files):
|
||||
convert_template(env, template, key_value_store, out)
|
||||
|
||||
def convert_template(env, template, key_value_store, out):
|
||||
with open(template, 'r') as file:
|
||||
source = file.read()
|
||||
|
||||
template = env.from_string(source)
|
||||
rendered = template.render(**key_value_store)
|
||||
|
||||
out_dir_name = os.path.dirname(out)
|
||||
if not os.path.isdir(out_dir_name):
|
||||
os.makedirs(out_dir_name)
|
||||
|
||||
with open(out, 'w') as file:
|
||||
file.write(rendered)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
23
run.sh
23
run.sh
@ -1,23 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
TEMPLATE_DIR="./templates/"
|
||||
OUT_DIR="./out/"
|
||||
ENV_FILE="./vars.env"
|
||||
|
||||
mkdir -p "$TEMPLATE_DIR"
|
||||
|
||||
cd "$TEMPLATE_DIR"
|
||||
template_files=$(find -type f)
|
||||
cd - > /dev/null
|
||||
|
||||
for file in $template_files
|
||||
do
|
||||
out_path=$(realpath -m "$OUT_DIR$file")
|
||||
mkdir -p $(dirname "$out_path")
|
||||
|
||||
./fill-template.pl -env="$ENV_FILE" "$TEMPLATE_DIR$file" > $out_path
|
||||
done
|
||||
Loading…
Reference in New Issue
Block a user