Last active
September 21, 2020 22:47
-
-
Save tkisason/5d91b1471f2206fcda3e0fdf4c2b8bda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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