Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import sys
  2. from pprint import pformat
  3.  
  4.  
  5. def read_pip_freeze_file(file_path):
  6. try:
  7. with open(file_path, 'r') as f:
  8. return [l.strip() for l in f.readlines()]
  9. except Exception as e:
  10. print("Error while reading pip freeze file: {}".format(e))
  11.  
  12.  
  13. def parse_line(line):
  14. if "===" in line:
  15. pkg, version = line.split("===")
  16. elif "==" in line:
  17. pkg, version = line.split("==")
  18. elif line.startswith("-e"):
  19. pkg = line.split("=")[-1]
  20. version = "egg"
  21. else:
  22. pkg, version = None, None
  23. print("Couldn't understand line: {}".format(line))
  24. return pkg, version
  25.  
  26.  
  27. def get_package_dict(file_path):
  28. lines = read_pip_freeze_file(file_path)
  29. return dict([parse_line(line) for line in lines])
  30.  
  31.  
  32. def compare_envs(d1, d2):
  33. pkg1 = set(list(d1.keys()))
  34. pkg2 = set(list(d2.keys()))
  35. in_both = pkg1.intersection(pkg2)
  36. only_in_1 = list(pkg1 - in_both)
  37. only_in_2 = list(pkg2 - in_both)
  38.  
  39. # compare versions
  40. diff_version = []
  41. same_version = []
  42.  
  43. for pkg in in_both:
  44. assert pkg not in only_in_1
  45. assert pkg not in only_in_2
  46.  
  47. v1, v2 = d1.get(pkg), d2.get(pkg)
  48. if v1 != v2:
  49. diff_version.append("{} ({} vs {})".format(pkg, v1, v2))
  50. else:
  51. same_version.append("{}=={}".format(pkg, v1))
  52.  
  53. return only_in_1, only_in_2, diff_version, same_version
  54.  
  55.  
  56. if __name__ == "__main__":
  57. try:
  58. env1 = sys.argv[1]
  59. env2 = sys.argv[2]
  60. except IndexError:
  61. print("you need to pass the name of two environments at the commmand line")
  62.  
  63. e1 = get_package_dict(env1)
  64. e2 = get_package_dict(env2)
  65. in_one, in_two, diff_version, same_version = compare_envs(e1, e2)
  66.  
  67. print("\n", "-" * 20, "only in {}".format(env1), "-" * 20)
  68. print(pformat(in_one))
  69. print("-" * 20, "only in {}".format(env2), "-" * 20)
  70. print(pformat(in_two))
  71. print("\n", "-" * 20, "Common same version", "-" * 20)
  72. print(pformat(same_version))
  73. print("\n", "-" * 20, "Common different version", "-" * 20)
  74. print(pformat(diff_version))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement