Ejemplo n.º 1
0
class TestOptionParser(optparse.OptionParser, object):
  """Option parser with predefined test options."""

  __long__ = None

  standard_option_list = [
      optparse.Option('--verbosity', default=0, type='int',
                      help='Verbosity level from least verbose, 0, to most, '
                           '3 [default=%default].'),
      optparse.Option('--seed', default=42, type='int',
                      help='Seed to use for random number generators '
                           '[default: %default].'),
      optparse.Option('--short', action='store_false', dest='long',
                      default=True, help='Run only short tests.'),
      optparse.Option('--long', action='store_true', dest='long',
                      default=False, help='Run all short and long tests.'),
      optparse.Option('--installDir', dest='installDir',
                      help='Installation directory used for this test run.'),
      ]

  def parse_args(self, args=None, values=None, consumeArgv=True):
    options, remainingArgs = super(TestOptionParser, self).parse_args(args, values)
    TestOptionParser.__long__ = options.long
    if consumeArgv:
      sys.argv = [sys.argv[0]] + remainingArgs
    return options, remainingArgs
    def setformats(self, formats, usetemplates):
        """Sets the format options using the given format dictionary.

        @type formats: Dictionary
        @param formats: The dictionary I{keys} should be:
            - single strings (or 1-tuples) containing an input format (if not usetemplates)
            - tuples containing an input format and template format (if usetemplates)
            - formats can be None to indicate what to do with standard input
        The dictionary I{values} should be tuples of outputformat (string) and processor method.
        
        """

        inputformats = []
        outputformats = []
        templateformats = []
        self.outputoptions = {}
        self.usetemplates = usetemplates
        for formatgroup, outputoptions in formats.iteritems():
            if isinstance(formatgroup, (str, unicode)) or formatgroup is None:
                formatgroup = (formatgroup, )
            if not isinstance(formatgroup, tuple):
                raise ValueError("formatgroups must be tuples or None/str/unicode")
            if len(formatgroup) < 1 or len(formatgroup) > 2:
                raise ValueError("formatgroups must be tuples of length 1 or 2")
            if len(formatgroup) == 1:
                formatgroup += (None, )
            inputformat, templateformat = formatgroup
            if not isinstance(outputoptions, tuple) or len(outputoptions) != 2:
                raise ValueError("output options must be tuples of length 2")
            outputformat, processor = outputoptions
            if not inputformat in inputformats:
                inputformats.append(inputformat)
            if not outputformat in outputformats:
                outputformats.append(outputformat)
            if not templateformat in templateformats:
                templateformats.append(templateformat)
            self.outputoptions[(inputformat, templateformat)] = (outputformat, processor)
        self.inputformats = inputformats
        inputformathelp = self.getformathelp(inputformats)
        inputoption = optparse.Option("-i", "--input", dest="input", default=None, metavar="INPUT",
                help="read from INPUT in %s" % (inputformathelp))
        inputoption.optionalswitch = True
        inputoption.required = True
        self.define_option(inputoption)
        excludeoption = optparse.Option("-x", "--exclude", dest="exclude", action="append",
                type="string", default=["CVS", ".svn", "_darcs", ".git", ".hg", ".bzr"], metavar="EXCLUDE",
                help="exclude names matching EXCLUDE from input paths")
        self.define_option(excludeoption)
        outputformathelp = self.getformathelp(outputformats)
        outputoption = optparse.Option("-o", "--output", dest="output", default=None, metavar="OUTPUT",
                help="write to OUTPUT in %s" % (outputformathelp))
        outputoption.optionalswitch = True
        outputoption.required = True
        self.define_option(outputoption)
        if self.usetemplates:
            self.templateformats = templateformats
            templateformathelp = self.getformathelp(self.templateformats)
            templateoption = optparse.Option("-t", "--template", dest="template", default=None, metavar="TEMPLATE",
                help="read from TEMPLATE in %s" % (templateformathelp))
            self.define_option(templateoption)
