This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
/
pip-diff
executable file
·116 lines (86 loc) · 3.22 KB
/
pip-diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Usage:
pip-diff (--fresh | --stale) <reqfile1> <reqfile2> [--exclude <package>...]
pip-diff (-h | --help)
Options:
-h --help Show this screen.
--fresh List newly added packages.
--stale List removed packages.
"""
import os
from docopt import docopt
from pip.req import parse_requirements
from pip.index import PackageFinder
from pip._vendor.requests import session
requests = session()
class Requirements(object):
def __init__(self, reqfile=None):
super(Requirements, self).__init__()
self.path = reqfile
self.requirements = []
if reqfile:
self.load(reqfile)
def __repr__(self):
return '<Requirements \'{}\'>'.format(self.path)
def load(self, reqfile):
if not os.path.exists(reqfile):
raise ValueError('The given requirements file does not exist.')
finder = PackageFinder([], [], session=requests)
for requirement in parse_requirements(reqfile, finder=finder, session=requests):
if requirement.req:
if not getattr(requirement.req, 'name', None):
# Prior to pip 8.1.2 the attribute `name` did not exist.
requirement.req.name = requirement.req.project_name
self.requirements.append(requirement.req)
def diff(self, requirements, ignore_versions=False, excludes=None):
r1 = self
r2 = requirements
results = {'fresh': [], 'stale': []}
# Generate fresh packages.
other_reqs = (
[r.name for r in r1.requirements]
if ignore_versions else r1.requirements
)
for req in r2.requirements:
r = req.name if ignore_versions else req
if r not in other_reqs and r not in excludes:
results['fresh'].append(req)
# Generate stale packages.
other_reqs = (
[r.name for r in r2.requirements]
if ignore_versions else r2.requirements
)
for req in r1.requirements:
r = req.name if ignore_versions else req
if r not in other_reqs and r not in excludes:
results['stale'].append(req)
return results
def diff(r1, r2, include_fresh=False, include_stale=False, excludes=None):
include_versions = True if include_stale else False
excludes = excludes if len(excludes) else []
try:
r1 = Requirements(r1)
r2 = Requirements(r2)
except ValueError:
print('There was a problem loading the given requirements files.')
exit(os.EX_NOINPUT)
results = r1.diff(r2, ignore_versions=True, excludes=excludes)
if include_fresh:
for line in results['fresh']:
print(line.name if include_versions else line)
if include_stale:
for line in results['stale']:
print(line.name if include_versions else line)
def main():
args = docopt(__doc__, version='pip-diff')
kwargs = {
'r1': args['<reqfile1>'],
'r2': args['<reqfile2>'],
'include_fresh': args['--fresh'],
'include_stale': args['--stale'],
'excludes': args['<package>']
}
diff(**kwargs)
if __name__ == '__main__':
main()