Пример #1
0
    def show(self, package_id, id, format):
        """Retrieve the page describing a package version.

        Depending on the format, this could be a user-readable webpage (.html),
        a machine-readable YAML document (.yaml), or a download of the actual
        package blob (.tar.gz).
        """

        # The built-in format parsing has trouble with versions since they
        # contain periods, so we have to undo it and apply our own.
        id = '%s.%s' % (id, format)
        if id.endswith('.tar.gz'):
            id = id[0:-len('.tar.gz')]
            version = handlers.request().package_version(id)
            try:
                deferred.defer(self._count_download, version.key())
            except apiproxy_errors.DeadlineExceededError:
                logging.info('Issues defering count update for %s ' % id)
                pass
            raise cherrypy.HTTPRedirect(version.download_url)
        elif id.endswith('.yaml'):
            id = id[0:-len('.yaml')]
            version = handlers.request().package_version(id)
            cherrypy.response.headers['Content-Type'] = 'text/yaml'
            return version.pubspec.to_yaml()
        else:
            handlers.http_error(404)
Пример #2
0
 def show(self, id, format='html'):
     """Retrieve the page describing a specific package."""
     if format == 'json':
         package = handlers.request().package
         cherrypy.response.headers['Content-Type'] = 'application/json'
         versions = [str(version.version) for version in package.version_set]
         return json.dumps({
             "name": package.name,
             "uploaders": [uploader.email() for uploader
                           in package.uploaders],
             "versions": versions
         })
     elif format == 'html':
         package = handlers.request().package
         version_count = package.version_set.count()
         title = package.name
         if package.latest_version:
             title = '%s %s' % (package.name, package.latest_version.version)
         return handlers.render(
             "packages/show", package=package,
             versions=package.version_set.order('-sort_order').fetch(10),
             version_count=version_count,
             show_versions_link=version_count > 10,
             is_uploader=users.get_current_user() in package.uploaders,
             layout={'title': title})
     else:
         raise handlers.http_error(404)
Пример #3
0
    def new(self, package_id, format="html", **kwargs):
        """Retrieve the form for uploading a package version.

        If the user isn't logged in, this presents a login screen. If they are
        logged in but don't have admin priviliges or don't own the package, this
        redirects to /packages/.

        This accepts arbitrary keyword arguments to support OAuth.
        """
        is_json = format == "json"
        user = handlers.get_current_user()

        package = handlers.request().maybe_package
        if not user:
            if is_json:
                handlers.http_error(403, "OAuth authentication failed.")
            else:
                raise cherrypy.HTTPRedirect(users.create_login_url(cherrypy.url()))
        elif package and user not in package.uploaders:
            message = "You aren't an uploader for package '%s'." % package.name
            if is_json:
                handlers.http_error(403, message)
            else:
                handlers.flash(message)
                raise cherrypy.HTTPRedirect("/packages/%s" % package.name)
        elif not handlers.is_current_user_admin():
            message = "Currently only admins may create packages."
            if is_json:
                handlers.http_error(403, message)
            else:
                handlers.flash(message)
                raise cherrypy.HTTPRedirect("/packages")
        elif PrivateKey.get() is None:
            if is_json:
                handlers.http_error(500, "No private key set.")
            else:
                raise cherrypy.HTTPRedirect("/admin#tab-private-key")

        id = str(uuid4())
        redirect_url = handlers.request().url(action="create", id=id)
        upload = cloud_storage.Upload(
            "tmp/" + id, acl="project-private", size_range=(0, Package.MAX_SIZE), success_redirect=redirect_url
        )

        # If the package hasn't been validated and moved out of tmp in five
        # minutes, delete it. This could happen if the user uploads the package
        # to cloud storage, but closes the browser before "create" is run.
        deferred.defer(self._remove_tmp_package, id, _countdown=5 * 60)

        if is_json:
            return upload.to_json()

        if package is not None:
            title = "Upload a new version of %s" % package.name
        else:
            title = "Upload a new package"

            return handlers.render(
                "packages/versions/new", form=upload.to_form(), package=package, layout={"title": title}
            )
Пример #4
0
 def show(self, id, format="html"):
     """Retrieve the page describing a specific package."""
     if format == "json":
         package = handlers.request().package
         versions = [str(version.version) for version in package.version_set]
         return json.dumps(
             {
                 "name": package.name,
                 "uploaders": [uploader.email() for uploader in package.uploaders],
                 "versions": versions,
             }
         )
     elif format == "html":
         package = handlers.request().package
         version_count = package.version_set.count()
         title = package.name
         if package.latest_version:
             title = "%s %s" % (package.name, package.latest_version.version)
         return handlers.render(
             "packages/show",
             package=package,
             versions=package.version_set.order("-sort_order").fetch(10),
             version_count=version_count,
             show_versions_link=version_count > 10,
             is_uploader=users.get_current_user() in package.uploaders,
             layout={"title": title},
         )
     else:
         raise handlers.http_error(404)
Пример #5
0
    def show(self, id, format='html'):
        """Retrieve the page describing a specific package."""
        if format == 'json':
            package = handlers.request().package
            versions = [str(version.version) for version in package.version_set]
            return json.dumps({
                "name": package.name,
                "uploaders": [uploader.email() for uploader
                              in package.uploaders],
                "versions": versions
            })
        elif format == 'html':
            package = handlers.request().package
            version_count = package.version_set.count()

            title = package.name
            readme = None
            readme_filename = None
            if package.latest_version:
                title = '%s %s' % (package.name, package.latest_version.version)
                if package.latest_version.readme:
                    readme = package.latest_version.readme.render()
                    readme_filename = package.latest_version.readme.filename;

            return handlers.render(
                "packages/show", package=package,
                versions=package.version_set.order('-sort_order').fetch(10),
                version_count=version_count,
                show_versions_link=version_count > 10,
                readme=readme,
                readme_filename=readme_filename,
                layout={'title': title})
        else:
            raise handlers.http_error(404)
Пример #6
0
    def show(self, id, format='html'):
        """Retrieve the page describing a specific package."""
        if format == 'json':
            package = handlers.request().package
            versions = [
                str(version.version) for version in package.version_set
            ]
            return json.dumps({
                "name":
                package.name,
                "uploaders":
                [uploader.email() for uploader in package.uploaders],
                "versions":
                versions
            })
        elif format == 'html':
            package = handlers.request().package
            version_count = package.version_set.count()

            title = package.name
            readme = None
            readme_filename = None
            changelog = None
            changelog_filename = None
            if package.latest_version:
                title = '%s %s' % (package.name,
                                   package.latest_version.version)
                if package.latest_version.readme:
                    readme = package.latest_version.readme.render()
                    readme_filename = package.latest_version.readme.filename
                if package.latest_version.changelog:
                    changelog = package.latest_version.changelog.render()
                    changelog_filename = \
                            package.latest_version.changelog.filename

            return handlers.render(
                "packages/show",
                package=package,
                versions=package.version_set.order('-sort_order').fetch(10),
                version_count=version_count,
                show_versions_link=version_count > 10,
                readme=readme,
                readme_filename=readme_filename,
                changelog=changelog,
                changelog_filename=changelog_filename,
                layout={'title': title})
        else:
            raise handlers.http_error(404)
Пример #7
0
    def index(self, page=1, format='html'):
        """Retrieve a paginated list of uploaded packages.

        Arguments:
          page: The page of packages to get. Each page contains 10 packages.
        """
        if format == 'json':
            pager = QueryPager(int(page), "/packages.json?page=%d",
                               Package.all().order('-updated'),
                               per_page=50)
            return json.dumps({
                "packages": [
                    handlers.request().url(action='show', id=package.name)
                    for package in pager.get_items()
                ],
                "prev": pager.prev_url,
                "next": pager.next_url,
                "pages": pager.page_count
            })
        else:
            pager = QueryPager(int(page), "/packages?page=%d",
                               Package.all().order('-updated'))
            title = 'All Packages'
            if page != 1: title = 'Page %s | %s' % (page, title)
            return handlers.render("packages/index",
                                   packages=pager.get_items(),
                                   pagination=pager.render_pagination(),
                                   layout={'title': title})
Пример #8
0
 def index(self, package_id):
     """Retrieve a list of all versions for a given package."""
     package = handlers.request().package
     return handlers.render(
         "packages/versions/index", package=package,
         versions=package.version_set.order('-sort_order').run(),
         layout={'title': 'All versions of %s' % package.name})
Пример #9
0
    def delete(self, package_id, id, format):
        """Delete one of this package's uploaders.

        Only uploaders may delete uploaders. If only one uploader remains, that
        uploader may not be deleted until a new one is added.
        """

        package = handlers.request().package
        if oauth.get_current_user() not in package.uploaders:
            handlers.http_error(
                403, "You aren't an uploader for package '%s'." %
                         package.name)

        user_to_delete = users.User(id)
        if user_to_delete not in package.uploaders:
            handlers.http_error(
                400, "'%s' isn't an uploader for package '%s'." %
                         (user_to_delete.nickname(), package.name))

        if len(package.uploaders) == 1:
            handlers.http_error(
                400, ("Package '%s' only has one uploader, so that uploader " +
                          "can't be removed.") % package.name)

        package.uploaders.remove(user_to_delete)
        package.put()
        return handlers.json_success(
            "'%s' is no longer an uploader for package '%s'." %
                (id, package.name))
Пример #10
0
 def index(self):
     """Retrieves the index of all entrypoints to the API."""
     return json.dumps({
         "packages_url":
         handlers.request().url(controller="api.packages", action="index") +
         "{/package}"
     })
Пример #11
0
    def delete(self, package_id, id, format=None):
        """Delete one of this package's uploaders.

        Only uploaders may delete uploaders. If only one uploader remains, that
        uploader may not be deleted until a new one is added.
        """

        if format: id = id + '.' + format

        package = handlers.request().package
        user_to_delete = users.User(id)
        if not package.has_uploader(user_to_delete):
            handlers.http_error(
                400, "'%s' isn't an uploader for package '%s'." %
                (user_to_delete.nickname(), package.name))

        if len(package.uploaders) == 1:
            handlers.http_error(
                400, ("Package '%s' only has one uploader, so that uploader " +
                      "can't be removed.") % package.name)

        email_to_delete = user_to_delete.email().lower()
        package.uploaders = [
            uploader for uploader in package.uploaders
            if uploader.email().lower() != email_to_delete
        ]
        package.put()
        return handlers.json_success(
            "'%s' is no longer an uploader for package '%s'." %
            (id, package.name))
Пример #12
0
    def delete(self, package_id, id, format):
        """Delete one of this package's uploaders.

        Only uploaders may delete uploaders. If only one uploader remains, that
        uploader may not be deleted until a new one is added.
        """

        package = handlers.request().package
        email = id
        if not package.has_uploader_email(email):
            handlers.http_error(
                400, "'%s' isn't an uploader for package '%s'." %
                         (email, package.name))

        if len(package.uploaderEmails) == 1:
            handlers.http_error(
                400, ("Package '%s' only has one uploader, so that uploader " +
                          "can't be removed.") % package.name)

        email_to_delete = email.lower()
        package.uploaderEmails = [email for email in package.uploaderEmails
                                  if email.lower() != email_to_delete]
        package.put()
        package.invalidate_cache()
        return handlers.json_success(
            "'%s' is no longer an uploader for package '%s'." %
                (id, package.name))
Пример #13
0
    def delete(self, package_id, id, format):
        """Delete one of this package's uploaders.

        Only uploaders may delete uploaders. If only one uploader remains, that
        uploader may not be deleted until a new one is added.
        """

        package = handlers.request().package
        email = id
        if not package.has_uploader_email(email):
            handlers.http_error(
                400, "'%s' isn't an uploader for package '%s'." %
                (email, package.name))

        if len(package.uploaderEmails) == 1:
            handlers.http_error(
                400, ("Package '%s' only has one uploader, so that uploader " +
                      "can't be removed.") % package.name)

        email_to_delete = email.lower()
        package.uploaderEmails = [
            email for email in package.uploaderEmails
            if email.lower() != email_to_delete
        ]
        package.put()
        package.invalidate_cache()
        return handlers.json_success(
            "'%s' is no longer an uploader for package '%s'." %
            (id, package.name))
Пример #14
0
    def index(self, page=1, format='html'):
        """Retrieve a paginated list of uploaded packages.

        Arguments:
          page: The page of packages to get. Each page contains 10 packages.
        """
        if format == 'json':
            pager = Pager(int(page),
                          "/packages.json?page=%d",
                          Package.all().order('-updated'),
                          per_page=50)
            return json.dumps({
                "packages": [
                    handlers.request().url(action='show', id=package.name)
                    for package in pager.get_items()
                ],
                "prev":
                pager.prev_url,
                "next":
                pager.next_url,
                "pages":
                pager.page_count
            })
        else:
            pager = Pager(int(page), "/packages?page=%d",
                          Package.all().order('-updated'))
            title = 'All Packages'
            if page != 1: title = 'Page %s | %s' % (page, title)
            return handlers.render("packages/index",
                                   packages=pager.get_items(),
                                   pagination=pager.render_pagination(),
                                   layout={'title': title})
Пример #15
0
    def delete(self, package_id, id, format):
        """Delete one of this package's uploaders.

        Only uploaders may delete uploaders. If only one uploader remains, that
        uploader may not be deleted until a new one is added.
        """

        package = handlers.request().package
        user_to_delete = users.User(id)
        if not package.has_uploader(user_to_delete):
            handlers.http_error(
                400, "'%s' isn't an uploader for package '%s'." %
                         (user_to_delete.nickname(), package.name))

        if len(package.uploaders) == 1:
            handlers.http_error(
                400, ("Package '%s' only has one uploader, so that uploader " +
                          "can't be removed.") % package.name)

        email_to_delete = user_to_delete.email().lower()
        package.uploaders = [uploader for uploader in package.uploaders
                             if uploader.email().lower() != email_to_delete]
        package.put()
        return handlers.json_success(
            "'%s' is no longer an uploader for package '%s'." %
                (id, package.name))
Пример #16
0
    def create(self, id, **kwargs):
        """Create a new package version.

        This creates a single package version. It will also create all the
        package metadata if that doesn't already exist. The package archive is
        uploaded to cloud storage separately.

        If the user doesn't own the package or isn't logged in with admin
        privileges, this will return a 403. If the package already has a version
        with this number, or if the version is invalid, this will return a 400.

        Arguments:
          id: The id of the package in cloud storage.
        """

        try:
            route = handlers.request().route
            if 'id' in route: del route['id']

            try:
                with closing(cloud_storage.read('tmp/' + id)) as f:
                    version = PackageVersion.from_archive(
                        f, uploaderEmail=handlers.get_oauth_user().email())
            except (KeyError, files.ExistenceError):
                handlers.http_error(
                    403, "Package upload " + id + " does not exist.")

            # If the package for this version already exists, make sure we're an
            # uploader for it. If it doesn't, we're fine to create it anew.
            if version.package.is_saved():
                if not version.package.has_uploader_email(
                    handlers.get_oauth_user().email()):
                    handlers.http_error(
                        403, "You aren't an uploader for package '%s'." %
                                 version.package.name)
                elif version.package.has_version(version.version):
                    message = 'Package "%s" already has version "%s".' % \
                        (version.package.name, version.version)
                    handlers.http_error(400, message)

                if self._should_update_latest_version(version):
                    version.package.latest_version = version
            else:
                version.package.latest_version = version

            cloud_storage.modify_object(version.storage_path,
                                        acl='public-read',
                                        copy_source='tmp/' + id)

            with models.transaction():
                version.package.put()
                version.put()
                version.package.invalidate_cache()

            deferred.defer(self._compute_version_order, version.package.name)

            return handlers.json_success('%s %s uploaded successfully.' %
                (version.package.name, version.version))
        finally:
            cloud_storage.delete_object('tmp/' + id)
Пример #17
0
    def create(self, id, **kwargs):
        """Create a new package version.

        This creates a single package version. It will also create all the
        package metadata if that doesn't already exist. The package archive is
        uploaded to cloud storage separately.

        If the user doesn't own the package or isn't logged in with admin
        privileges, this will return a 403. If the package already has a version
        with this number, or if the version is invalid, this will return a 400.

        Arguments:
          id: The id of the package in cloud storage.
        """

        try:
            route = handlers.request().route
            if 'id' in route: del route['id']

            try:
                with closing(cloud_storage.read('tmp/' + id)) as f:
                    version = PackageVersion.from_archive(
                        f, uploader=handlers.get_oauth_user())
            except (KeyError, files.ExistenceError):
                handlers.http_error(
                    403, "Package upload " + id + " does not exist.")

            # If the package for this version already exists, make sure we're an
            # uploader for it. If it doesn't, we're fine to create it anew.
            if version.package.is_saved():
                if not version.package.has_uploader(handlers.get_oauth_user()):
                    handlers.http_error(
                        403, "You aren't an uploader for package '%s'." %
                        version.package.name)
                elif version.package.has_version(version.version):
                    message = 'Package "%s" already has version "%s".' % \
                        (version.package.name, version.version)
                    handlers.http_error(400, message)

                if self._should_update_latest_version(version):
                    version.package.latest_version = version
            else:
                version.package.latest_version = version

            cloud_storage.modify_object(version.storage_path,
                                        acl='public-read',
                                        copy_source='tmp/' + id)

            with models.transaction():
                version.package.put()
                version.put()

            deferred.defer(self._compute_version_order, version.package.name)

            return handlers.json_success(
                '%s %s uploaded successfully.' %
                (version.package.name, version.version))
        finally:
            cloud_storage.delete_object('tmp/' + id)
