Esempio n. 1
0
def check(dists, output_stream=sys.stdout):
    uploads = [i for i in _find_dists(dists) if not i.endswith(".asc")]
    if not uploads:  # Return early, if there are no files to check.
        output_stream.write("No files to check.\n")
        return False

    failure = False

    for filename in uploads:
        output_stream.write("Checking %s: " % filename)
        render_warning_stream = _WarningStream()
        warnings, is_ok = _check_file(filename, render_warning_stream)

        # Print the status and/or error
        if not is_ok:
            failure = True
            output_stream.write("FAILED\n")

            error_text = ("`long_description` has syntax errors in markup and "
                          "would not be rendered on PyPI.\n")
            output_stream.write(_indented(error_text, "  "))
            output_stream.write(_indented(str(render_warning_stream), "    "))
        elif warnings:
            output_stream.write("PASSED, with warnings\n")
        else:
            output_stream.write("PASSED\n")

        # Print warnings after the status and/or error
        for message in warnings:
            output_stream.write('  warning: ' + message + '\n')

    return failure
Esempio n. 2
0
def test_find_dists_expands_globs():
    files = sorted(commands._find_dists(["twine/__*.py"]))
    expected = [
        os.path.join("twine", "__init__.py"),
        os.path.join("twine", "__main__.py"),
    ]
    assert expected == files
Esempio n. 3
0
def check(dists, output_stream=sys.stdout):
    uploads = [i for i in _find_dists(dists) if not i.endswith(".asc")]
    stream = _WarningStream()
    failure = False

    for filename in uploads:
        output_stream.write("Checking distribution %s: " % filename)
        package = PackageFile.from_filename(filename, comment=None)

        metadata = package.metadata_dictionary()
        content_type, parameters = cgi.parse_header(
            metadata.get("description_content_type") or "")

        # Get the appropriate renderer
        renderer = _RENDERERS.get(content_type, readme_renderer.txt)

        # Actually render the given value
        rendered = renderer.render(metadata.get("description"),
                                   stream=stream,
                                   **parameters)

        if rendered is None:
            failure = True
            output_stream.write("Failed\n")
            output_stream.write(
                "The project's long_description has invalid markup which will "
                "not be rendered on PyPI. The following syntax errors were "
                "detected:\n%s" % stream)
        else:
            output_stream.write("Passed\n")

    return failure
Esempio n. 4
0
def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
    dists = commands._find_dists(dists)
    # Determine if the user has passed in pre-signed distributions
    signatures = {os.path.basename(d): d for d in dists if d.endswith(".asc")}
    uploads = [i for i in dists if not i.endswith(".asc")]

    upload_settings.check_repository_url()
    repository_url = cast(str, upload_settings.repository_config["repository"])
    print(f"Uploading distributions to {repository_url}")

    packages_to_upload = [
        _make_package(filename, signatures, upload_settings)
        for filename in uploads
    ]

    repository = upload_settings.create_repository()
    uploaded_packages = []

    for package in packages_to_upload:
        skip_message = "  Skipping {} because it appears to already exist".format(
            package.basefilename)

        # Note: The skip_existing check *needs* to be first, because otherwise
        #       we're going to generate extra HTTP requests against a hardcoded
        #       URL for no reason.
        if upload_settings.skip_existing and repository.package_is_uploaded(
                package):
            print(skip_message)
            continue

        resp = repository.upload(package)

        # Bug 92. If we get a redirect we should abort because something seems
        # funky. The behaviour is not well defined and redirects being issued
        # by PyPI should never happen in reality. This should catch malicious
        # redirects as well.
        if resp.is_redirect:
            raise exceptions.RedirectDetected.from_args(
                repository_url,
                resp.headers["location"],
            )

        if skip_upload(resp, upload_settings.skip_existing, package):
            print(skip_message)
            continue

        utils.check_status_code(resp, upload_settings.verbose)

        uploaded_packages.append(package)

    release_urls = repository.release_urls(uploaded_packages)
    if release_urls:
        print("\nView at:")
        for url in release_urls:
            print(url)

    # Bug 28. Try to silence a ResourceWarning by clearing the connection
    # pool.
    repository.close()
Esempio n. 5
0
def upload(upload_settings, dists):
    dists = _find_dists(dists)

    # Determine if the user has passed in pre-signed distributions
    signatures = dict(
        (os.path.basename(d), d) for d in dists if d.endswith(".asc")
    )
    uploads = [i for i in dists if not i.endswith(".asc")]
    upload_settings.check_repository_url()
    repository_url = upload_settings.repository_config['repository']

    print("Uploading distributions to {0}".format(repository_url))

    repository = upload_settings.create_repository()

    for filename in uploads:
        package = PackageFile.from_filename(filename, upload_settings.comment)
        skip_message = (
            "  Skipping {0} because it appears to already exist".format(
                package.basefilename)
        )

        # Note: The skip_existing check *needs* to be first, because otherwise
        #       we're going to generate extra HTTP requests against a hardcoded
        #       URL for no reason.
        if (upload_settings.skip_existing and
                repository.package_is_uploaded(package)):
            print(skip_message)
            continue

        signed_name = package.signed_basefilename
        if signed_name in signatures:
            package.add_gpg_signature(signatures[signed_name], signed_name)
        elif upload_settings.sign:
            package.sign(upload_settings.sign_with, upload_settings.identity)

        resp = repository.upload(package)

        # Bug 92. If we get a redirect we should abort because something seems
        # funky. The behaviour is not well defined and redirects being issued
        # by PyPI should never happen in reality. This should catch malicious
        # redirects as well.
        if resp.is_redirect:
            raise exc.RedirectDetected(
                ('"{0}" attempted to redirect to "{1}" during upload.'
                 ' Aborting...').format(repository_url,
                                        resp.headers["location"]))

        if skip_upload(resp, upload_settings.skip_existing, package):
            print(skip_message)
            continue
        utils.check_status_code(resp, upload_settings.verbose)

    # Bug 28. Try to silence a ResourceWarning by clearing the connection
    # pool.
    repository.close()

    return True
Esempio n. 6
0
def test_find_dists_handles_real_files():
    expected = [
        "twine/__init__.py",
        "twine/__main__.py",
        "twine/cli.py",
        "twine/utils.py",
        "twine/wheel.py",
    ]
    files = commands._find_dists(expected)
    assert expected == files
Esempio n. 7
0
File: upload.py Progetto: pypa/twine
def upload(upload_settings, dists):
    dists = _find_dists(dists)

    # Determine if the user has passed in pre-signed distributions
    signatures = {os.path.basename(d): d for d in dists if d.endswith(".asc")}
    uploads = [i for i in dists if not i.endswith(".asc")]
    upload_settings.check_repository_url()
    repository_url = upload_settings.repository_config['repository']

    print("Uploading distributions to {}".format(repository_url))

    repository = upload_settings.create_repository()

    for filename in uploads:
        package = PackageFile.from_filename(filename, upload_settings.comment)
        skip_message = (
            "  Skipping {} because it appears to already exist".format(
                package.basefilename)
        )

        # Note: The skip_existing check *needs* to be first, because otherwise
        #       we're going to generate extra HTTP requests against a hardcoded
        #       URL for no reason.
        if (upload_settings.skip_existing and
                repository.package_is_uploaded(package)):
            print(skip_message)
            continue

        signed_name = package.signed_basefilename
        if signed_name in signatures:
            package.add_gpg_signature(signatures[signed_name], signed_name)
        elif upload_settings.sign:
            package.sign(upload_settings.sign_with, upload_settings.identity)

        resp = repository.upload(package)

        # Bug 92. If we get a redirect we should abort because something seems
        # funky. The behaviour is not well defined and redirects being issued
        # by PyPI should never happen in reality. This should catch malicious
        # redirects as well.
        if resp.is_redirect:
            raise exceptions.RedirectDetected(
                ('"{0}" attempted to redirect to "{1}" during upload.'
                 ' Aborting...').format(repository_url,
                                        resp.headers["location"]))

        if skip_upload(resp, upload_settings.skip_existing, package):
            print(skip_message)
            continue
        utils.check_status_code(resp, upload_settings.verbose)

    # Bug 28. Try to silence a ResourceWarning by clearing the connection
    # pool.
    repository.close()
Esempio n. 8
0
File: check.py Progetto: pypa/twine
def check(dists, output_stream=sys.stdout):
    uploads = [i for i in _find_dists(dists) if not i.endswith(".asc")]
    stream = _WarningStream()
    failure = False

    for filename in uploads:
        output_stream.write("Checking distribution %s: " % filename)
        package = PackageFile.from_filename(filename, comment=None)

        metadata = package.metadata_dictionary()
        description = metadata["description"]
        description_content_type = metadata["description_content_type"]

        if description_content_type is None:
            output_stream.write(
                'warning: `long_description_content_type` missing.  '
                'defaulting to `text/x-rst`.\n'
            )
            description_content_type = 'text/x-rst'

        content_type, params = cgi.parse_header(description_content_type)
        renderer = _RENDERERS.get(content_type, _RENDERERS[None])

        if description in {None, 'UNKNOWN\n\n\n'}:
            output_stream.write('warning: `long_description` missing.\n')
            output_stream.write("Passed\n")
        else:
            if (
                renderer
                and renderer.render(description, stream=stream, **params)
                is None
            ):
                failure = True
                output_stream.write("Failed\n")
                output_stream.write(
                    "The project's long_description has invalid markup which "
                    "will not be rendered on PyPI. The following syntax "
                    "errors were detected:\n%s" % stream
                )
            else:
                output_stream.write("Passed\n")

    return failure
