def get_repository_from_config(config_file, repository, repository_url=None):
    # Get our config from, if provided, command-line values for the
    # repository name and URL, or the .pypirc file
    if repository_url and "://" in repository_url:
        # prefer CLI `repository_url` over `repository` or .pypirc
        return {
            "repository": repository_url,
            "username": None,
            "password": None,
        }
    if repository_url and "://" not in repository_url:
        raise exceptions.UnreachableRepositoryURLDetected(
            "Repository URL {} has no protocol. Please add "
            "'https://'. \n".format(repository_url))
    try:
        return get_config(config_file)[repository]
    except KeyError:
        msg = (
            "Missing '{repo}' section from the configuration file\n"
            "or not a complete URL in --repository-url.\n"
            "Maybe you have a out-dated '{cfg}' format?\n"
            "more info: "
            "https://docs.python.org/distutils/packageindex.html#pypirc\n"
        ).format(
            repo=repository,
            cfg=config_file
        )
        raise exceptions.InvalidConfiguration(msg)
Example #2
0
def _validate_repository_url(repository_url: str) -> None:
    """Validate the given url for allowed schemes and components."""
    # Allowed schemes are http and https, based on whether the repository
    # supports TLS or not, and scheme and host must be present in the URL
    validator = (rfc3986.validators.Validator().allow_schemes(
        "http", "https").require_presence_of("scheme", "host"))
    try:
        validator.validate(rfc3986.uri_reference(repository_url))
    except rfc3986.exceptions.RFC3986Exception as exc:
        raise exceptions.UnreachableRepositoryURLDetected(
            f"Invalid repository URL: {exc.args[0]}.")