--- a/testing/profiles/profile
+++ b/testing/profiles/profile
@@ -108,42 +108,48 @@ def read(key):
a suite as defined in suites.json.
"""
prefs = {}
for profile in get_profiles(key):
prefs.update(read_prefs(profile))
return prefs
-def format_diff(diff, fmt):
+def format_diff(diff, fmt, limit_key):
"""Format a diff."""
+ indent = ' '
+ if limit_key:
+ diff = {limit_key: diff[limit_key]}
+ indent = ''
+
if fmt == 'json':
print(json.dumps(diff, sort_keys=True, indent=2))
return 0
lines = []
for key, prefs in sorted(diff.items()):
- lines.append("{}:".format(key))
+ if not limit_key:
+ lines.append("{}:".format(key))
for pref, value in sorted(prefs.items()):
context = {'pref': pref, 'value': repr(value)}
if isinstance(value, list):
context['value_a'] = repr(value[0])
context['value_b'] = repr(value[1])
text = FORMAT_STRINGS[fmt][1].format(**context)
else:
text = FORMAT_STRINGS[fmt][0].format(**context)
- lines.append(' {}'.format(text))
+ lines.append('{}{}'.format(indent, text))
lines.append('')
print('\n'.join(lines).strip())
-def diff(a, b, fmt):
+def diff(a, b, fmt, limit_key):
"""Diff two profiles or suites.
:param a: The first profile or suite name.
:param b: The second profile or suite name.
"""
prefs_a = read(a)
prefs_b = read(b)
res = jsondiff.diff(prefs_a, prefs_b, syntax='symmetric')
@@ -164,17 +170,17 @@ def diff(a, b, fmt):
symbols = [(k, v) for k, v in res.items() if isinstance(k, jsondiff.Symbol)]
results['insert'] = {k: v for sym, pref in symbols for k, v in pref.items()
if sym.label == 'insert'}
results['delete'] = {k: v for sym, pref in symbols for k, v in pref.items()
if sym.label == 'delete'}
same = set(prefs_a.keys()) - set(chain(*results.values()))
results['same'] = {k: v for k, v in prefs_a.items() if k in same}
- return format_diff(results, fmt)
+ return format_diff(results, fmt, limit_key)
def sort_file(path):
"""Sort the given pref file alphabetically, preserving preceding comments
that start with '//'.
:param path: Path to the preference file to sort.
"""
@@ -237,16 +243,19 @@ def cli(args=sys.argv[1:]):
diff_parser = subparsers.add_parser('diff')
diff_parser.add_argument('a', metavar='A',
help="Path to the first profile or suite name to diff.")
diff_parser.add_argument('b', metavar='B',
help="Path to the second profile or suite name to diff.")
diff_parser.add_argument('-f', '--format', dest='fmt', default='pretty',
choices=['pretty', 'json', 'names'],
help="Format to dump diff in (default: pretty)")
+ diff_parser.add_argument('-k', '--limit-key', default=None,
+ choices=['change', 'delete', 'insert', 'same'],
+ help="Restrict diff to the specified key.")
diff_parser.set_defaults(func=diff)
sort_parser = subparsers.add_parser('sort')
sort_parser.add_argument('profile', help="Path to profile to sort preferences.")
sort_parser.set_defaults(func=sort)
show_parser = subparsers.add_parser('show')
show_parser.add_argument('suite', help="Name of suite to show arguments for.")