Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- import os
- import pprint
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument("dep", help="Name of the cookbook.", type=str)
- parser.add_argument('-lc','--listcommon', nargs='+', help='List of cookbook, used to find common package', type=str, required=False)
- parser.add_argument("-d", "--depth", help="depth of dependency, by default : 2", type=int, default=2)
- parser.add_argument("-w", "--who", help="Give cookbooks that use this cookbook", action="store_true")
- parser.add_argument("-t", "--template", help="dependencies from template file", action="store_true")
- args = parser.parse_args()
- dependency = {}
- versions = {}
- tab = ' '
- berksfile = Path('./Berksfile.lock')
- with berksfile.open('r') as berks:
- line = berks.readline()
- insert = False
- currentParentDep = ""
- while line:
- if line == "GRAPH\n":
- insert = True
- line = berks.readline()
- if insert:
- nline = line.split(tab)
- l = len(nline)
- if l == 2:
- dependency_vers = nline[1][:-1]
- name_and_vers = dependency_vers.split(' (')
- currentParentDep = name_and_vers[0]
- dependency[currentParentDep] = []
- if currentParentDep not in versions:
- versions[currentParentDep] = name_and_vers[1][:-1]
- elif l == 3:
- dependency_vers = nline[2][:-1]
- name_and_vers = dependency_vers.split(' (')
- dependency[currentParentDep].append(name_and_vers[0])
- if currentParentDep not in versions:
- versions[currentParentDep] = name_and_vers[1][:-1]
- def rec_find_dep(name, depth):
- if name in dependency:
- print(" " * depth, end='')
- print("- {} ~> {}".format(name, versions[name]), end='')
- if args.depth == depth:
- print("")
- return
- listOfDep = dependency[name]
- if len(listOfDep) > 0:
- print(" : ")
- else:
- print("")
- for dep in listOfDep:
- rec_find_dep(dep, depth+1)
- def find_common_dep(listOfDep):
- first = set(dependency[listOfDep[0]])
- deps = []
- for dep in listOfDep[1:]:
- if dep not in dependency:
- print("No dependency \033[1m{}\033[0m".format(dep))
- return
- deps.append(set(dependency[dep]))
- commonDep = first.intersection(*deps)
- for d in commonDep:
- print("{} ~> {}".format(d, versions[d]))
- def who_use_me(name):
- for k,v in dependency.items():
- if name in v:
- print("- {} ~> {}".format( k, versions[k]))
- def deps_from_conf(cookbookPath):
- import re
- blacklist = {"git", "source", "install_dir", "config_dir", "environment", "error_log",
- "test_configuration", "install_method", "app_name", "user_installs", "log_file", "applications",
- "service", "version", "region", "healthcheck_name", "blockedUserAgents", "default_autoscaling_group",
- "default_load_balancer"}
- attributes = cookbookPath / "attributes"
- recipes = cookbookPath / "recipes"
- attributesDefault = attributes / "default.rb"
- regex = "(default|force_default|normal|override|force_override|automatic)(\[.+\])+(?=\s*=)\s*=\s*"
- validDict = re.compile(regex, re.UNICODE)
- first = set()
- second = set()
- definitions = validDict.findall(attributesDefault.read_text())
- for defi in definitions:
- keys = defi[1].replace("']['", " ")[2:-2].split(" ")
- first.add(keys[1])
- second.add(keys[0])
- print("---- intern deps of \033[1m {} \033[0m ----".format(cookbookPath))
- for dep in first - blacklist:
- print("~ \033[91m", dep, "\033[0m")
- print("---- extern deps of \033[1m {} \033[0m ----".format(cookbookPath))
- for dep in second - blacklist - {args.dep}:
- print("~ \033[93m", dep, "\033[0m")
- """
- def dep_from_tempconf(path):
- import json
- import re
- conf = path / "templates" / "default" / "config.json.erb"
- content = conf.read_text()
- res = re.sub("(\")?<%=(.)*%>(\")?", "\"\"", content)
- res = re.sub("<%(.)*%>", "", res)
- jmap = json.loads(res)
- for k, _ in jmap.items():
- print(k)
- """
- if args.who is False:
- if args.template:
- cookbookPath = Path('./site-cookbooks/{}'.format(args.dep))
- deps_from_conf(cookbookPath)
- elif args.listcommon is None or len(args.listcommon) == 0:
- rec_find_dep(args.dep, 0)
- else:
- listOfDep = args.listcommon + [args.dep]
- find_common_dep(listOfDep)
- else:
- who_use_me(args.dep)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement