Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. from pathlib import Path
  2. import os
  3. import pprint
  4. import argparse
  5.  
  6. parser = argparse.ArgumentParser()
  7. parser.add_argument("dep", help="Name of the cookbook.", type=str)
  8.  
  9. parser.add_argument('-lc','--listcommon', nargs='+', help='List of cookbook, used to find common package', type=str, required=False)
  10. parser.add_argument("-d", "--depth", help="depth of dependency, by default : 2", type=int, default=2)
  11. parser.add_argument("-w", "--who", help="Give cookbooks that use this cookbook", action="store_true")
  12. parser.add_argument("-t", "--template", help="dependencies from template file", action="store_true")
  13.  
  14. args = parser.parse_args()
  15.  
  16. dependency = {}
  17. versions = {}
  18. tab = ' '
  19.  
  20. berksfile = Path('./Berksfile.lock')
  21.  
  22. with berksfile.open('r') as berks:
  23. line = berks.readline()
  24. insert = False
  25.  
  26. currentParentDep = ""
  27.  
  28. while line:
  29. if line == "GRAPH\n":
  30. insert = True
  31.  
  32. line = berks.readline()
  33. if insert:
  34. nline = line.split(tab)
  35. l = len(nline)
  36.  
  37. if l == 2:
  38. dependency_vers = nline[1][:-1]
  39. name_and_vers = dependency_vers.split(' (')
  40. currentParentDep = name_and_vers[0]
  41. dependency[currentParentDep] = []
  42. if currentParentDep not in versions:
  43. versions[currentParentDep] = name_and_vers[1][:-1]
  44. elif l == 3:
  45. dependency_vers = nline[2][:-1]
  46. name_and_vers = dependency_vers.split(' (')
  47. dependency[currentParentDep].append(name_and_vers[0])
  48. if currentParentDep not in versions:
  49. versions[currentParentDep] = name_and_vers[1][:-1]
  50.  
  51.  
  52. def rec_find_dep(name, depth):
  53. if name in dependency:
  54.  
  55. print(" " * depth, end='')
  56. print("- {} ~> {}".format(name, versions[name]), end='')
  57. if args.depth == depth:
  58. print("")
  59. return
  60.  
  61. listOfDep = dependency[name]
  62. if len(listOfDep) > 0:
  63. print(" : ")
  64. else:
  65. print("")
  66.  
  67. for dep in listOfDep:
  68. rec_find_dep(dep, depth+1)
  69.  
  70.  
  71. def find_common_dep(listOfDep):
  72. first = set(dependency[listOfDep[0]])
  73. deps = []
  74.  
  75. for dep in listOfDep[1:]:
  76. if dep not in dependency:
  77. print("No dependency \033[1m{}\033[0m".format(dep))
  78. return
  79.  
  80. deps.append(set(dependency[dep]))
  81.  
  82. commonDep = first.intersection(*deps)
  83.  
  84. for d in commonDep:
  85. print("{} ~> {}".format(d, versions[d]))
  86.  
  87. def who_use_me(name):
  88. for k,v in dependency.items():
  89. if name in v:
  90. print("- {} ~> {}".format( k, versions[k]))
  91.  
  92.  
  93.  
  94. def deps_from_conf(cookbookPath):
  95. import re
  96.  
  97. blacklist = {"git", "source", "install_dir", "config_dir", "environment", "error_log",
  98. "test_configuration", "install_method", "app_name", "user_installs", "log_file", "applications",
  99. "service", "version", "region", "healthcheck_name", "blockedUserAgents", "default_autoscaling_group",
  100. "default_load_balancer"}
  101.  
  102. attributes = cookbookPath / "attributes"
  103. recipes = cookbookPath / "recipes"
  104.  
  105. attributesDefault = attributes / "default.rb"
  106.  
  107. regex = "(default|force_default|normal|override|force_override|automatic)(\[.+\])+(?=\s*=)\s*=\s*"
  108. validDict = re.compile(regex, re.UNICODE)
  109.  
  110.  
  111. first = set()
  112. second = set()
  113.  
  114. definitions = validDict.findall(attributesDefault.read_text())
  115.  
  116. for defi in definitions:
  117. keys = defi[1].replace("']['", " ")[2:-2].split(" ")
  118. first.add(keys[1])
  119. second.add(keys[0])
  120.  
  121. print("---- intern deps of \033[1m {} \033[0m ----".format(cookbookPath))
  122. for dep in first - blacklist:
  123. print("~ \033[91m", dep, "\033[0m")
  124.  
  125. print("---- extern deps of \033[1m {} \033[0m ----".format(cookbookPath))
  126. for dep in second - blacklist - {args.dep}:
  127. print("~ \033[93m", dep, "\033[0m")
  128.  
  129.  
  130. """
  131.  
  132. def dep_from_tempconf(path):
  133. import json
  134. import re
  135.  
  136. conf = path / "templates" / "default" / "config.json.erb"
  137. content = conf.read_text()
  138. res = re.sub("(\")?<%=(.)*%>(\")?", "\"\"", content)
  139. res = re.sub("<%(.)*%>", "", res)
  140. jmap = json.loads(res)
  141.  
  142. for k, _ in jmap.items():
  143. print(k)
  144. """
  145.  
  146. if args.who is False:
  147. if args.template:
  148. cookbookPath = Path('./site-cookbooks/{}'.format(args.dep))
  149. deps_from_conf(cookbookPath)
  150. elif args.listcommon is None or len(args.listcommon) == 0:
  151. rec_find_dep(args.dep, 0)
  152. else:
  153. listOfDep = args.listcommon + [args.dep]
  154. find_common_dep(listOfDep)
  155. else:
  156. who_use_me(args.dep)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement