Skip to content

Instantly share code, notes, and snippets.

Forked from ddahan/
Created February 22, 2020 23:29
Show Gist options
  • Save jcppkkk/eea79425405cb34fb316e3a11915b81f to your computer and use it in GitHub Desktop.
Save jcppkkk/eea79425405cb34fb316e3a11915b81f to your computer and use it in GitHub Desktop.
Compare two Pipfile.lock files
# Python 3.6 min
import json
import re
import argparse
DEFAULT = 'default'
VERSION = 'version'
def _remove_equal(line):
return re.sub('[=]', '', line)
def pipenvdiff():
parser = argparse.ArgumentParser(description='Compare two Pipfile.lock files')
parser.add_argument('file1', type=str, nargs='+', help='File 1 (the old one) to be compared')
parser.add_argument('file2', type=str, nargs='+', help='File 2 (the new one) to be compared')
args = parser.parse_args()
file_1, file_2 = args.file1[0], args.file2[0]
with open(file_1) as data_file_1, open(file_2) as data_file_2:
data_1_loaded, data_2_loaded = json.load(data_file_1), json.load(data_file_2)
packages_1, packages_2 = data_1_loaded[DEFAULT], data_2_loaded[DEFAULT]
changes = sorted(set(packages_1.keys()) & set(packages_2.keys()))
removed = sorted(set(packages_1.keys()) - set(packages_2.keys()))
added = sorted(set(packages_2.keys()) - set(packages_1.keys()))
if changes:
change_major = []
change_minor = []
change_patch = []
for package_name in changes:
p1_version = _remove_equal(packages_1.get(package_name, {}).get(VERSION, "[None]"))
p2_version = _remove_equal(packages_2.get(package_name, {}).get(VERSION, "[None]"))
p1_numbers = p1_version.split(".")
p2_numbers = p2_version.split(".")
if p1_version == p2_version:
line = f'{package_name}: {p1_version} -> {p2_version}'
if p1_numbers[0] != p2_numbers[0]:
change_major += [line]
elif p1_numbers[1] != p2_numbers[1]:
change_minor += [line]
change_patch += [line]
if change_patch:
if change_minor:
print("\n[Minor change]")
if change_major:
print("\n[Major change]")
if removed:
for package_name in removed:
p1_version = _remove_equal(packages_1.get(package_name, {}).get(VERSION, "[None]"))
print(f'{package_name}: {p1_version}')
if added:
for package_name in added:
p2_version = _remove_equal(packages_2.get(package_name, {}).get(VERSION, "[None]"))
print(f'{package_name}: {p2_version}')
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment