Bug 1460912 - [testing/profiles] Add a ./profile rm subcommand for removing prefs from a profile
Usage is:
./profile rm <profile> [<file>]
The file is a list of prefs, one per line, to remove. The file defaults to stdin, so the culmination
of the last three commits is the ability to do:
./profile diff reftest common -f names -k same | ./profile rm reftest
The above command will remove from the reftest profile, the prefs that are identical in both
reftest and common. This method is quicker and less error prone than doing it manually (which
was how I was doing this previously).
MozReview-Commit-ID: Je0JjFXoora
--- a/testing/profiles/profile
+++ b/testing/profiles/profile
@@ -173,22 +173,17 @@ def diff(a, b, fmt, limit_key):
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, 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.
- """
+def read_with_comments(path):
with open(path, 'r') as fh:
lines = fh.readlines()
result = []
buf = []
for line in lines:
line = line.strip()
if not line:
@@ -199,22 +194,31 @@ def sort_file(path):
continue
if buf:
result.append(buf + [line])
buf = []
continue
result.append([line])
+ return result
+
+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.
+ """
+ result = read_with_comments(path)
result = sorted(result, key=lambda x: x[-1])
result = chain(*result)
with open(path, 'w') as fh:
- fh.write('\n'.join(result))
+ fh.write('\n'.join(result) + '\n')
def sort(profile):
"""Sort all prefs in the given profile alphabetically. This will preserve
comments on preceding lines.
:param profile: The name of the profile to sort.
"""
@@ -231,16 +235,38 @@ def show(suite):
:param suite: The name of the suite to show preferences for. This must
be a key in suites.json.
"""
for k, v in sorted(read(suite).items()):
print("{}: {}".format(k, repr(v)))
+def rm(profile, pref_file):
+ if pref_file == '-':
+ lines = sys.stdin.readlines()
+ else:
+ with open(pref_file, 'r') as fh:
+ lines = fh.readlines()
+
+ lines = [l.strip() for l in lines if l.strip()]
+ if not lines:
+ return
+
+ def filter_line(content):
+ return not any(line in content[-1] for line in lines)
+
+ path = os.path.join(here, profile, 'user.js')
+ contents = read_with_comments(path)
+ contents = filter(filter_line, contents)
+ contents = chain(*contents)
+ with open(path, 'w') as fh:
+ fh.write('\n'.join(contents))
+
+
def cli(args=sys.argv[1:]):
parser = ArgumentParser()
subparsers = parser.add_subparsers()
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',
@@ -256,15 +282,21 @@ def cli(args=sys.argv[1:]):
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.")
show_parser.set_defaults(func=show)
+ rm_parser = subparsers.add_parser('rm')
+ rm_parser.add_argument('profile', help="Name of the profile to remove prefs from.")
+ rm_parser.add_argument('--pref-file', default='-', help="File containing a list of pref "
+ "substrings to delete (default: stdin)")
+ rm_parser.set_defaults(func=rm)
+
args = vars(parser.parse_args(args))
func = args.pop('func')
func(**args)
if __name__ == '__main__':
sys.exit(cli())