Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from pprint import pformat
- def read_pip_freeze_file(file_path):
- try:
- with open(file_path, 'r') as f:
- return [l.strip() for l in f.readlines()]
- except Exception as e:
- print("Error while reading pip freeze file: {}".format(e))
- def parse_line(line):
- if "===" in line:
- pkg, version = line.split("===")
- elif "==" in line:
- pkg, version = line.split("==")
- elif line.startswith("-e"):
- pkg = line.split("=")[-1]
- version = "egg"
- else:
- pkg, version = None, None
- print("Couldn't understand line: {}".format(line))
- return pkg, version
- def get_package_dict(file_path):
- lines = read_pip_freeze_file(file_path)
- return dict([parse_line(line) for line in lines])
- def compare_envs(d1, d2):
- pkg1 = set(list(d1.keys()))
- pkg2 = set(list(d2.keys()))
- in_both = pkg1.intersection(pkg2)
- only_in_1 = list(pkg1 - in_both)
- only_in_2 = list(pkg2 - in_both)
- # compare versions
- diff_version = []
- same_version = []
- for pkg in in_both:
- assert pkg not in only_in_1
- assert pkg not in only_in_2
- v1, v2 = d1.get(pkg), d2.get(pkg)
- if v1 != v2:
- diff_version.append("{} ({} vs {})".format(pkg, v1, v2))
- else:
- same_version.append("{}=={}".format(pkg, v1))
- return only_in_1, only_in_2, diff_version, same_version
- if __name__ == "__main__":
- try:
- env1 = sys.argv[1]
- env2 = sys.argv[2]
- except IndexError:
- print("you need to pass the name of two environments at the commmand line")
- e1 = get_package_dict(env1)
- e2 = get_package_dict(env2)
- in_one, in_two, diff_version, same_version = compare_envs(e1, e2)
- print("\n", "-" * 20, "only in {}".format(env1), "-" * 20)
- print(pformat(in_one))
- print("-" * 20, "only in {}".format(env2), "-" * 20)
- print(pformat(in_two))
- print("\n", "-" * 20, "Common same version", "-" * 20)
- print(pformat(same_version))
- print("\n", "-" * 20, "Common different version", "-" * 20)
- print(pformat(diff_version))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement