import json, sys, os, hashlib
if len(sys.argv) < 2:
print "You must specify the pipeline configuration as argument."
sys.exit(1)
rules = []
commands = []
def parse_dependencies(j):
return [v for k , v, in j.items()]
def eq(js, config_name):
f = open(config_name)
cd = f.read()
f.close()
js5 = hashlib.md5()
js5.update(js)
cn5 = hashlib.md5()
cn5.update(cd)
return js5.digest() == cn5.digest()
def write_config(target, config, variables):
config_name = ".pipeline/config.json." + target.replace("/", "_") + ".cfg"
if "target" in config:
print "found reserved word 'target' in configuration of " + target
sys.exit(1)
config["target"] = target
config.pop("comment", "")
js = replace_vars(json.dumps(config), variables)
if not os.path.exists(config_name) or not eq(js, config_name):
print "writing configuration " + config_name + " ..."
open(config_name, "w").write(js)
return config_name
def replace_vars(s, variables):
r = s
for key, val in variables.items():
r = r.replace("$$" + key + "$$", str(val))
return r
if not os.path.exists(".pipeline"):
os.makedirs(".pipeline")
j = json.loads(open(sys.argv[1]).read())
variables = j.get("variables", {})
for target, v in j["stages"].items():
config_name = write_config(target, v, variables)
rules.append(
target + ": " +
" ".join([replace_vars(b, variables) for a, b in v["dependencies"].items()]) + " " + config_name
)
cpth = os.getcwdu() + "/" + config_name
cmd = replace_vars(v["command"], variables)
commands.append(
"@echo '\x1b[33;01m'executing target\x1b[0m [\"" + target + "\"], " +
"'\x1b[33;01m'cmd\x1b[0m [\"" + cmd + "\"]\n\t" +
"@PIPELINE_CONFIG=" + cpth + " sh -c '" + cmd + "'"
)
print "writing Makefile ..."
f = open("Makefile", "w")
print >> f, "all: Makefile " + " ".join(j["targets"])
print >> f
print >> f, "Makefile: " + sys.argv[1]
print >> f, "\t" + sys.argv[0] + " " + sys.argv[1]
for i, j in zip(rules, commands):
print >> f
print >> f, i
print >> f, "\t" + j
print >> f, "\nclean:"
print >> f, "\trm -rf .pipeline"