import os # Define global variables totalAliasCount = 0 uniqueAlias = {} def fileOpener(path): dirList = os.listdir(path) global totalAliasCount global uniqueAlias for f in dirList: # ignore test, input, output, and Kustomization files if "test" in f or "kustomization" in f or "output" in f or "input" in f: continue file = open(path + "/" + f, 'r') lines = file.readlines() totalAliasCount, uniqueAlias = globalAliasFinder(totalAliasCount, uniqueAlias, lines , f) def globalAliasFinder(totalAliasCount, uniqueAlias, lines, filename): for line in lines: if "alias" in line or "Alias" in line: totalAliasCount += 1 linesplit = line.split() # Example of linesplit results # ['alias:', 'kured'] key = linesplit[1] if key in uniqueAlias.keys(): if filename not in uniqueAlias[key][0]: uniqueAlias[key][0].append(filename) uniqueAlias[key][1] += 1 else: uniqueAlias[key] = [[filename], 1] return totalAliasCount, uniqueAlias # # logic for individual file finder # def aliasFinderWithinFile(file): # global totalAliasCount # global uniqueAlias # openedFile = open(file, "r") # lines = openedFile.readlines() # # split the file name on "/" so we can extract the file name which will be the last value in the split # filename = file.split("/") # totalAliasCount, uniqueAlias = globalAliasFinder(totalAliasCount, uniqueAlias, lines , filename[-1]) # Get the list of all files and directories def main(): path = os.getcwd() # Directories containerPath = "/config/pallets/o11y/fluentbit/base/k8s-container" nodePath = "/config/pallets/o11y/fluentbit/base/k8s-node" luaPath = "/config/pallets/o11y/fluentbit/base/lua-preprocess" restrictionPath = "/config/pallets/o11y/fluentbit-restrictions/" fileOpener(path + containerPath) fileOpener(path + nodePath) fileOpener(path + luaPath) fileOpener(path + restrictionPath) print(totalAliasCount, " alias found") print( "---------------") for key, value in uniqueAlias.items(): if value[1] > 1: raise Exception(key, " has duplicate alias names (", value[1], " times) . List of files with same name alias: ", value[0]) else: print(key, "is a clean alias") if __name__=="__main__": main()