Пример #18
0
    def show(self, package_id, id, format):
        """Retrieve the page describing a package version.

        Depending on the format, this could be a user-readable webpage (.html),
        a machine-readable YAML document (.yaml), or a download of the actual
        package blob (.tar.gz).
        """

        # The built-in format parsing has trouble with versions since they
        # contain periods, so we have to undo it and apply our own.
        id = '%s.%s' % (id, format)
        if id.endswith('.tar.gz'):
            id = id[0:-len('.tar.gz')]
            version = handlers.request().package_version(id)
            deferred.defer(self._count_download, version.key())
            raise cherrypy.HTTPRedirect(version.download_url)
        elif id.endswith('.zip'):
            id = id[0:-len('.zip')]
            version = handlers.request().package_version(id)
            deferred.defer(self._count_download, version.key())

            filename = 'packages/{}-{}.tar.gz'.format(package_id, id)
            zipfilename = os.path.basename(filename).replace('tar.gz', 'zip')

            cherrypy.response.headers['Content-Type'] = \
                'application/octet-stream'
            cherrypy.response.headers['Content-Disposition'] = \
                'attachment; filename=%s' % zipfilename

            # Read the .tar.gz file from cloud storage into memory, and create
            # an uncompressed zip from it.
            try:
                with cloud_storage.open(filename) as tar_fileobj:
                    bytes = io.BytesIO()
                    tar = tarfile.open(fileobj=tar_fileobj)
                    zip = zipfile.ZipFile(bytes, mode='w')

                    for file_info in tar:
                        zip.writestr(file_info.name, \
                            tar.extractfile(file_info).read())
                    zip.close()

                    bytes.seek(0)
                    return bytes.read()
            except KeyError, ExistenceError:
                handlers.http_error(404)
Пример #19
0
 def index(self, package_id):
     """Retrieve a list of all versions for a given package."""
     package = handlers.request().package
     return handlers.render(
         "packages/versions/index",
         package=package,
         versions=package.version_set.order('-sort_order').run(),
         layout={'title': 'All versions of %s' % package.name})
Пример #20
0
    def new_dartdoc(self, package_id, id, format):
        """Retrieve the form for uploading dartdoc for this package version."""
        version = handlers.request().package_version(id)
        upload = cloud_storage.Upload(version.dartdoc_storage_path,
                                      acl='public-read',
                                      size_range=(0, Package.MAX_SIZE))

        return upload.to_json()
Пример #21
0
    def new_dartdoc(self, package_id, id):
        """Retrieve the form for uploading dartdoc for this package version."""
        version = handlers.request().package_version(id)
        upload = cloud_storage.Upload(version.dartdoc_storage_path,
                                      acl='public-read',
                                      size_range=(0, Package.MAX_SIZE))

        return upload.to_json()
Пример #22
0
 def show(self, package_id, id, format=None):
     """Retrieve the document describing a package version."""
     # The mapper expects anything past a period to be the format of the
     # document, which is fine for "index.html" or "packages.json" but not
     # for "1.2.3". It thinks "3" is the format, which is wrong, so we add it
     # on here.
     if format: id = id + '.' + format
     return json.dumps(
         handlers.request().package_version(id).as_dict(full=True))
Пример #23
0
    def new_dartdoc(self, package_id, id, format):
        """Retrieve the form for uploading dartdoc for this package version."""
        if PrivateKey.get() is None:
            handlers.http_error(500, 'No private key set.')

        version = handlers.request().package_version(id)
        upload = cloud_storage.Upload(version.dartdoc_storage_path,
                                      acl='public-read',
                                      size_range=(0, Package.MAX_SIZE))

        return upload.to_json()
Пример #24
0
    def new_dartdoc(self, package_id, id, format):
        """Retrieve the form for uploading dartdoc for this package version."""
        if PrivateKey.get() is None:
            handlers.http_error(500, 'No private key set.')

        version = handlers.request().package_version(id)
        upload = cloud_storage.Upload(version.dartdoc_storage_path,
                                      acl='public-read',
                                      size_range=(0, Package.MAX_SIZE))

        return upload.to_json()
