Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP one-shot upload works, adding tests/docs
fixes #4396 https://pulp.plan.io/issues/4396
- Loading branch information
1 parent
3c65c04
commit f3c1c27
Showing
4 changed files
with
101 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,84 @@ | ||
from pulpcore.app.models.content import Artifact, ContentArtifact | ||
import os | ||
from gettext import gettext as _ | ||
import pkginfo | ||
import shutil | ||
import tempfile | ||
|
||
from pulpcore.plugin.models import Artifact, CreatedResource, Repository, RepositoryVersion | ||
from rest_framework import serializers | ||
|
||
from pulp_python.app.models import PythonPackageContent | ||
from pulp_python.app.utils import parse_project_metadata | ||
|
||
import logging | ||
log=logging.getLogger('moose') | ||
|
||
DIST_EXTENSIONS = { | ||
".whl": "bdist_wheel", | ||
".exe": "bdist_wininst", | ||
".egg": "bdist_egg", | ||
".tar.bz2": "sdist", | ||
".tar.gz": "sdist", | ||
".zip": "sdist", | ||
} | ||
|
||
DIST_TYPES = { | ||
"bdist_wheel": pkginfo.Wheel, | ||
"bdist_wininst": pkginfo.Distribution, | ||
"bdist_egg": pkginfo.BDist, | ||
"sdist": pkginfo.SDist, | ||
} | ||
|
||
def one_shot_upload(artifact, filename, data, repository=None): | ||
|
||
def one_shot_upload(artifact_pk, filename, repository_pk=None): | ||
""" | ||
One shot upload for pulp_python | ||
Args: | ||
artifact: validated artifact | ||
artifact_pk: validated artifact | ||
filename: file name | ||
repository: optional repository to add Content to | ||
repository_pk: optional repository to add Content to | ||
""" | ||
|
||
# iterate through extensions since splitext does not support things like .tar.gz | ||
for ext, packagetype in DIST_EXTENSIONS.items(): | ||
if filename.endswith(ext): | ||
# Copy file to a temp directory under the user provided filename, we do this | ||
# because pkginfo validates that the filename has a valid extension before | ||
# reading it | ||
with tempfile.TemporaryDirectory() as td: | ||
temp_path = os.path.join(td, filename) | ||
artifact = Artifact.objects.get(pk=artifact_pk) | ||
shutil.copy2(artifact.file.path, temp_path) | ||
metadata = DIST_TYPES[packagetype](temp_path) | ||
metadata.packagetype = packagetype | ||
break | ||
else: | ||
raise serializers.ValidationError(_( | ||
"Extension on {} is not a valid python extension " | ||
"(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)").format(filename) | ||
) | ||
data = parse_project_metadata(vars(metadata)) | ||
data['classifiers'] = [{'name': classifier} for classifier in metadata.classifiers] | ||
data['packagetype'] = metadata.packagetype | ||
data['version'] = metadata.version | ||
data['filename'] = filename | ||
data['_relative_path'] = filename | ||
|
||
new_content = PythonPackageContent.objects.create( | ||
filename=filename, | ||
package_type=data['package_type'], | ||
name=data['classifier'], | ||
packagetype=metadata.packagetype, | ||
name=data['classifiers'], | ||
version=data['version'] | ||
) | ||
|
||
new_content_artifact = ContentArtifact.objects.create( | ||
artifact=artifact, | ||
content = new_content, | ||
relative_path=filename | ||
) | ||
log.warn(new_content) | ||
queryset = PythonPackageContent.objects.filter(pk=new_content.pk) | ||
|
||
if repository: | ||
if repository_pk: | ||
repository = Repository.objects.get(pk=repository_pk) | ||
with RepositoryVersion.create(repository) as new_version: | ||
new_version.add_content(new_content_artifact) | ||
new_version.add_content(queryset) | ||
|
||
resource = CreatedResource(content_object=new_content) | ||
resource.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.conf.urls import url | ||
|
||
from .viewsets import PythonOneShotUploadViewSet | ||
|
||
|
||
urlpatterns = [ | ||
url(r'python/upload/$', PythonOneShotUploadViewSet.as_view({'post': 'create'})) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters