2017-08-17 18:13:50 +03:00
|
|
|
# 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
|
|
|
|
|
2017-09-18 19:43:03 +03:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2017-08-17 18:13:50 +03:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
from .templates import all_templates
|
|
|
|
|
|
|
|
|
2017-10-27 18:06:24 +03:00
|
|
|
COMMON_ARGUMENT_GROUPS = {
|
|
|
|
'push': [
|
2017-09-18 19:43:03 +03:00
|
|
|
[['-m', '--message'],
|
|
|
|
{'const': 'editor',
|
|
|
|
'default': '{msg}',
|
|
|
|
'nargs': '?',
|
|
|
|
'help': 'Use the specified commit message, or create it in your '
|
|
|
|
'$EDITOR if blank. Defaults to computed message.',
|
|
|
|
}],
|
2017-08-21 20:14:31 +03:00
|
|
|
[['--no-push'],
|
|
|
|
{'dest': 'push',
|
|
|
|
'action': 'store_false',
|
|
|
|
'help': 'Do not push to try as a result of running this command (if '
|
|
|
|
'specified this command will only print calculated try '
|
|
|
|
'syntax and selection info).',
|
|
|
|
}],
|
2017-10-27 18:06:24 +03:00
|
|
|
[['--closed-tree'],
|
|
|
|
{'action': 'store_true',
|
|
|
|
'default': False,
|
|
|
|
'help': 'Push despite a closed try tree',
|
|
|
|
}],
|
|
|
|
],
|
|
|
|
'preset': [
|
2017-08-17 18:14:07 +03:00
|
|
|
[['--save'],
|
|
|
|
{'default': None,
|
|
|
|
'help': 'Save selection for future use with --preset.',
|
|
|
|
}],
|
|
|
|
[['--preset'],
|
|
|
|
{'default': None,
|
|
|
|
'help': 'Load a saved selection.',
|
|
|
|
}],
|
|
|
|
[['--list-presets'],
|
2017-10-27 21:58:34 +03:00
|
|
|
{'action': 'store_const',
|
|
|
|
'const': 'list_presets',
|
|
|
|
'dest': 'mod_presets',
|
|
|
|
'default': None,
|
2017-08-17 18:14:07 +03:00
|
|
|
'help': 'List available preset selections.',
|
|
|
|
}],
|
2017-10-27 21:58:34 +03:00
|
|
|
[['--edit-presets'],
|
|
|
|
{'action': 'store_const',
|
|
|
|
'const': 'edit_presets',
|
|
|
|
'dest': 'mod_presets',
|
|
|
|
'default': None,
|
|
|
|
'help': 'Edit the preset file.',
|
|
|
|
}],
|
2017-10-27 18:06:24 +03:00
|
|
|
],
|
2017-11-02 23:03:38 +03:00
|
|
|
'task': [
|
|
|
|
[['--full'],
|
|
|
|
{'action': 'store_true',
|
|
|
|
'default': False,
|
|
|
|
'help': "Use the full set of tasks as input to fzf (instead of "
|
|
|
|
"target tasks).",
|
|
|
|
}],
|
|
|
|
[['-p', '--parameters'],
|
|
|
|
{'default': None,
|
|
|
|
'help': "Use the given parameters.yml to generate tasks, "
|
|
|
|
"defaults to latest parameters.yml from mozilla-central",
|
|
|
|
}],
|
|
|
|
],
|
2017-10-27 18:06:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTryParser(ArgumentParser):
|
|
|
|
name = 'try'
|
|
|
|
common_groups = ['push', 'preset']
|
2017-08-17 18:13:50 +03:00
|
|
|
arguments = []
|
|
|
|
templates = []
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
ArgumentParser.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
group = self.add_argument_group("{} arguments".format(self.name))
|
|
|
|
for cli, kwargs in self.arguments:
|
|
|
|
group.add_argument(*cli, **kwargs)
|
|
|
|
|
2017-10-27 18:06:24 +03:00
|
|
|
for name in self.common_groups:
|
|
|
|
group = self.add_argument_group("{} arguments".format(name))
|
|
|
|
arguments = COMMON_ARGUMENT_GROUPS[name]
|
|
|
|
for cli, kwargs in arguments:
|
|
|
|
group.add_argument(*cli, **kwargs)
|
2017-08-17 18:13:50 +03:00
|
|
|
|
|
|
|
group = self.add_argument_group("template arguments")
|
|
|
|
self.templates = {t: all_templates[t]() for t in self.templates}
|
|
|
|
for template in self.templates.values():
|
|
|
|
template.add_arguments(group)
|
|
|
|
|
2017-09-18 19:43:03 +03:00
|
|
|
def validate(self, args):
|
2017-10-27 18:06:24 +03:00
|
|
|
if hasattr(args, 'message'):
|
|
|
|
if args.message == 'editor':
|
|
|
|
if 'EDITOR' not in os.environ:
|
|
|
|
self.error("must set the $EDITOR environment variable to use blank --message")
|
2017-09-18 19:43:03 +03:00
|
|
|
|
2017-10-27 18:06:24 +03:00
|
|
|
with tempfile.NamedTemporaryFile(mode='r') as fh:
|
|
|
|
subprocess.call([os.environ['EDITOR'], fh.name])
|
|
|
|
args.message = fh.read().strip()
|
2017-09-18 19:43:03 +03:00
|
|
|
|
2017-10-27 18:06:24 +03:00
|
|
|
if '{msg}' not in args.message:
|
|
|
|
args.message = '{}\n\n{}'.format(args.message, '{msg}')
|
2017-09-18 19:43:03 +03:00
|
|
|
|
2017-08-17 18:13:50 +03:00
|
|
|
def parse_known_args(self, *args, **kwargs):
|
|
|
|
args, remainder = ArgumentParser.parse_known_args(self, *args, **kwargs)
|
2017-09-18 19:43:03 +03:00
|
|
|
self.validate(args)
|
2017-08-17 18:13:50 +03:00
|
|
|
|
|
|
|
if self.templates:
|
|
|
|
args.templates = {}
|
2018-01-16 00:05:17 +03:00
|
|
|
for cls in self.templates.itervalues():
|
2017-08-17 18:13:50 +03:00
|
|
|
context = cls.context(**vars(args))
|
|
|
|
if context is not None:
|
2018-01-16 00:05:17 +03:00
|
|
|
args.templates.update(context)
|
2017-08-17 18:13:50 +03:00
|
|
|
|
|
|
|
return args, remainder
|