def parseargs()

in pylib/mercurial-support/run-tests.py [0:0]


def parseargs(args, parser):
    """Parse arguments with our OptionParser and validate results."""
    options = parser.parse_args(args)

    # jython is always pure
    if 'java' in sys.platform or '__pypy__' in sys.modules:
        options.pure = True

    if options.local:
        if options.with_hg or options.with_chg:
            parser.error('--local cannot be used with --with-hg or --with-chg')
        testdir = os.path.dirname(_bytespath(canonpath(sys.argv[0])))
        reporootdir = os.path.dirname(testdir)
        pathandattrs = [(b'hg', 'with_hg')]
        if options.chg:
            pathandattrs.append((b'contrib/chg/chg', 'with_chg'))
        for relpath, attr in pathandattrs:
            binpath = os.path.join(reporootdir, relpath)
            if os.name != 'nt' and not os.access(binpath, os.X_OK):
                parser.error(
                    '--local specified, but %r not found or '
                    'not executable' % binpath
                )
            setattr(options, attr, _strpath(binpath))

    if options.with_hg:
        options.with_hg = canonpath(_bytespath(options.with_hg))
        if not (
            os.path.isfile(options.with_hg)
            and os.access(options.with_hg, os.X_OK)
        ):
            parser.error('--with-hg must specify an executable hg script')
        if os.path.basename(options.with_hg) not in [b'hg', b'hg.exe']:
            sys.stderr.write('warning: --with-hg should specify an hg script\n')
            sys.stderr.flush()

    if (options.chg or options.with_chg) and os.name == 'nt':
        parser.error('chg does not work on %s' % os.name)
    if options.with_chg:
        options.chg = False  # no installation to temporary location
        options.with_chg = canonpath(_bytespath(options.with_chg))
        if not (
            os.path.isfile(options.with_chg)
            and os.access(options.with_chg, os.X_OK)
        ):
            parser.error('--with-chg must specify a chg executable')
    if options.chg and options.with_hg:
        # chg shares installation location with hg
        parser.error(
            '--chg does not work when --with-hg is specified '
            '(use --with-chg instead)'
        )

    if options.color == 'always' and not pygmentspresent:
        sys.stderr.write(
            'warning: --color=always ignored because '
            'pygments is not installed\n'
        )

    if options.bisect_repo and not options.known_good_rev:
        parser.error("--bisect-repo cannot be used without --known-good-rev")

    global useipv6
    if options.ipv6:
        useipv6 = checksocketfamily('AF_INET6')
    else:
        # only use IPv6 if IPv4 is unavailable and IPv6 is available
        useipv6 = (not checksocketfamily('AF_INET')) and checksocketfamily(
            'AF_INET6'
        )

    options.anycoverage = options.cover or options.annotate or options.htmlcov
    if options.anycoverage:
        try:
            import coverage

            covver = version.StrictVersion(coverage.__version__).version
            if covver < (3, 3):
                parser.error('coverage options require coverage 3.3 or later')
        except ImportError:
            parser.error('coverage options now require the coverage package')

    if options.anycoverage and options.local:
        # this needs some path mangling somewhere, I guess
        parser.error(
            "sorry, coverage options do not work when --local is specified"
        )

    if options.anycoverage and options.with_hg:
        parser.error(
            "sorry, coverage options do not work when --with-hg is specified"
        )

    global verbose
    if options.verbose:
        verbose = ''

    if options.tmpdir:
        options.tmpdir = canonpath(options.tmpdir)

    if options.jobs < 1:
        parser.error('--jobs must be positive')
    if options.interactive and options.debug:
        parser.error("-i/--interactive and -d/--debug are incompatible")
    if options.debug:
        if options.timeout != defaults['timeout']:
            sys.stderr.write('warning: --timeout option ignored with --debug\n')
        if options.slowtimeout != defaults['slowtimeout']:
            sys.stderr.write(
                'warning: --slowtimeout option ignored with --debug\n'
            )
        options.timeout = 0
        options.slowtimeout = 0
    if options.py3_warnings:
        if PYTHON3:
            parser.error('--py3-warnings can only be used on Python 2.7')

    if options.blacklist:
        options.blacklist = parselistfiles(options.blacklist, 'blacklist')
    if options.whitelist:
        options.whitelisted = parselistfiles(options.whitelist, 'whitelist')
    else:
        options.whitelisted = {}

    if options.showchannels:
        options.nodiff = True

    return options