Bug 1419512 - [tryselect] Consolidate subcommand parser retrieval in mach_commands.py, r?armenzg draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 02 Nov 2017 16:02:35 -0400
changeset 701466 619841b575f10b683ab9a11f49cc91e511c4ae24
parent 701250 72ee4800d4156931c89b58bd807af4a3083702bb
child 741168 4be22b25890b9d2113e458f2ac36bf3cdfe0fb49
push id90165
push userahalberstadt@mozilla.com
push dateTue, 21 Nov 2017 19:10:08 +0000
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
--- 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 (
@@ -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.
-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)
                 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
         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)
                 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
         from tryselect.selectors.empty import run_empty_try
         return run_empty_try(**kwargs)
                 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: