Bug 1400503 - [tryselect] Add an option to open the presets file in an editor, r=armenzg
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Fri, 27 Oct 2017 14:58:34 -0400
changeset 389083 301666d1d339f3c3a22ebce3c0c640e076d779c4
parent 389082 7741abf30b9bd8c81233edefee5e1217d93bdc9f
child 389084 dd0f265a130098cda83a0c25c7617d2365b28f2d
child 389170 c78fdb7724524585387b43ef958a17408827dcfa
push id32777
push userarchaeopteryx@coole-files.de
push dateMon, 30 Oct 2017 22:44:45 +0000
treeherdermozilla-central@dd0f265a1300 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarmenzg
bugs1400503
milestone58.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 1400503 - [tryselect] Add an option to open the presets file in an editor, r=armenzg MozReview-Commit-ID: 2jwdAaNWHH7
tools/tryselect/cli.py
tools/tryselect/mach_commands.py
tools/tryselect/preset.py
tools/tryselect/selectors/fuzzy.py
tools/tryselect/selectors/syntax.py
tools/tryselect/test/test_preset.t
--- a/tools/tryselect/cli.py
+++ b/tools/tryselect/cli.py
@@ -39,20 +39,29 @@ COMMON_ARGUMENT_GROUPS = {
          {'default': None,
           'help': 'Save selection for future use with --preset.',
           }],
         [['--preset'],
          {'default': None,
           'help': 'Load a saved selection.',
           }],
         [['--list-presets'],
-         {'action': 'store_true',
-          'default': False,
+         {'action': 'store_const',
+          'const': 'list_presets',
+          'dest': 'mod_presets',
+          'default': None,
           'help': 'List available preset selections.',
           }],
+        [['--edit-presets'],
+         {'action': 'store_const',
+          'const': 'edit_presets',
+          'dest': 'mod_presets',
+          'default': None,
+          'help': 'Edit the preset file.',
+          }],
     ],
 }
 
 
 class BaseTryParser(ArgumentParser):
     name = 'try'
     common_groups = ['push', 'preset']
     arguments = []
--- a/tools/tryselect/mach_commands.py
+++ b/tools/tryselect/mach_commands.py
@@ -78,18 +78,18 @@ class TrySelect(MachCommandBase):
         that provides its own set of command line arguments and are
         listed below.
 
         If no subcommand is specified, the `syntax` selector is run by
         default. Run |mach try syntax --help| for more information on
         scheduling with the `syntax` selector.
         """
         from tryselect import preset
-        if kwargs['list_presets']:
-            preset.list_presets()
+        if kwargs['mod_presets']:
+            getattr(preset, kwargs['mod_presets'])()
             return
 
         # We do special handling of presets here so that `./mach try --preset foo`
         # works no matter what subcommand 'foo' was saved with.
         sub = self._mach_context.settings['try']['default']
         if kwargs['preset']:
             _, section = preset.load(kwargs['preset'])
             sub = 'syntax' if section == 'try' else section
--- a/tools/tryselect/preset.py
+++ b/tools/tryselect/preset.py
@@ -1,42 +1,50 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import ConfigParser
 import os
+import subprocess
 
 from mozboot.util import get_state_dir
 
 
 CONFIG_PATH = os.path.join(get_state_dir()[0], "autotry.ini")
 
 
 def list_presets(section=None):
     config = ConfigParser.RawConfigParser()
 
     data = []
     if config.read([CONFIG_PATH]):
         sections = [section] if section else config.sections()
         for s in sections:
             try:
                 data.extend(config.items(s))
-            except ConfigParser.NoOptionError:
+            except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
                 pass
 
     if not data:
         print("No presets found")
 
     for name, value in data:
         print("%s: %s" % (name, value))
 
 
+def edit_presets(section=None):
+    if 'EDITOR' not in os.environ:
+        print("error: must set the $EDITOR environment variable to use --edit-presets")
+        return
+    subprocess.call([os.environ['EDITOR'], CONFIG_PATH])
+
+
 def load(name, section=None):
     config = ConfigParser.RawConfigParser()
     if not config.read([CONFIG_PATH]):
         return
 
     sections = [section] if section else config.sections()
     for s in sections:
         try:
--- a/tools/tryselect/selectors/fuzzy.py
+++ b/tools/tryselect/selectors/fuzzy.py
@@ -193,20 +193,20 @@ def format_header():
     shortcuts = []
     for action, key in sorted(fzf_header_shortcuts.iteritems()):
         shortcuts.append('{t.white}{action}{t.normal}: {t.yellow}<{key}>{t.normal}'.format(
                          t=terminal, action=action, key=key))
     return FZF_HEADER.format(shortcuts=', '.join(shortcuts), t=terminal)
 
 
 def run_fuzzy_try(update=False, query=None, templates=None, full=False, parameters=None,
-                  save=False, preset=None, list_presets=False, push=True, message='{msg}',
+                  save=False, preset=None, mod_presets=False, push=True, message='{msg}',
                   **kwargs):
-    if list_presets:
-        return pset.list_presets(section='fuzzy')
+    if mod_presets:
+        return getattr(pset, mod_presets)(section='fuzzy')
 
     fzf = fzf_bootstrap(update)
 
     if not fzf:
         print(FZF_NOT_FOUND)
         return
 
     vcs = VCSHelper.create()
--- a/tools/tryselect/selectors/syntax.py
+++ b/tools/tryselect/selectors/syntax.py
@@ -569,18 +569,18 @@ class AutoTry(object):
 
         extra_values = {k['dest'] for k in SyntaxParser.pass_through_arguments.values()}
         extra_args = {k: v for k, v in kwargs.items()
                       if k in extra_values and v}
 
         return kwargs["builds"], platforms, tests, talos, jobs, paths, tags, extra_args
 
     def run(self, **kwargs):
-        if kwargs["list_presets"]:
-            preset.list_presets(section='try')
+        if kwargs["mod_presets"]:
+            getattr(preset, kwargs["mod_presets"])(section='try')
             sys.exit()
 
         if kwargs["preset"]:
             value = preset.load(kwargs["preset"], section='try')[0]
             defaults = vars(SyntaxParser().parse_args(self.split_try_string(value)))
 
             if defaults is None:
                 print("No saved configuration called %s found in autotry.ini" % kwargs["preset"],
--- a/tools/tryselect/test/test_preset.t
+++ b/tools/tryselect/test/test_preset.t
@@ -4,50 +4,69 @@
 Test preset with no subcommand
 
   $ ./mach try $testargs --save foo -b do -p linux -u mochitests -t none --tag foo
   Commit message:
   try: -b do -p linux -u mochitests -t none --tag foo
   
   Pushed via `mach try syntax`
   preset saved, run with: --preset=foo
+
   $ ./mach try $testargs --preset foo
   Commit message:
   try: -b do -p linux -u mochitests -t none --tag foo
   
   Pushed via `mach try syntax`
+
   $ ./mach try syntax $testargs --preset foo
   Commit message:
   try: -b do -p linux -u mochitests -t none --tag foo
   
   Pushed via `mach try syntax`
+
   $ ./mach try $testargs --list-presets
   foo: -b do -p linux -u mochitests -t none --tag foo
+  $ unset EDITOR
+  $ ./mach try $testargs --edit-presets
+  error: must set the $EDITOR environment variable to use --edit-presets
+  $ export EDITOR=cat
+  $ ./mach try $testargs --edit-presets
+  [try]
+  foo = -b do -p linux -u mochitests -t none --tag foo
+  
 
 Test preset with syntax subcommand
 
   $ ./mach try syntax $testargs --save bar -b do -p win32 -u none -t all --tag bar
   Commit message:
   try: -b do -p win32 -u none -t all --tag bar
   
   Pushed via `mach try syntax`
   preset saved, run with: --preset=bar
+
   $ ./mach try syntax $testargs --preset bar
   Commit message:
   try: -b do -p win32 -u none -t all --tag bar
   
   Pushed via `mach try syntax`
+
   $ ./mach try $testargs --preset bar
   Commit message:
   try: -b do -p win32 -u none -t all --tag bar
   
   Pushed via `mach try syntax`
+
   $ ./mach try syntax $testargs --list-presets
   foo: -b do -p linux -u mochitests -t none --tag foo
   bar: -b do -p win32 -u none -t all --tag bar
+  $ ./mach try syntax $testargs --edit-presets
+  [try]
+  foo = -b do -p linux -u mochitests -t none --tag foo
+  bar = -b do -p win32 -u none -t all --tag bar
+  
 
 Test preset with fuzzy subcommand
 
   $ ./mach try fuzzy $testargs --save baz -q "'baz"
   preset saved, run with: --preset=baz
   Commit message:
   Fuzzy with query: 'baz
   
@@ -80,8 +99,19 @@ Test preset with fuzzy subcommand
   {
     "tasks":[
       "build-baz"
     ]
   }
   
   $ ./mach try fuzzy $testargs --list-presets
   baz: 'baz
+
+  $ ./mach try fuzzy $testargs --list-presets
+  baz: 'baz
+  $ ./mach try fuzzy $testargs --edit-presets
+  [try]
+  foo = -b do -p linux -u mochitests -t none --tag foo
+  bar = -b do -p win32 -u none -t all --tag bar
+  
+  [fuzzy]
+  baz = 'baz
+