Skip to content

Instantly share code, notes, and snippets.

@tkisason
Last active September 21, 2020 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkisason/5d91b1471f2206fcda3e0fdf4c2b8bda to your computer and use it in GitHub Desktop.
Save tkisason/5d91b1471f2206fcda3e0fdf4c2b8bda to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import sys
from ruamel.yaml import YAML
def getRulefilesFromPath(path):
rulefiles = []
for root, dirs, files in os.walk(path):
dirs[:] = [d for d in dirs if not d.startswith(".")]
for name in files:
file = os.path.join(root, name)
if (file.endswith(".yml") or file.endswith(".yaml")) and (
not name.startswith(".")
):
rulefiles.append(file)
print(f"Found {len(rulefiles)} rulefiles")
return rulefiles
def mergeRules(rulefilelist, outputfile):
yaml = YAML(typ="rt")
rulefile = {"rules": []}
for file in rulefilelist:
rulefileyaml = yaml.load(open(file))
rulefile["rules"] += rulefileyaml["rules"]
rulecount = len(rulefile["rules"])
print(f"Created {rulecount} rules")
output = open(outputfile, "w")
yaml.dump(rulefile, output)
output.close()
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Merges all yaml/yml files in a directory and all subdirs")
print("Skips directories and files prefixed with a .")
print(f"Usage {sys.argv[0]} [input folder] [output filename]")
else:
rules = getRulefilesFromPath(sys.argv[1])
print("Merging...")
mergeRules(rules, sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment