Skip to content

Commit

Permalink
Best print and compare buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekmo committed Jan 16, 2017
1 parent 892ca17 commit 8913ab1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ ENV/

# Rope project settings
.ropeproject

.directory
70 changes: 51 additions & 19 deletions cmpreqs/compare_requirements.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

import subprocess
from io import StringIO

def get_version(line):
if 'git+' in line or 'hg+' in line:
Expand All @@ -19,9 +20,9 @@ def get_package_name(line):
return line.split('==')[0]


def get_requirements(file):
def get_requirements(indata):
requirements = {}
lines = open(file).readlines()
lines = indata.readlines()
for line in lines:
if line.startswith('#'):
continue
Expand All @@ -32,25 +33,56 @@ def get_requirements(file):


def req_not_in(requirements1, requirements2):
return set(requirements1) - set(requirements2)
return {key: requirements1[key] for key in list(set(requirements1) - set(requirements2))}


def print_columns(rows, titles=False):
rows = [titles] + rows if titles else rows
widths = [max(map(len, map(str, col))) for col in zip(*rows)]
if titles:
rows = [rows[0]] + [['-' * width for width in widths]] + rows[1:]
for row in rows:
print(" ".join((str(val).ljust(width) for val, width in zip(row, widths))))


def compare(file1, file2):
requirements1 = get_requirements(file1)
requirements2 = get_requirements(file2)
print('{} <> {}'.format(file1, file2))
for name, version in requirements1.items():
if not name in requirements2:
continue
version2 = requirements2[name]
print('{}: {} {} {}'.format(name, version,'==' if version == version2 else '!=', version2))
not_in = {file1: [requirements2, requirements1], file2: [requirements1, requirements2]}
def get_input_title(indata, index=None):
return getattr(indata, 'name', 'Input {}'.format(index or '?'))


def print_title(name):
print()
print(name)
print('=' * len(name))


def compare(input1, input2):
requirements1 = get_requirements(input1)
requirements2 = get_requirements(input2)
title1, title2 = get_input_title(input1, 1), get_input_title(input2, 2)
reqs = {name: (version, requirements2[name]) for name, version
in requirements1.items() if name in requirements2}
print_title('Different dependencies')
print_columns([[name, ver[0], ver[1]] for name, ver in reqs.items()
if ver[0] != ver[1]], ['Name', title1, title2])
print_title('Equal dependencies')
print_columns([[name, ver[0]] for name, ver in reqs.items()
if ver[0] == ver[1]], ['Name', 'Version'])
not_in = {title2: [requirements2, requirements1], title1: [requirements1, requirements2]}
for file, reqs in not_in.items():
print('\n')
for req in req_not_in(*reqs):
print('{} not present in file {}'.format(req, file))

print_title('Only available on {}'.format(file))
print_columns([[name, ver] for name, ver in req_not_in(*reqs).items()], ['Name', 'Version'])


def read_freeze():
return StringIO(subprocess.check_output(['pip', 'freeze']).decode('utf-8'))


def read_pipdeptree():
data = subprocess.check_output(['pipdeptree']).decode('utf-8')
data = [x for x in data.split('\n') if x and not x[0] in ['-', ' ', '*']]
return StringIO('\n'.join(data))


if __name__ == '__main__':
import sys
compare(sys.argv[1], sys.argv[2])
compare(open(sys.argv[1]), open(sys.argv[2]))
23 changes: 23 additions & 0 deletions scripts/cmpreqs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
"""Compare requirements files created by Pip (Python)
"""
from cmpreqs.compare_requirements import compare, read_freeze, read_pipdeptree

if __name__ == '__main__':
import sys
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('req1', nargs='?', help='First requirement file',
default='requirements.txt')
parser.add_argument('req2', nargs='?',
help='Second requirement file. By default, uses pip freeze.')
parser.add_argument('--pipdeptree', action='store_true')
args = parser.parse_args()
if args.req2:
req2 = open(args.req2)
elif args.pipdeptree:
req2 = read_pipdeptree()
else:
req2 = read_freeze()
compare(open(args.req1), req2)

0 comments on commit 8913ab1

Please sign in to comment.