Skip to content

Fully implement PEP 527 #7529

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

Merged
merged 4 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 4 additions & 101 deletions tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,10 @@ def test_upload_fails_with_legacy_type(self, pyramid_config, db_request):
resp = excinfo.value

assert resp.status_code == 400
assert resp.status == "400 Unknown type of file."
assert (
resp.status
== "400 Invalid value for filetype. Error: Use a known file type."
)

def test_upload_fails_with_legacy_ext(self, pyramid_config, db_request):
pyramid_config.testing_securitypolicy(userid=1)
Expand Down Expand Up @@ -2519,106 +2522,6 @@ def storage_service_store(path, file_path, *, meta):
)
]

def test_upload_succeeds_with_legacy_ext(
self, tmpdir, monkeypatch, pyramid_config, db_request, metrics
):
monkeypatch.setattr(tempfile, "tempdir", str(tmpdir))

pyramid_config.testing_securitypolicy(userid=1)

user = UserFactory.create()
EmailFactory.create(user=user)
project = ProjectFactory.create(allow_legacy_files=True)
release = ReleaseFactory.create(project=project, version="1.0")
RoleFactory.create(user=user, project=project)

filename = "{}-{}.tar.bz2".format(project.name, release.version)

db_request.user = user
db_request.remote_addr = "10.10.10.30"
db_request.user_agent = "warehouse-tests/6.6.6"
db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": release.version,
"filetype": "sdist",
"pyversion": "source",
"md5_digest": _TAR_BZ2_PKG_MD5,
"content": pretend.stub(
filename=filename,
file=io.BytesIO(_TAR_BZ2_PKG_TESTDATA),
type="application/tar",
),
}
)

def storage_service_store(path, file_path, *, meta):
with open(file_path, "rb") as fp:
assert fp.read() == _TAR_BZ2_PKG_TESTDATA

storage_service = pretend.stub(store=storage_service_store)
db_request.find_service = lambda svc, name=None, context=None: {
IFileStorage: storage_service,
IMetricsService: metrics,
}.get(svc)

monkeypatch.setattr(legacy, "_is_valid_dist_file", lambda *a, **kw: True)

resp = legacy.file_upload(db_request)

assert resp.status_code == 200

def test_upload_succeeds_with_legacy_type(
self, tmpdir, monkeypatch, pyramid_config, db_request, metrics
):
monkeypatch.setattr(tempfile, "tempdir", str(tmpdir))

pyramid_config.testing_securitypolicy(userid=1)

user = UserFactory.create()
EmailFactory.create(user=user)
project = ProjectFactory.create(allow_legacy_files=True)
release = ReleaseFactory.create(project=project, version="1.0")
RoleFactory.create(user=user, project=project)

filename = "{}-{}.tar.gz".format(project.name, release.version)

db_request.user = user
db_request.remote_addr = "10.10.10.30"
db_request.user_agent = "warehouse-tests/6.6.6"
db_request.POST = MultiDict(
{
"metadata_version": "1.2",
"name": project.name,
"version": release.version,
"filetype": "bdist_dumb",
"pyversion": "3.5",
"md5_digest": _TAR_GZ_PKG_MD5,
"content": pretend.stub(
filename=filename,
file=io.BytesIO(_TAR_GZ_PKG_TESTDATA),
type="application/tar",
),
}
)

def storage_service_store(path, file_path, *, meta):
with open(file_path, "rb") as fp:
assert fp.read() == _TAR_GZ_PKG_TESTDATA

storage_service = pretend.stub(store=storage_service_store)
db_request.find_service = lambda svc, name=None, context=None: {
IFileStorage: storage_service,
IMetricsService: metrics,
}.get(svc)

monkeypatch.setattr(legacy, "_is_valid_dist_file", lambda *a, **kw: True)

resp = legacy.file_upload(db_request)

assert resp.status_code == 200

@pytest.mark.parametrize("plat", ["linux_x86_64", "linux_x86_64.win32"])
def test_upload_fails_with_unsupported_wheel_plat(
self, monkeypatch, pyramid_config, db_request, plat
Expand Down
30 changes: 3 additions & 27 deletions warehouse/forklift/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ def _valid_platform_tag(platform_tag):
_error_message_order = ["metadata_version", "name", "version"]


_dist_file_regexes = {
# True/False is for legacy or not.
True: re.compile(r".+?\.(exe|tar\.gz|bz2|rpm|deb|zip|tgz|egg|dmg|msi|whl)$", re.I),
False: re.compile(r".+?\.(tar\.gz|zip|whl|egg)$", re.I),
}
_dist_file_re = re.compile(r".+?\.(tar\.gz|zip|whl|egg)$", re.I)


_wheel_file_re = re.compile(
Expand Down Expand Up @@ -500,17 +496,7 @@ class MetadataForm(forms.Form):
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.AnyOf(
[
"bdist_dmg",
"bdist_dumb",
"bdist_egg",
"bdist_msi",
"bdist_rpm",
"bdist_wheel",
"bdist_wininst",
"sdist",
],
message="Use a known file type.",
["bdist_egg", "bdist_wheel", "sdist"], message="Use a known file type.",
),
]
)
Expand Down Expand Up @@ -1171,7 +1157,7 @@ def file_upload(request):
)

# Make sure the filename ends with an allowed extension.
if _dist_file_regexes[project.allow_legacy_files].search(filename) is None:
if _dist_file_re.search(filename) is None:
raise _exc_with_message(
HTTPBadRequest,
"Invalid file extension: Use .egg, .tar.gz, .whl or .zip "
Expand All @@ -1193,16 +1179,6 @@ def file_upload(request):
):
raise _exc_with_message(HTTPBadRequest, "Invalid distribution file.")

# Ensure that the package filetype is allowed.
# TODO: Once PEP 527 is completely implemented we should be able to delete
# this and just move it into the form itself.
if not project.allow_legacy_files and form.filetype.data not in {
"sdist",
"bdist_wheel",
"bdist_egg",
}:
raise _exc_with_message(HTTPBadRequest, "Unknown type of file.")

# The project may or may not have a file size specified on the project, if
# it does then it may or may not be smaller or larger than our global file
# size limits.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
"""
Fully deprecate legacy distribution types

Revision ID: b265ed9eeb8a
Revises: c4cb2d15dada
Create Date: 2020-03-12 17:51:08.447903
"""

from alembic import op

revision = "b265ed9eeb8a"
down_revision = "c4cb2d15dada"


def upgrade():
op.drop_column("projects", "allow_legacy_files")


def downgrade():
raise RuntimeError("Order No. 227 - Ни шагу назад!")
1 change: 0 additions & 1 deletion warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class Project(SitemapMixin, db.Model):
has_docs = Column(Boolean)
upload_limit = Column(Integer, nullable=True)
last_serial = Column(Integer, nullable=False, server_default=sql.text("0"))
allow_legacy_files = Column(Boolean, nullable=False, server_default=sql.false())
zscore = Column(Float, nullable=True)

total_size = Column(BigInteger, server_default=sql.text("0"))
Expand Down