Example #1
0
def test_write_existing_conf(tmp, mocker):
    mocked_input = mocker.patch("mozregression.config.input")
    mocked_input.return_value = ""
    conf_path = os.path.join(tmp, "conf.cfg")
    write_config(conf_path)
    results = get_config(conf_path)
    assert results
    # write conf again
    write_config(conf_path)
    # nothing changed
    assert results == get_config(conf_path)
    with open(conf_path) as f:
        # ensure we have comments
        assert "# ------ mozregression configuration file ------" in f.read()
Example #2
0
def cli(argv=None, conf_file=DEFAULT_CONF_FNAME, namespace=None):
    """
    parse cli args basically and returns a :class:`Configuration`.

    if namespace is given, it will be used as a arg parsing result, so no
    arg parsing will be done.
    """
    config = get_config(conf_file)
    if namespace:
        options = namespace
    else:
        options = parse_args(argv=argv, defaults=config)
        if not options.cmdargs:
            # we don't set the cmdargs default to be that from the
            # configuration file, because then any new arguments
            # will be appended: https://bugs.python.org/issue16399
            options.cmdargs = config["cmdargs"]
    if conf_file and not os.path.isfile(conf_file):
        print("*" * 10)
        print(
            colorize("You should use a config file. Please use the " +
                     "{sBRIGHT}--write-config{sRESET_ALL}" +
                     " command line flag to help you create one."))
        print("*" * 10)
        print()
    return Configuration(options, config)
Example #3
0
def tc_authenticate(logger):
    """
    Returns valid credentials for use with Taskcluster private builds.
    """
    # first, try to load credentials from mozregression config file
    defaults = get_config(DEFAULT_CONF_FNAME)
    client_id = defaults.get("taskcluster-clientid")
    access_token = defaults.get("taskcluster-accesstoken")
    if client_id and access_token:
        return dict(clientId=client_id, accessToken=access_token)

    try:
        # else, try to load a valid certificate locally
        with open(TC_CREDENTIALS_FNAME) as f:
            creds = json.load(f)
        if not tc_utils.isExpired(creds["certificate"]):
            return creds
    except Exception:
        pass

    # here we need to ask for a certificate, this require web browser
    # authentication
    logger.info(
        "Authentication required from taskcluster. We are going to ask for a"
        " certificate.\nNote that if you have long term access you can instead"
        " set your taskcluster-clientid and taskcluster-accesstoken in the"
        " configuration file (%s)." % DEFAULT_CONF_FNAME
    )
    creds = tc_utils.authenticate("mozregression private build access")

    # save the credentials and the certificate for later use
    with open(TC_CREDENTIALS_FNAME, "w") as f:
        json.dump(creds, f)
    return creds
Example #4
0
def get_prefs():
    """
    Return the global prefs as a dict.
    """
    settings = get_config(DEFAULT_CONF_FNAME)
    options = dict()
    options["persist"] = settings["persist"]
    options["http_timeout"] = float(settings["http-timeout"])
    options["persist_size_limit"] = float(settings["persist-size-limit"])
    options["background_downloads"] = (
        False if settings.get("background_downloads") == "no" else True
    )
    options["approx_policy"] = settings["approx-policy"] == "auto"
    options["archive_base_url"] = settings["archive-base-url"]
    options["cmdargs"] = settings["cmdargs"]
    options["enable_telemetry"] = not settings.get("enable-telemetry") in ["no", "0", "false"]

    return options
Example #5
0
def test_write_config(tmp, mocker, os_, bits, inputs, conf_dir_exists, results):
    mozinfo = mocker.patch("mozregression.config.mozinfo")
    mozinfo.os = os_
    mozinfo.bits = bits
    mocked_input = mocker.patch("mozregression.config.input")
    mocked_input.return_value = ""
    mocked_input.side_effect = inputs
    conf_path = os.path.join(tmp, "conf.cfg")
    if not conf_dir_exists:
        mozfile.remove(conf_path)
    write_config(conf_path)
    if "persist" in results and results["persist"] is None:
        # default persist is base on the directory of the conf file
        results["persist"] = os.path.join(tmp, "persist")
    conf = get_config(conf_path)
    for key in results:
        assert conf[key] == results[key]
    with open(conf_path) as f:
        # ensure we have comments
        assert "# ------ mozregression configuration file ------" in f.read()
Example #6
0
def parser():
    """
    Create and returns the mozregression ArgumentParser instance.
    """
    defaults = get_config(DEFAULT_CONF_FNAME)
    return create_parser(defaults=defaults)