/
diff.py
239 lines (191 loc) · 8.07 KB
/
diff.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil; coding: utf-8 -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the reproman package for the
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Report if a specification satisfies the requirements in another specification
"""
import sys
import time
from .base import Interface
from ..support.constraints import EnsureStr
from ..support.exceptions import InsufficientArgumentsError
from ..support.param import Parameter
from reproman.formats.reproman import RepromanProvenance
from ..distributions.debian import DebianDistribution
from ..distributions.conda import CondaDistribution
from ..distributions.vcs import GitDistribution, SVNDistribution
__docformat__ = 'restructuredtext'
from logging import getLogger
lgr = getLogger('reproman.api.retrace')
class MultipleDistributionsError(Exception):
"""Multiple distributions of a given type found"""
def __init__(self, cls):
self.cls = cls
def _make_plural(s):
"""Poor man 'plural' version for now"""
if s.endswith('repository'):
return s.replace('repository', 'repositories')
else:
return s + 's'
class Diff(Interface):
"""Report if a specification satisfies the requirements in another
specification
Examples
--------
$ reproman diff environment1.yml environment2.yml
"""
_params_ = dict(
prov1=Parameter(
doc="ReproMan provenance file",
metavar='prov1',
constraints=EnsureStr()),
prov2=Parameter(
metavar="prov2",
doc="ReproMan provenance file",
constraints=EnsureStr()),
satisfies=Parameter(
args=("--satisfies", "-s"),
doc="Make sure the first environment satisfies the needs of the second environment",
action="store_true")
)
@staticmethod
def __call__(prov1, prov2, satisfies):
env_1 = RepromanProvenance(prov1).get_environment()
env_2 = RepromanProvenance(prov2).get_environment()
if satisfies:
return Diff.satisfies(env_1, env_2)
return Diff.diff(env_1, env_2)
@staticmethod
def diff(env_1, env_2):
result = {'method': 'diff', 'distributions': []}
# distribution type -> package type string
supported_distributions = {
DebianDistribution: 'Debian package',
CondaDistribution: 'Conda package',
GitDistribution: 'Git repository',
SVNDistribution: 'SVN repository'
}
env_1_dist_types = { d.__class__ for d in env_1.distributions }
env_2_dist_types = { d.__class__ for d in env_2.distributions }
all_dist_types = env_1_dist_types.union(env_2_dist_types)
for dist_type in all_dist_types:
if dist_type not in supported_distributions:
msg = 'diff doesn\'t know how to handle %s' % str(dist_type)
raise ValueError(msg)
dist_res = {'pkg_type': supported_distributions[dist_type],
'pkg_diffs': []}
dist_1 = env_1.get_distribution(dist_type)
if dist_1:
pkgs_1 = {p._diff_cmp_id: p for p in dist_1.packages}
else:
pkgs_1 = {}
dist_2 = env_2.get_distribution(dist_type)
if dist_2:
pkgs_2 = {p._diff_cmp_id: p for p in dist_2.packages}
else:
pkgs_2 = {}
dist_res['pkgs_1'] = pkgs_1
dist_res['pkgs_2'] = pkgs_2
pkgs_1_s = set(pkgs_1)
pkgs_2_s = set(pkgs_2)
dist_res['pkgs_only_1'] = pkgs_1_s - pkgs_2_s
dist_res['pkgs_only_2'] = pkgs_2_s - pkgs_1_s
for cmp_key in pkgs_1_s.intersection(pkgs_2_s):
package_1 = pkgs_1[cmp_key]
package_2 = pkgs_2[cmp_key]
if package_1._diff_vals != package_2._diff_vals:
dist_res['pkg_diffs'].append((package_1, package_2))
result['distributions'].append(dist_res)
files1 = set(env_1.files)
files2 = set(env_2.files)
result['files_1_only'] = files1 - files2
result['files_2_only'] = files2 - files1
return result
@staticmethod
def satisfies(env_1, env_2):
result = {'method': 'satisfies', 'distributions': []}
# distribution type -> package type string
supported_distributions = {
DebianDistribution: 'Debian package'
}
env_1_dist_types = { d.__class__ for d in env_1.distributions }
env_2_dist_types = { d.__class__ for d in env_2.distributions }
all_dist_types = env_1_dist_types.union(env_2_dist_types)
for dist_type in all_dist_types:
if dist_type not in supported_distributions:
msg = 'diff --satisfies doesn\'t know how to handle %s' % str(dist_type)
raise ValueError(msg)
unsatisfied_packages = []
dist_1 = env_1.get_distribution(dist_type)
dist_2 = env_2.get_distribution(dist_type)
if not dist_2:
continue
for pkg in dist_2.packages:
if not pkg.compare(dist_1, mode='satisfied_by'):
unsatisfied_packages.append(pkg)
if unsatisfied_packages:
dist_res = {'pkg_type': supported_distributions[dist_type],
'packages': unsatisfied_packages}
result['distributions'].append(dist_res)
files1 = set(env_1.files)
files2 = set(env_2.files)
result['files'] = files2 - files1
return result
@staticmethod
def result_renderer_cmdline(result):
if result['method'] == 'diff':
return Diff.render_cmdline_diff(result)
return Diff.render_cmdline_satisfies(result)
@staticmethod
def render_cmdline_diff(result):
status = 0
for dist_res in result['distributions']:
if dist_res['pkgs_only_1'] or dist_res['pkgs_only_2']:
print(_make_plural(dist_res['pkg_type']) + ':')
if dist_res['pkgs_only_1']:
for cmp_key in sorted(dist_res['pkgs_only_1']):
package = dist_res['pkgs_1'][cmp_key]
print('< %s' % package.diff_identity_string)
status = 3
if dist_res['pkgs_only_1'] and dist_res['pkgs_only_2']:
print('---')
if dist_res['pkgs_only_2']:
for cmp_key in sorted(dist_res['pkgs_only_2']):
package = dist_res['pkgs_2'][cmp_key]
print('> %s' % package.diff_identity_string)
status = 3
for (package_1, package_2) in dist_res['pkg_diffs']:
print('%s %s:' % (dist_res['pkg_type'],
" ".join(package_1._diff_cmp_id)))
print('< %s' % package_1.diff_subidentity_string)
print('---')
print('> %s' % package_2.diff_subidentity_string)
status = 3
if result['files_1_only'] or result['files_2_only']:
print('Files:')
for fname in result['files_1_only']:
print('< %s' % fname)
if result['files_1_only'] and result['files_2_only']:
print('---')
for fname in result['files_2_only']:
print('> %s' % fname)
status = 3
return status
@staticmethod
def render_cmdline_satisfies(result):
status = 0
for dist_res in result['distributions']:
print(_make_plural(dist_res['pkg_type']) + ':')
for package in dist_res['packages']:
print('> %s' % package.identity_string)
status = 3
if result['files']:
print('Files:')
for fname in result['files']:
print('> %s' % fname)
status = 3
return status