Commit 2cabf6fb authored by Jakub Jalowiec's avatar Jakub Jalowiec
Browse files

Adding files

parent 1b2e8baa
# fill-template
A simple script to fill up templates based on configuration of fields passed in .json files.
\ No newline at end of file
import json
import jinja2
from jinja2 import Template
import os
from shutil import copyfile
from stat import S_IREAD, S_IRGRP
import argparse
import logging
import sys
templates_dir = "./templates"
default_json = "./default.json"
config_json = "./config.json"
parser = argparse.ArgumentParser(description='Fill up templates in the specified directory with default/custom values.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-c', default="./templates/config.json",
help='path to the JSON file with custom values')
parser.add_argument('-d', default="./templates/default.json",
help='path to the JSON file with default values')
parser.add_argument('-t', default="./templates/",
help='path to the directory with templates')
parser.add_argument('-l', default="INFO",
help='log level', choices=["ERROR", "INFO"])
args = parser.parse_args()
templates_dir = args.t
default_json = args.d
config_json = args.c
log_level = args.l
format = '%(message)s'
if(log_level == "ERROR"):
logging.basicConfig(level=logging.ERROR, format=format)
elif (log_level == "INFO"):
logging.basicConfig(level=logging.INFO, format=format)
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
logging.info("Templates directory: " + templates_dir)
logging.info("File with custom values: " + config_json)
logging.info("File with default values: " + default_json)
with open(default_json, "r") as read_file:
default = json.load(read_file)
global_values = default.get("global", dict())
for key in default:
if not key == "global":
for global_default_key in global_values:
default[key][global_default_key] = default[key].get(global_default_key, global_values[global_default_key])
with open(config_json, "r") as write_file:
config = json.load(write_file)
for directory, content in config.items():
logging.info("Target directory: " + directory)
path = "./" + directory
if(os.path.exists(path)):
raise Exception("Path " + path + " already exists!")
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
for template_name, files in content.items():
# if the key does not exist return an empty dictionary
dictionary = default.get(template_name, dict())
for target, custom_values in files.items():
if(len(custom_values) > 0):
logging.info("Filling up " + template_name + " with values from " + target + " dictionary and saving as " + directory + "/" + target + ".")
else:
logging.info("Copying file " + template_name + " as " + directory + "/" + target + ".")
# absolute or relative path?
if(template_name[0] == '.' or template_name[0] == '/'):
template_path = template_name
else:
template_path = templates_dir + template_name
t = templateEnv.get_template(template_path)
for key, value in custom_values.items():
dictionary[key] = value
dictionary['directory'] = directory
output = t.render(dictionary)
with open(path + "/" + target, 'w') as filetowrite:
filetowrite.write(output)
os.chmod(path + "/" + target, S_IREAD|S_IRGRP) # read-only
logging.info("Copying " + config_json + " to " + path + "/config.json")
copyfile(config_json, path + "/config.json")
logging.info("Copying " + default_json + " to " + path + "/default.json")
copyfile(default_json, path + "/default.json")
sys.exit(0)
\ No newline at end of file
{
"xgt-ldbc":
{
"run.template": {
"run.sh": {
"time": "00:30:00"
}
}
}
}
{
"global": {
"time": "00:10:00",
"cpus_per_task": 1,
"memory": "64G"
}
}
#!/bin/bash -l
#SBATCH -J {{directory}}
#SBATCH --output="{{directory}}.log"
#SBATCH --time={{time}}
#SBATCH --mem={{memory}}
#SBATCH --cpus-per-task={{cpus_per_task}}
#SBATCH -A GR84-25
#SBATCH --gres=xgt
echo "venv activation"
source ~/trovares/xgt/bin/activate
echo "run_xgtd"
/opt/xgtd/bin/run_xgtd.sh
python benchmark.py
\ No newline at end of file
{
"out": {
"test.template": { "test.out": {} }
}
}
\ No newline at end of file
{
}
\ No newline at end of file
some text
some more text
123
\ No newline at end of file
some text
some more text
123
\ No newline at end of file
{
"out":
{
"test.template":
{
"test.out": {
"value1": 123,
"value2": "asdf",
"sometext": "sometext"
}
}
}
}
\ No newline at end of file
{
}
\ No newline at end of file
value1: 123
value2: asdf
sometext
\ No newline at end of file
value1: {{value1}}
value2: {{value2}}
{{sometext}}
\ No newline at end of file
{
"out":
{
"test.template":
{
"test.out":
{
"val2": "test",
"val3": "hello from config.json"
}
}
}
}
\ No newline at end of file
{
"test.template":
{
"val1": "123",
"val3": "hello from default.json"
}
}
\ No newline at end of file
this value is only in default.json: 123
this value is only in config.json: test
this value is both in config.json and in default.json: hello from config.json
\ No newline at end of file
this value is only in default.json: {{val1}}
this value is only in config.json: {{val2}}
this value is both in config.json and in default.json: {{val3}}
\ No newline at end of file
{
"out": {
"test.template": { "test.out": {} }
}
}
\ No newline at end of file
{
"test.template": {
"value1": 123,
"value2": "asdf",
"sometext": "sometext"
}
}
\ No newline at end of file
value1: 123
value2: asdf
sometext
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment