Bug 1460912 - [testing/profiles] Add a ./profile rm subcommand for removing prefs from a profile r=rwood
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Mon, 14 May 2018 21:43:47 -0400
changeset 418777 631008a3bf2973db48cf70502751795ac1065b37
parent 418776 794cca0138c45b0882aa06593c41e83fff5e44b4
child 418778 24fc34c2bb343e7ef534936cebea310a37a2b60f
push id103398
push userdluca@mozilla.com
push dateFri, 18 May 2018 10:00:25 +0000
treeherdermozilla-inbound@54db0dcb08f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrwood
bugs1460912
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1460912 - [testing/profiles] Add a ./profile rm subcommand for removing prefs from a profile r=rwood 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
testing/profiles/profile
--- 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())