Bug 1419512 - [tryselect] Consolidate subcommand parser retrieval in mach_commands.py, r=armenzg
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 02 Nov 2017 16:02:35 -0400
changeset 392988 a465c93f52b4f94e4f50baf6194ec45eb481051b
parent 392987 00f6b8f2e19605e0ce7e1abb8b371661bd68f9c0
child 392989 8d6fd02420f2eb8d09a1c50e060294b44c17d357
push id32947
push useraciure@mozilla.com
push dateWed, 22 Nov 2017 09:57:59 +0000
treeherdermozilla-central@5378dcb45044 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarmenzg
bugs1419512
milestone59.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 1419512 - [tryselect] Consolidate subcommand parser retrieval in mach_commands.py, r=armenzg This is a minor cleanup around finding and importing subcommand parser in |mach try|. MozReview-Commit-ID: IHRXXi5mB4G
tools/tryselect/mach_commands.py
--- a/tools/tryselect/mach_commands.py
+++ b/tools/tryselect/mach_commands.py
@@ -1,15 +1,16 @@
 # 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 argparse
+import importlib
 import os
 import sys
 
 from mach.decorators import (
     CommandProvider,
     Command,
     SettingsProvider,
     SubCommand,
@@ -22,30 +23,23 @@ No config environment detected. This mea
 detect test files in the specified paths or tags. Please run:
 
     $ mach configure
 
 and try again.
 '''.lstrip()
 
 
-def syntax_parser():
-    from tryselect.selectors.syntax import SyntaxParser
-    parser = SyntaxParser()
-    return parser
-
+class get_parser(object):
+    def __init__(self, selector):
+        self.selector = selector
 
-def fuzzy_parser():
-    from tryselect.selectors.fuzzy import FuzzyParser
-    return FuzzyParser()
-
-
-def empty_parser():
-    from tryselect.selectors.empty import EmptyParser
-    return EmptyParser()
+    def __call__(self):
+        mod = importlib.import_module('tryselect.selectors.{}'.format(self.selector))
+        return getattr(mod, '{}Parser'.format(self.selector.capitalize()))()
 
 
 def generic_parser():
     from tryselect.cli import BaseTryParser
     parser = BaseTryParser()
     parser.add_argument('argv', nargs=argparse.REMAINDER)
     return parser
 
@@ -95,17 +89,17 @@ class TrySelect(MachCommandBase):
             sub = 'syntax' if section == 'try' else section
 
         return self._mach_context.commands.dispatch(
             'try', subcommand=sub, context=self._mach_context, argv=argv, **kwargs)
 
     @SubCommand('try',
                 'fuzzy',
                 description='Select tasks on try using a fuzzy finder',
-                parser=fuzzy_parser)
+                parser=get_parser('fuzzy'))
     def try_fuzzy(self, **kwargs):
         """Select which tasks to use with fzf.
 
         This selector runs all task labels through a fuzzy finding interface.
         All selected task labels and their dependencies will be scheduled on
         try.
 
         Keyboard Shortcuts
@@ -145,33 +139,33 @@ class TrySelect(MachCommandBase):
           ^start 'exact | !ignore fuzzy end$
         """
         from tryselect.selectors.fuzzy import run_fuzzy_try
         return run_fuzzy_try(**kwargs)
 
     @SubCommand('try',
                 'empty',
                 description='Push to try without scheduling any tasks.',
-                parser=empty_parser)
+                parser=get_parser('empty'))
     def try_empty(self, **kwargs):
         """Push to try, running no builds or tests
 
         This selector does not prompt you to run anything, it just pushes
         your patches to try, running no builds or tests by default. After
         the push finishes, you can manually add desired jobs to your push
         via Treeherder's Add New Jobs feature, located in the per-push
         menu.
         """
         from tryselect.selectors.empty import run_empty_try
         return run_empty_try(**kwargs)
 
     @SubCommand('try',
                 'syntax',
                 description='Select tasks on try using try syntax',
-                parser=syntax_parser)
+                parser=get_parser('syntax'))
     def try_syntax(self, **kwargs):
         """Push the current tree to try, with the specified syntax.
 
         Build options, platforms and regression tests may be selected
         using the usual try options (-b, -p and -u respectively). In
         addition, tests in a given directory may be automatically
         selected by passing that directory as a positional argument to the
         command. For example: