Esempio n. 1
0
def generate_docker_args(job_mode: conf.JobMode,
                         args: Dict[str, Any]) -> Dict[str, Any]:
    """gemerate docker args from args and job mode"""

    # Get extra dependencies in case you want to install your requirements via a
    # setup.py file.
    setup_extras = b.base_extras(job_mode, "setup.py", args.get("extras"))

    # Google application credentials, from the CLI or from an env variable.
    creds_path = conf.extract_cloud_key(args)

    # Application default credentials location.
    adc_loc = csdk.get_application_default_credentials_path()
    adc_path = adc_loc if os.path.isfile(adc_loc) else None

    # TODO we may want to take custom paths, here, in addition to detecting them.
    reqs = "requirements.txt"
    conda_env = "environment.yml"

    # Arguments that make their way down to caliban.docker.build.build_image.
    docker_args = {
        "extra_dirs": args.get("dir"),
        "requirements_path": reqs if os.path.exists(reqs) else None,
        "conda_env_path": conda_env if os.path.exists(conda_env) else None,
        "caliban_config": conf.caliban_config(),
        "credentials_path": creds_path,
        "adc_path": adc_path,
        "setup_extras": setup_extras,
        "no_cache": args.get("no_cache", False),
        'build_path': os.getcwd(),
    }

    return docker_args
Esempio n. 2
0
def test_caliban_config(tmpdir):
    """Tests validation of the CalibanConfig schema and the method that returns the
  parsed config.

  """
    valid = {"apt_packages": {"cpu": ["face"]}}
    valid_path = tmpdir.join('valid.json')

    with open(valid_path, 'w') as f:
        json.dump(valid, f)

    valid_shared = {"apt_packages": ["face"]}
    valid_shared_path = tmpdir.join('valid_shared.json')

    with open(valid_shared_path, 'w') as f:
        json.dump(valid_shared, f)

    invalid = {"apt_packages": "face"}
    invalid_path = tmpdir.join('invalid.json')

    with open(invalid_path, 'w') as f:
        json.dump(invalid, f)

    # Failing the schema raises an error.
    with pytest.raises(us.FatalSchemaError):
        c.caliban_config(invalid_path)

    # paths that don't exist return an empty map:
    assert c.caliban_config('random_path') == {}

    # If the config is valid, c.apt_packages can fetch the packages we specified.
    config = c.caliban_config(valid_path)
    assert c.apt_packages(config, c.JobMode.GPU) == []
    assert c.apt_packages(config, c.JobMode.CPU) == ["face"]

    # If the user supplies a list instead of a dict, all still works well.
    valid_shared_conf = c.caliban_config(valid_shared_path)
    cpu = c.apt_packages(valid_shared_conf, c.JobMode.CPU)
    gpu = c.apt_packages(valid_shared_conf, c.JobMode.GPU)

    assert cpu == gpu
    assert cpu == ["face"]