Ejemplo n.º 3
0
 def _add_option(self, option):
     # from libbe.ui.command_line.CmdOptionParser._add_option
     option.validate()
     long_opt = '--%s' % option.name
     if option.short_name != None:
         short_opt = '-%s' % option.short_name
     assert '_' not in option.name, \
         'Non-reconstructable option name %s' % option.name
     kwargs = {'dest': option.name.replace('-', '_'), 'help': option.help}
     if option.arg == None or option.arg.type == 'bool':
         kwargs['action'] = 'store_true'
         kwargs['metavar'] = None
         kwargs['default'] = False
     else:
         kwargs['type'] = option.arg.type
         kwargs['action'] = 'store'
         kwargs['metavar'] = option.arg.metavar
         kwargs['default'] = option.arg.default
     if option.short_name != None:
         opt = optparse.Option(short_opt, long_opt, **kwargs)
     else:
         opt = optparse.Option(long_opt, **kwargs)
     #option.takes_value = lambda : option.arg != None
     opt._option = option
     self._command_opts.append(opt)
     self.add_option(opt)
 def _add_option(self, option):
     option.validate()
     self._option_by_name[option.name] = option
     long_opt = '--%s' % option.name
     if option.short_name != None:
         short_opt = '-%s' % option.short_name
     assert '_' not in option.name, \
         'Non-reconstructable option name %s' % option.name
     kwargs = {'dest': option.name.replace('-', '_'), 'help': option.help}
     if option.arg == None:  # a callback option
         kwargs['action'] = 'callback'
         kwargs['callback'] = self.callback
     elif option.arg.type == 'bool':
         kwargs['action'] = 'store_true'
         kwargs['metavar'] = None
         kwargs['default'] = False
     else:
         kwargs['type'] = option.arg.type
         kwargs['action'] = 'store'
         kwargs['metavar'] = option.arg.metavar
         kwargs['default'] = option.arg.default
     if option.short_name != None:
         opt = optparse.Option(short_opt, long_opt, **kwargs)
     else:
         opt = optparse.Option(long_opt, **kwargs)
     opt._option = option
     self.add_option(opt)
Ejemplo n.º 5
0
    def __init__(self,
                 usage=None,
                 option_list=None,
                 option_class=Option,
                 version=None,
                 conflict_handler="error",
                 description=None,
                 formatter=None,
                 add_help_option=True,
                 prog=None,
                 command_container=None,
                 default_command="help",
                 add_username_password_options=False):

        usage = usage or "%prog <command> [args] [--help]"
        self.container = command_container
        self.default_command = default_command
        self.command = None
        formatter = formatter or optparse.IndentedHelpFormatter(
            max_help_position=33)

        optparse.OptionParser.__init__(self, usage, option_list, option_class,
                                       version, conflict_handler, description,
                                       formatter, add_help_option, prog)

        if add_username_password_options:
            option_list = [
                optparse.Option("--username", help="specify user"),
                optparse.Option("--password", help="specify password"),
            ]
            self._populate_option_list(option_list, add_help=False)
Ejemplo n.º 6
0
class failing(Command):
    """Show the current failures known by the repository.
    
    Today this is the failures from the most recent run, but once partial
    and full runs are understood it will be all the failures from the last
    full run combined with any failures in subsequent partial runs, minus any
    passes that have occured in a run more recent than a given failure. Deleted
    tests will only be detected on full runs with this approach.

    Without --subunit, the process exit code will be non-zero if the test run
    was not successful. With --subunit, the process exit code is non-zero if
    the subunit stream could not be generated successfully.
    """

    options = [
        optparse.Option(
            "--subunit", action="store_true",
            default=False, help="Show output as a subunit stream."),
        optparse.Option(
            "--list", action="store_true",
            default=False, help="Show only a list of failing tests."),
        ]
    # Can be assigned to to inject a custom command factory.
    command_factory = TestCommand

    def _show_subunit(self, run):
        stream = run.get_subunit_stream()
        self.ui.output_stream(stream)
        return 0

    def _make_result(self, repo):
        testcommand = self.command_factory(self.ui, repo)
        if self.ui.options.list:
            list_result = testtools.StreamSummary()
            return list_result, list_result
        else:
            return self.ui.make_result(repo.latest_id, testcommand)

    def run(self):
        repo = self.repository_factory.open(self.ui.here)
        run = repo.get_failing()
        if self.ui.options.subunit:
            return self._show_subunit(run)
        case = run.get_test()
        failed = False
        result, summary = self._make_result(repo)
        result.startTestRun()
        try:
            case.run(result)
        finally:
 def options(self):
     """Returns command parser options."""
     options = [
         optparse.Option(
             '--assigned',
             action='store_true',
             help=('Show only machines that are assigned to users.'
                   ' Default is %default')),
         optparse.Option(
             '--deployment',
             help=
             ('Deployment name. Uses project name by default if not explicitly'
              ' provided')),
     ]
     return options
Ejemplo n.º 8
0
class run(Command):
    __doc__ = """Run the tests for a project and load them into testrepository.
    """ + testrconf_help

    options = [
        optparse.Option("--failing", action="store_true",
            default=False, help="Run only tests known to be failing."),
        optparse.Option("--parallel", action="store_true",
            default=False, help="Run tests in parallel processes."),
        optparse.Option("--concurrency", action="store", type="int", default=0,
            help="How many processes to use. The default (0) autodetects your CPU count."),
        optparse.Option("--load-list", default=None,
            help="Only run tests listed in the named file."),
        optparse.Option("--partial", action="store_true",
            default=False,
            help="Only some tests will be run. Implied by --failing."),
        optparse.Option("--subunit", action="store_true",
            default=False, help="Display results in subunit format."),
        optparse.Option("--full-results", action="store_true",
            default=False,
            help="No-op - deprecated and kept only for backwards compat."),
        optparse.Option("--until-failure", action="store_true",
            default=False,
            help="Repeat the run again and again until failure occurs."),
        optparse.Option("--analyze-isolation", action="store_true",
            default=False,
            help="Search the last test run for 2-test test isolation interactions."),
        optparse.Option("--isolated", action="store_true",
            default=False,
            help="Run each test id in a separate test runner."),
        ]
    args = [StringArgument('testfilters', 0, None), DoubledashArgument(),
        StringArgument('testargs', 0, None)]
    # Can be assigned to to inject a custom command factory.
    command_factory = TestCommand

    def _find_failing(self, repo):
        run = repo.get_failing()
        case = run.get_test()
        ids = []
        def gather_errors(test_dict):
            if test_dict['status'] == 'fail':
                ids.append(test_dict['id'])
        result = testtools.StreamToDict(gather_errors)
        result.startTestRun()
        try:
            case.run(result)
        finally:
Ejemplo n.º 9
0
def option_recommendation_to_cli_option(add_option, rec):
    opt = rec.option
    switches = ['-' + opt.short_switch] if opt.short_switch else []
    switches.append('--' + opt.long_switch)
    attrs = dict(dest=opt.name,
                 help=opt.help,
                 choices=opt.choices,
                 default=rec.recommended_value)
    if isinstance(rec.recommended_value, type(True)):
        attrs['action'] = 'store_false' if rec.recommended_value else \
                          'store_true'
    else:
        if isinstance(rec.recommended_value, numbers.Integral):
            attrs['type'] = 'int'
        if isinstance(rec.recommended_value, numbers.Real):
            attrs['type'] = 'float'

    if opt.long_switch == 'verbose':
        attrs['action'] = 'count'
        attrs.pop('type', '')
    if opt.name == 'read_metadata_from_opf':
        switches.append('--from-opf')
    if opt.name == 'transform_css_rules':
        attrs['help'] = ('Path to a file containing rules to transform the '
                         'CSS styles in this book. The easiest way to create '
                         'such a file is to use the wizard for creating rules '
                         'in the calibre GUI. Access it in the "Look & '
                         'feel->Transform styles" section of the conversion '
                         'dialog. Once you create the rules, you can use the '
                         '"Export" button to save them to a file.')
    if opt.name in DEFAULT_TRUE_OPTIONS and rec.recommended_value is True:
        switches = ['--disable-' + opt.long_switch]
    add_option(optparse.Option(*switches, **attrs))
Ejemplo n.º 10
0
def test_generate_callback_from_composition(
    comma_separated_list,
    normalize_paths,
    defaults,
    value,
    expected_value,
):
    """Verify our generate_callback_from composition.

    We mock out parse_comma_separated_list and parse_normalized_paths with
    simple string transformations for better readability.
    """
    dest = 'foo'
    opt_str = '--foo'
    option = optparse.Option(opt_str, dest=dest)
    parser = mock.Mock(values=optparse.Values(defaults))

    base_callback = mock.Mock()
    callback = options.generate_callback_from(
        comma_separated_list=comma_separated_list,
        normalize_paths=normalize_paths,
        base_callback=base_callback,
    )

    with mock.patch('flake8_polyfill.options.parse_comma_separated_list') as \
            parse_comma_separated_list, \
            mock.patch('flake8_polyfill.options.parse_normalized_paths') as \
            parse_normalized_paths:

        parse_comma_separated_list.side_effect = lambda v: 'C({})'.format(v)
        parse_normalized_paths.side_effect = lambda v: 'N({})'.format(v)
        callback(option, opt_str, value, parser)

    base_callback.assert_called_with(option, opt_str, expected_value, parser)
Ejemplo n.º 11
0
class BuildPkgInfoCommand(Command):
    long_descr = """\
Purpose: generate PKG-INFO file
Usage:   bentomaker build_pkg_info [OPTIONS]"""
    short_descr = "generate PKG-INFO file."
    common_options = Command.common_options + [
        optparse.Option("-o", "--output", dest="output", help="Output file for PKG-INFO"),
    ]

    def run(self, ctx):
        argv = ctx.command_argv
        p = ctx.options_context.parser
        o, a = p.parse_args(argv)
        if o.help:
            p.print_help()
            return
        bento_script = ctx.top_node.find_node(BENTO_SCRIPT)

        if not o.output:
            pkg_info = "PKG-INFO"
        else:
            pkg_info = o.output

        pkg = PackageDescription.from_file(bento_script.abspath())

        fid = open(pkg_info, "w")
        try:
            write_pkg_info(pkg, fid)
        finally:
            fid.close()
 def seterrorleveloptions(self):
     """sets the errorlevel options"""
     self.errorleveltypes = ["none", "message", "exception", "traceback"]
     errorleveloption = optparse.Option(None, "--errorlevel", dest="errorlevel", default="message",
             choices = self.errorleveltypes, metavar="ERRORLEVEL",
             help="show errorlevel as: %s" % (", ".join(self.errorleveltypes)))
     self.define_option(errorleveloption)
Ejemplo n.º 13
0
 def setprogressoptions(self):
     """Sets the progress options."""
     progressoption = optparse.Option(
         None, "--progress", dest="progress", default="bar",
         choices=list(ProgressBar.progress_types.keys()), metavar="PROGRESS",
         help="show progress as: %s" % (", ".join(ProgressBar.progress_types)))
     self.define_option(progressoption)
Ejemplo n.º 14
0
 def to_optparse(self):
     """Convert a Flake8 Option to an optparse Option."""
     if self._opt is None:
         self._opt = optparse.Option(
             *self.option_args, **self.option_kwargs
         )
     return self._opt
Ejemplo n.º 15
0
    def add_format_option(self, flags=('-f', '--format'), target=None):
        """Add -f/--format option to print some LibModel instances with a
        custom format.

        `target` is optional and can be one of ``library.Item``, 'item',
        ``library.Album`` and 'album'.

        Several behaviors are available:
            - if `target` is given then the format is only applied to that
            LibModel
            - if the album option is used then the target will be autodetected
            - otherwise the format is applied to both items and albums.

        Sets the format property on the options extracted from the CLI.
        """
        kwargs = {}
        if target:
            if isinstance(target, six.string_types):
                target = {'item': library.Item, 'album': library.Album}[target]
            kwargs['target'] = target

        opt = optparse.Option(*flags,
                              action='callback',
                              callback=self._set_format,
                              callback_kwargs=kwargs,
                              help=u'print with custom format')
        self.add_option(opt)
Ejemplo n.º 16
0
    def test_debug(self):
        # Hack: we can modify this from inside the callback.
        confdict = {}

        def callback(option, opt_str, value, parser):
            section = parser.values.config.collapse_named_section('sect')
            # It would actually be better if debug was True here.
            # We check for an unintended change, not ideal behaviour.
            self.assertFalse(section.debug)
            confdict['sect'] = section

        parser = commandline.OptionParser(option_list=[
            optparse.Option('-a', action='callback', callback=callback)])
        parser = helpers.mangle_parser(parser)
        values = parser.get_default_values()
        values._config = mk_config([{
            'sect': basics.HardCodedConfigSection({'class': sect})}])

        values, args = parser.parse_args(['-a', '--debug'], values)
        self.assertFalse(args)
        self.assertTrue(values.debug)
        self.assertTrue(confdict['sect'].debug)

        values = parser.get_default_values()
        values._config = mk_config([{
            'sect': basics.HardCodedConfigSection({'class': sect})}])
        values, args = parser.parse_args(['-a'], values)
        self.assertFalse(args)
        self.assertFalse(values.debug)
        self.assertFalse(confdict['sect'].debug)
Ejemplo n.º 17
0
def test_store_callback():
    """Verify the default callback behaves like option with action='store'."""
    dest = 'foo'
    opt_str = '--foo'
    option = optparse.Option(opt_str, dest=dest)
    parser = mock.Mock(values=optparse.Values({'foo': 'defaultval'}))
    options.store_callback(option, opt_str, 'val', parser)
    assert parser.values.foo == 'val'
 def options(self):
   """Returns command parser options."""
   options = [
       optparse.Option('--deployment', help=(
           'Deployment name. Uses project name by default if not explicitly'
           ' provided')),
       ]
   return options
Ejemplo n.º 19
0
def test_comma_separated_callback(values, parsed_value, expected_value):
    """Assert our comma_separated_callback behaves the right way."""
    dest = 'select'
    opt_str = '--{}'.format(dest)
    option = optparse.Option(opt_str, dest=dest)
    parser = mock.Mock(values=optparse.Values(values))
    options.comma_separated_callback(option, opt_str, parsed_value, parser)
    assert getattr(parser.values, dest) == expected_value
Ejemplo n.º 20
0
    def setup(self, package_options):
        self.add_group("build_customization", "Build customization")
        opt = optparse.Option("--use-distutils",
                              help="Build extensions with distutils",
                              action="store_true")
        self.add_option(opt, "build_customization")

        self._is_setup = True
Ejemplo n.º 21
0
    def _add_opts(self, *args):
        """populates one or more options with their respective help texts"""
        option_list = [
            optparse.Option(option, help=help_text)
            for option, help_text in args
        ]

        self._populate_option_list(option_list=option_list, add_help=False)
Ejemplo n.º 22
0
def test_normalize_paths_callback(values, parsed_value, expected_value):
    """Assert our normalize_paths_callback behaves the right way."""
    dest = 'exclude'
    opt_str = '--exclude'
    option = optparse.Option(opt_str, dest=dest)
    parser = mock.Mock(values=optparse.Values(values))
    options.normalize_paths_callback(option, opt_str, parsed_value, parser)
    assert getattr(parser.values, dest) == expected_value
Ejemplo n.º 23
0
 def option_register(self):
     '''register the options to parse by the command line option parser'''
     option = optparse.Option("-s",
                              "--single",
                              action="count",
                              dest="single_instance",
                              default=False,
                              help="Allow only one instance of emesene")
     return option
Ejemplo n.º 24
0
def _get_pip_options(pip_args):
    # Reproduce piptools.scripts.compile.cli code
    pip_command = PipCommand()
    pip.cmdoptions.make_option_group(pip.cmdoptions.index_group, pip_command.parser)
    pip_command.parser.add_option(optparse.Option('--pre', action='store_true', default=False))
    pip_options, _ = pip_command.parse_args(pip_args)
    # pylint: disable=protected-access
    session = pip_command._build_session(pip_options)
    return pip_options, session
Ejemplo n.º 25
0
 def option_register(self):
     '''register options'''
     option = optparse.Option('--ext-default', '-e')
     option.type = 'string'  #well, it's a extName:defaultValue string
     option.action = 'callback'
     option.callback = self.set_default
     option.help = 'Set the default extension by name'
     option.nargs = 1
     return option
Ejemplo n.º 26
0
 def option_register(self):
     '''register the options to parse by the command line option parser'''
     option = optparse.Option("-m",
                              "--minimized",
                              action="count",
                              dest="minimized",
                              default=False,
                              help="Minimize emesene at start")
     return option
Ejemplo n.º 27
0
 def option_register(self):
     '''register the options to parse by the command line option parser'''
     option = optparse.Option(
         "-v",
         "--verbose",
         action="count",
         dest="debuglevel",
         default=0,
         help="Enable debug in console (add another -v to show debug)")
     return option
Ejemplo n.º 28
0
def computation_backend_options() -> Tuple[optparse.Option, ...]:
    return (
        optparse.Option(
            "--computation-backend",
            help=
            ("Computation backend for compiled PyTorch distributions, "
             "e.g. 'cu92', 'cu101', or 'cpu'. "
             "If not specified, the computation backend is detected from the "
             "available hardware, preferring CUDA over CPU."),
        ),
        optparse.Option(
            "--cpu",
            action="store_true",
            default=False,
            help=("Shortcut for '--computation-backend=cpu'. "
                  "If '--computation-backend' is used simultaneously, "
                  "it takes precedence over '--cpu'."),
        ),
    )
 def setpsycooption(self):
     try:
         import psyco # pylint: disable-msg=W0612
     except Exception:
         return
     psycomodes = ["none", "full", "profile"]
     psycooption = optparse.Option(None, "--psyco", dest="psyco", default=None,
         choices=psycomodes, metavar="MODE",
         help="use psyco to speed up the operation, modes: %s" % (", ".join(psycomodes)))
     self.define_option(psycooption)
Ejemplo n.º 30
0
 def __init__(self, opts, help=None, title=None, metavar=None, choices=None, dest=None, prompt=False, default=None, action=None):
     self.opts = opts
     self.help = help
     self.metavar = metavar
     self._choices = choices
     self.prompt = prompt
     self.default = default
     self.action = action
     self.op = optparse.Option(*opts, dest=dest, help=self.help, metavar=self.metavar, action=action)
     self.dest = self.op.dest
     self.title = title or self.dest