Esempio n. 1
0
    def _pt_save_file(self, bytes):
        p = self._pt_gen_file_path()
        exists = os.path.exists(p)

        if not exists:
            d = os.path.dirname(p)
            try:
                mkpath(d)
            except:
                return pt_rest_err(http.client.INTERNAL_SERVER_ERROR,
                                   "Can't create directory %s" % d)

        flags = os.O_RDWR | os.O_EXCL | os.O_TRUNC
        if not self.id:
            flags |= os.O_CREAT

        do_locks = not sys.platform.startswith('win')

        try:
            f = os.fdopen(os.open(p, flags), "wb")
            if not IS_WINDOWS:  # FIXME
                fcntl.flock(f, fcntl.LOCK_EX)
            f.write(bytes)
            if not IS_WINDOWS:  # FIXME
                fcntl.flock(f, fcntl.LOCK_UN)
            f.close()
        except IOError as e:
            return pt_rest_err(
                http.client.INTERNAL_SERVER_ERROR,
                "Can't store artifact to file %s, %s" % (p, str(e)))

        self.size = os.path.getsize(p)
        return None
Esempio n. 2
0
    def pt_get_http_content(self):
        p = self._pt_gen_file_path()
        if not p or not os.path.exists(p):
            if self.deleted:
                return pt_rest_err(
                    http.client.GONE, "Data with UUID %s was deleted @ %s" %
                    (self.uuid, str(self.deleted_dt)))
            else:
                return pt_rest_err(http.client.NOT_FOUND,
                                   "Can't find artifact file for UUID %s" %
                                   self.uuid,
                                   uuid=self.uuid)

        try:
            if self.compression:
                f = bz2.BZ2File(p, 'rb')
            else:
                f = open(p, 'rb')
            content = f.read()
            f.close()
        except IOError:
            return pt_rest_err(
                http.client.INTERNAL_SERVER_ERROR,
                "Internal error, can't read artifact for UUID %s" % self.uuid)

        resp = HttpResponse(content, content_type=self.mime)
        if self.inline:
            resp['Content-Disposition'] = 'inline'
        else:
            resp[
                'Content-Disposition'] = 'attachment; filename=%s' % self.filename
        return resp
Esempio n. 3
0
def pt_artifact_meta_all_json(request, api_ver, project_id):
    project_id = int(project_id)

    if not ArtifactMetaModel.pt_artifacts_enabled():
        return pt_rest_err(
            http.client.NOT_IMPLEMENTED,
            "Artifacts management is not configured, set the ARTIFACTS_STORAGE_DIR setting"
        )

    if request.method == "GET":
        if project_id:
            qs = ArtifactMetaModel.objects.filter(project_id=project_id,
                                                  deleted=False)
        else:
            qs = ArtifactMetaModel.objects.filter(deleted=False)
        try:
            start = int(request.GET.get('start', 0))
            length = int(request.GET.get('length', 10))
        except ValueError:
            raise SuspiciousOperation("start and length must be integer")

        qs = qs.order_by('-uploaded_dt')[start:(start + length)]
        return pt_rest_ok(ArtifactMetaSerializer(qs, many=True).data)

    raise pt_rest_method_not_allowed(request)
Esempio n. 4
0
def pt_artifact_meta_id_json(request, api_ver, project_id, uuid):
    project_id = int(project_id)

    uuid = uuid.lower()
    if not pt_is_valid_uuid(uuid):
        return pt_rest_bad_req("Invalid artifact UUID %s" % uuid)

    if not ArtifactMetaModel.pt_artifacts_enabled():
        return pt_rest_err(
            http.client.NOT_IMPLEMENTED,
            "Artifacts management is not configured, set the ARTIFACTS_STORAGE_DIR setting"
        )

    try:
        artifact = ArtifactMetaModel.objects.get(uuid=uuid)
    except ArtifactMetaModel.DoesNotExist:
        artifact = None

    if request.method == "POST":
        if artifact is None:
            artifact = ArtifactMetaModel(uuid=uuid)
        return artifact.pt_update(request.POST, request.FILES)

    if artifact is None:
        return pt_rest_not_found("Artifact with UUID %s is not found" % uuid)

    if request.method == "PATCH":
        data = json.loads(request.body.decode("utf-8"))
        return artifact.pt_update(data)
    elif request.method == "DELETE":
        return artifact.pt_delete()
    elif request.method == "GET":
        return pt_rest_ok(ArtifactMetaSerializer(artifact).data)

    return pt_rest_method_not_allowed(request)
Esempio n. 5
0
def pt_artifact_content_id(request, project_id, uuid):
    if not ArtifactMetaModel.pt_artifacts_enabled():
        return pt_rest_err(
            http.client.NOT_IMPLEMENTED,
            "Artifacts management is not configured, set the ARTIFACTS_STORAGE_DIR setting"
        )

    try:
        artifact = ArtifactMetaModel.objects.get(uuid=uuid)
    except ArtifactMetaModel.DoesNotExist:
        return Http404

    return artifact.pt_get_http_content()