Esempio n. 9
0
def check(dists, output_stream=sys.stdout):
    uploads = [i for i in _find_dists(dists) if not i.endswith(".asc")]
    stream = _WarningStream()
    failure = False

    for filename in uploads:
        output_stream.write("Checking distribution %s: " % filename)
        package = PackageFile.from_filename(filename, comment=None)

        metadata = package.metadata_dictionary()
        description = metadata["description"]
        description_content_type = metadata["description_content_type"]

        if description_content_type is None:
            output_stream.write(
                'warning: `long_description_content_type` missing.  '
                'defaulting to `text/x-rst`.\n'
            )
            description_content_type = 'text/x-rst'

        content_type, params = cgi.parse_header(description_content_type)
        renderer = _RENDERERS.get(content_type, _RENDERERS[None])

        if description in {None, 'UNKNOWN\n\n\n'}:
            output_stream.write('warning: `long_description` missing.\n')
            output_stream.write("Passed\n")
        else:
            if (
                renderer
                and renderer.render(description, stream=stream, **params)
                is None
            ):
                failure = True
                output_stream.write("Failed\n")
                output_stream.write(
                    "The project's long_description has invalid markup which "
                    "will not be rendered on PyPI. The following syntax "
                    "errors were detected:\n%s" % stream
                )
            else:
                output_stream.write("Passed\n")

    return failure
Esempio n. 10
0
def check(
    dists: List[str],
    output_stream: IO[str] = sys.stdout,
    strict: bool = False,
) -> bool:
    uploads = [i for i in commands._find_dists(dists) if not i.endswith(".asc")]
    if not uploads:  # Return early, if there are no files to check.
        output_stream.write("No files to check.\n")
        return False

    failure = False

    for filename in uploads:
        output_stream.write("Checking %s: " % filename)
        render_warning_stream = _WarningStream()
        warnings, is_ok = _check_file(filename, render_warning_stream)

        # Print the status and/or error
        if not is_ok:
            failure = True
            output_stream.write("FAILED\n")

            error_text = (
                "`long_description` has syntax errors in markup and "
                "would not be rendered on PyPI.\n"
            )
            output_stream.write(textwrap.indent(error_text, "  "))
            output_stream.write(textwrap.indent(str(render_warning_stream), "    "))
        elif warnings:
            if strict:
                failure = True
                output_stream.write("FAILED, due to warnings\n")
            else:
                output_stream.write("PASSED, with warnings\n")
        else:
            output_stream.write("PASSED\n")

        # Print warnings after the status and/or error
        for message in warnings:
            output_stream.write("  warning: " + message + "\n")

    return failure
Esempio n. 11
0
def test_find_dists_errors_on_invalid_globs():
    with pytest.raises(exceptions.InvalidDistribution):
        commands._find_dists(["twine/*.rb"])
Esempio n. 12
0
def upload(upload_settings, dists):
    dists = _find_dists(dists)

    # Determine if the user has passed in pre-signed distributions
    signatures = {os.path.basename(d): d for d in dists if d.endswith(".asc")}
    uploads = [i for i in dists if not i.endswith(".asc")]
    upload_settings.check_repository_url()
    repository_url = upload_settings.repository_config['repository']

    print(f"Uploading distributions to {repository_url}")

    repository = upload_settings.create_repository()
    uploaded_packages = []

    for filename in uploads:
        package = PackageFile.from_filename(filename, upload_settings.comment)
        skip_message = (
            "  Skipping {} because it appears to already exist".format(
                package.basefilename))

        # Note: The skip_existing check *needs* to be first, because otherwise
        #       we're going to generate extra HTTP requests against a hardcoded
        #       URL for no reason.
        if (upload_settings.skip_existing
                and repository.package_is_uploaded(package)):
            print(skip_message)
            continue

        signed_name = package.signed_basefilename
        if signed_name in signatures:
            package.add_gpg_signature(signatures[signed_name], signed_name)
        elif upload_settings.sign:
            package.sign(upload_settings.sign_with, upload_settings.identity)

        # Suppress TLS verification warning on trusted custom certs
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            resp = repository.upload(package)

        # Bug 92. If we get a redirect we should abort because something seems
        # funky. The behaviour is not well defined and redirects being issued
        # by PyPI should never happen in reality. This should catch malicious
        # redirects as well.
        if resp.is_redirect:
            raise exceptions.RedirectDetected.from_args(
                repository_url,
                resp.headers["location"],
            )

        if skip_upload(resp, upload_settings.skip_existing, package):
            print(skip_message)
            continue

        utils.check_status_code(resp, upload_settings.verbose)

        uploaded_packages.append(package)

    release_urls = repository.release_urls(uploaded_packages)
    if release_urls:
        print('\nView at:')
        for url in release_urls:
            print(url)

    # Bug 28. Try to silence a ResourceWarning by clearing the connection
    # pool.
    repository.close()
Esempio n. 13
0
def test_find_dists_errors_on_invalid_globs():
    with pytest.raises(ValueError):
        _find_dists(["twine/*.rb"])