Пример #25
0
    def show(self, package_id, id, format):
        """Retrieve the page describing a package version.

        Depending on the format, this could be a user-readable webpage (.html),
        a machine-readable YAML document (.yaml), or a download of the actual
        package blob (.tar.gz).
        """

        # The built-in format parsing has trouble with versions since they
        # contain periods, so we have to undo it and apply our own.
        id = '%s.%s' % (id, format)
        if id.endswith('.tar.gz'):
            id = id[0:-len('.tar.gz')]
            version = handlers.request().package_version(id)
            raise cherrypy.HTTPRedirect(version.download_url)
        elif id.endswith('.yaml'):
            id = id[0:-len('.yaml')]
            version = handlers.request().package_version(id)
            cherrypy.response.headers['Content-Type'] = 'text/yaml'
            return version.pubspec.to_yaml()
        else:
            handlers.http_error(404)
Пример #26
0
def _error_page(status, message, traceback, version):
    # Don't show tracebacks to end users.
    if not handlers.is_dev_server() and not users.is_current_user_admin():
        traceback = None

    if handlers.request().is_json:
        handlers.JsonError(status, message).set_response()
        return cherrypy.response.body

    return str(handlers.render('error',
        status=status,
        message=message,
        traceback=traceback,
        layout={'title': 'Error %s' % status}))
Пример #27
0
def _error_page(status, message, traceback, version):
    # Don't show tracebacks to end users.
    if not handlers.is_dev_server() and not users.is_current_user_admin():
        traceback = None

    if handlers.request().is_json:
        cherrypy.response.headers['Content-Type'] = 'application/json'
        return json.dumps({'error': {'message': message}})

    return str(handlers.render('error',
        status=status,
        message=message,
        traceback=traceback,
        layout={'title': 'Error %s' % status}))
Пример #28
0
    def show(self, package_id, id, format):
        """Retrieve the page describing a package version.

        Depending on the format, this could be a user-readable webpage (.html),
        a machine-readable YAML document (.yaml), or a download of the actual
        package blob (.tar.gz).
        """

        # The built-in format parsing has trouble with versions since they
        # contain periods, so we have to undo it and apply our own.
        id = "%s.%s" % (id, format)
        if id.endswith(".tar.gz"):
            id = id[0 : -len(".tar.gz")]
            version = handlers.request().package_version(id)
            deferred.defer(self._count_download, version.key())
            raise cherrypy.HTTPRedirect(version.download_url)
        elif id.endswith(".yaml"):
            id = id[0 : -len(".yaml")]
            version = handlers.request().package_version(id)
            cherrypy.response.headers["Content-Type"] = "text/yaml"
            return version.pubspec.to_yaml()
        else:
            handlers.http_error(404)
Пример #29
0
def _error_page(status, message, traceback, version):
    # Don't show tracebacks to end users.
    if not handlers.is_dev_server() and not users.is_current_user_admin():
        traceback = None

    if handlers.request().is_json:
        handlers.JsonError(status, message).set_response()
        return cherrypy.response.body

    return str(
        handlers.render('error',
                        status=status,
                        message=message,
                        traceback=traceback,
                        layout={'title': 'Error %s' % status}))
Пример #30
0
    def new(self, package_id, format='json', **kwargs):
        """Retrieve the form for uploading a package version.

        This accepts arbitrary keyword arguments to support OAuth.
        """
        id = str(uuid4())
        redirect_url = handlers.request().url(action="create", id=id)
        upload = cloud_storage.Upload("tmp/" + id, acl="project-private",
                                      size_range=(0, Package.MAX_SIZE),
                                      success_redirect=redirect_url)

        # If the package hasn't been validated and moved out of tmp in five
        # minutes, delete it. This could happen if the user uploads the package
        # to cloud storage, but closes the browser before "create" is run.
        deferred.defer(self._remove_tmp_package, id, _countdown=5*60)

        return upload.to_json()
Пример #31
0
    def create(self, package_id, email):
        """Add a new uploader for this package.

        Only other uploaders may add new uploaders."""

        package = handlers.request().package
        user_to_add = users.User(email)
        if package.has_uploader(user_to_add):
            handlers.http_error(
                400, "User '%s' is already an uploader for package '%s'." %
                (email, package.name))

        package.uploaders.append(user_to_add)
        package.put()
        return handlers.json_success(
            "'%s' added as an uploader for package '%s'." %
            (email, package.name))
Пример #32
0
    def create(self, package_id, format, email):
        """Add a new uploader for this package.

        Only other uploaders may add new uploaders."""

        package = handlers.request().package
        if package.has_uploader_email(email):
            handlers.http_error(
                400, "User '%s' is already an uploader for package '%s'." %
                (email, package.name))

        package.uploaderEmails.append(email)
        package.put()
        package.invalidate_cache()
        return handlers.json_success(
            "'%s' added as an uploader for package '%s'." %
            (email, package.name))
Пример #33
0
    def create(self, package_id, format, email):
        """Add a new uploader for this package.

        Only other uploaders may add new uploaders."""

        package = handlers.request().package
        if package.has_uploader_email(email):
            handlers.http_error(
                400, "User '%s' is already an uploader for package '%s'." %
                         (email, package.name))

        package.uploaderEmails.append(email)
        package.put()
        package.invalidate_cache()
        return handlers.json_success(
            "'%s' added as an uploader for package '%s'." %
                (email, package.name))
Пример #34
0
    def new(self, format=''):
        """Retrieve the form for uploading a package version.

        This accepts arbitrary keyword arguments to support OAuth.
        """
        id = str(uuid4())
        redirect_url = handlers.request().url(action="create", id=id)
        upload = cloud_storage.Upload("tmp/" + id,
                                      acl="project-private",
                                      size_range=(0, Package.MAX_SIZE),
                                      success_redirect=redirect_url)

        # If the package hasn't been validated and moved out of tmp in five
        # minutes, delete it. This could happen if the user uploads the package
        # to cloud storage, but closes the browser before "create" is run.
        deferred.defer(self._remove_tmp_package, id, _countdown=5 * 60)

        return upload.to_json()
Пример #35
0
    def create(self, package_id, format, email):
        """Add a new uploader for this package.

        Only other uploaders may add new uploaders."""

        package = handlers.request().package
        if oauth.get_current_user() not in package.uploaders:
            handlers.http_error(
                403, "You aren't an uploader for package '%s'." %
                         package.name)

        user_to_add = users.User(email)
        if user_to_add in package.uploaders:
            handlers.http_error(
                400, "User '%s' is already an uploader for package '%s'." %
                         (email, package.name))

        package.uploaders.append(user_to_add)
        package.put()
        return handlers.json_success(
            "'%s' added as an uploader for package '%s'." %
                (email, package.name))
Пример #36
0
 def show(self, id):
     """Retrieve the page describing a specific package."""
     return json.dumps(handlers.request().package.as_dict(full=True))
Пример #37
0
 def show(self, package_id, id, format):
     """Retrieve the document describing a package version."""
     # The mapper thinks the final version digit is the format.
     id = id + '.' + format
     return json.dumps(
         handlers.request().package_version(id).as_dict(full=True))
Пример #38
0
    def create(self, package_id, id, format='html', **kwargs):
        """Create a new package version.

        This creates a single package version. It will also create all the
        package metadata if that doesn't already exist. The package archive is
        uploaded to cloud storage separately.

        If the user doesn't own the package or isn't logged in with admin
        privileges, this will return a 403. If the package already has a version
        with this number, or if the version is invalid, this will redirect to
        the new version form.

        Arguments:
          id: The id of the package in cloud storage.
        """

        try:
            is_json = format == 'json'

            route = handlers.request().route
            if 'id' in route: del route['id']

            package = handlers.request().maybe_package
            if package and handlers.get_current_user() not in package.uploaders:
                handlers.request().error(
                    403, "You aren't an uploader for package '%s'." %
                             package.name)
            elif not handlers.is_current_user_admin():
                handlers.request().error(
                    403, "Only admins may create packages.")

            try:
                with closing(cloud_storage.read('tmp/' + id)) as f:
                    version = PackageVersion.from_archive(
                        f, uploader=handlers.get_current_user())
            except (KeyError, files.ExistenceError):
                handlers.request().error(
                    403, "Package upload " + id + " does not exist.")

            if version.package.is_saved():
                if handlers.get_current_user() not in version.package.uploaders:
                    handlers.request().error(
                        403, "You aren't an uploader for package '%s'." %
                                 version.package.name)
                elif version.package.has_version(version.version):
                    message = 'Package "%s" already has version "%s".' % \
                        (version.package.name, version.version)
                    if is_json: handlers.json_error(400, message)

                    handlers.flash(message)
                    url = handlers.request().url(
                        action='new', package_id=version.package.name)
                    raise cherrypy.HTTPRedirect(url)

                if self._should_update_latest_version(version):
                    version.package.latest_version = version
            else:
                version.package.latest_version = version

            cloud_storage.modify_object(version.storage_path,
                                        acl='public-read',
                                        copy_source='tmp/' + id)

            with models.transaction():
                version.package.put()
                version.put()

            deferred.defer(self._compute_version_order, version.package.name)

            message = '%s %s uploaded successfully.' % \
                (version.package.name, version.version)
            if is_json:
                cherrypy.response.headers['Content-Type'] = 'application/json'
                return json.dumps({"success": {"message": message}})

            handlers.flash(message)
            raise cherrypy.HTTPRedirect('/packages/%s' % version.package.name)
        finally:
            cloud_storage.delete_object('tmp/' + id)
