Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the "safe_name" attribute of PackageFile for backwards compatibility #745

Merged
merged 2 commits into from Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/745.bugfix.rst
@@ -0,0 +1 @@
Fixed a regression that was causing some namespace packages with dots in them fail to upload to PyPI.
24 changes: 24 additions & 0 deletions tests/test_package.py
Expand Up @@ -11,6 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import string

import pretend
import pytest

Expand Down Expand Up @@ -110,6 +112,28 @@ def test_package_signed_name_is_correct():
assert package.signed_filename == (filename + ".asc")


@pytest.mark.parametrize(
"pkg_name,expected_name",
[
(string.ascii_letters, string.ascii_letters),
(string.digits, string.digits),
(string.punctuation, "-.-"),
("mosaik.SimConfig", "mosaik.SimConfig"),
("mosaik$$$$.SimConfig", "mosaik-.SimConfig"),
],
)
def test_package_safe_name_is_correct(pkg_name, expected_name):
package = package_file.PackageFile(
filename="tests/fixtures/deprecated-pypirc",
comment=None,
metadata=pretend.stub(name=pkg_name),
python_version=None,
filetype=None,
)

assert package.safe_name == expected_name


@pytest.mark.parametrize("gpg_signature", [(None), (pretend.stub())])
def test_metadata_dictionary(gpg_signature):
meta = pretend.stub(
Expand Down
12 changes: 10 additions & 2 deletions twine/package.py
Expand Up @@ -14,11 +14,11 @@
import hashlib
import io
import os
import re
import subprocess
from typing import Dict, NamedTuple, Optional, Sequence, Tuple, Union

import importlib_metadata
import packaging.utils
import pkginfo

from twine import exceptions
Expand All @@ -44,6 +44,14 @@
MetadataValue = Union[str, Sequence[str]]


def _safe_name(name: str) -> str:
"""Convert an arbitrary string to a standard distribution name.

Any runs of non-alphanumeric/. characters are replaced with a single '-'.
"""
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
return re.sub("[^A-Za-z0-9.]+", "-", name)


class PackageFile:
def __init__(
self,
Expand All @@ -59,7 +67,7 @@ def __init__(
self.metadata = metadata
self.python_version = python_version
self.filetype = filetype
self.safe_name = packaging.utils.canonicalize_name(metadata.name)
self.safe_name = _safe_name(metadata.name)
self.signed_filename = self.filename + ".asc"
self.signed_basefilename = self.basefilename + ".asc"
self.gpg_signature: Optional[Tuple[str, bytes]] = None
Expand Down