Пример #39
0
 def show(self, package_id, id, format):
     """Retrieve the document describing a package version."""
     # The mapper thinks the final version digit is the format.
     id = id + '.' + format
     return json.dumps(
         handlers.request().package_version(id).as_dict(full=True))
Пример #40
0
 def index(self):
     """Retrieves the index of all entrypoints to the API."""
     return json.dumps({
         "packages_url": handlers.request().url(
             controller="api.packages", action="index") + "{/package}"
     })
Пример #41
0
 def show(self, id):
     """Retrieve the page describing a specific package."""
     return handlers.request().package.as_json()
Пример #42
0
 def show(self, id):
     """Retrieve the page describing a specific package."""
     return handlers.request().package.as_json()
Пример #43
0
    def __init__(self, obj, lifetime=10*60, acl=None, cache_control=None,
                 content_disposition=None, content_encoding=None,
                 content_type=None, expires=None, success_redirect=None,
                 success_status=None, size_range=None, metadata={}):
        """Create a new Upload.

        Most arguments are identical to the form fields listed in
        https://developers.google.com/storage/docs/reference-methods#postobject, but
        there are a few differences:

        * The expires argument takes a number of seconds since the epoch.
        * The key argument only specifies the key name, not the bucket.
        * The metadata argument is a dictionary of metadata header names to values.
          Each one is transformed into an x-goog-meta- field. The keys should not
          include "x-goog-meta-". Null values are ignored.
        * The policy document is automatically created and signed. It ensures that
          all fields have the assigned values when they're submitted to Cloud
          Storage.

        The lifetime argument specifies how long the form is valid. It defaults to
        ten minutes.

        The size_range argument should be a tuple indicating the lower and upper
        bounds on the size of the uploaded file, in bytes.
        """

        obj = _object_path(obj)

        metadata = {'x-goog-meta-' + key: value for key, value
                    in metadata.iteritems()}
        if expires is not None: expires = _iso8601(expires)

        policy = {}
        policy['expiration'] = _iso8601(time.time() + lifetime)
        policy['conditions'] = [{'key': obj}]
        def _try_add_condition(name, value):
            if value is not None: policy['conditions'].append({name: value})
        _try_add_condition('acl', acl)
        _try_add_condition('cache-control', cache_control)
        _try_add_condition('content-disposition', content_disposition)
        _try_add_condition('content-encoding', content_encoding)
        _try_add_condition('content-type', content_type)
        _try_add_condition('expires', expires)
        _try_add_condition('success_action_redirect', success_redirect)
        _try_add_condition('success_action_status', success_status)
        for key, value in metadata.items(): _try_add_condition(key, value)
        if size_range is not None:
            policy['conditions'].append(
                ['content-length-range', size_range[0], size_range[1]])
        policy = b64encode(json.dumps(policy))
        signature = PrivateKey.sign(policy)

        self._fields = {'key': obj,
                       'acl': acl,
                       'Cache-Control': cache_control,
                       'Content-Disposition': content_disposition,
                       'Content-Encoding': content_encoding,
                       'Content-Type': content_type,
                       'expires': expires,
                       'GoogleAccessId': _ACCESS_KEY,
                       'policy': policy,
                       'signature': signature,
                       'success_action_redirect': success_redirect,
                       'success_action_status': success_status}
        self._fields.update(metadata)

        if handlers.is_production():
            self._url = "https://storage.googleapis.com"
        else:
            self._url = handlers.request().url(
                controller="versions", action="upload")