Пример #1
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))
Пример #2
0
 def create(self, key):
     """Set the private key."""
     if not users.is_current_user_admin():
         handlers.http_error(403, "Only admins may set the private key.")
     PrivateKey.set(key)
     handlers.flash("Private key set successfully.")
     raise cherrypy.HTTPRedirect("/admin#tab-private-key")
Пример #3
0
    def show(self, filename):
        """Display a documentation page.

        Each page is static HTML wrapped in a dynamic layout. The HTML is
        generated offline from Markdown source files in /doc; the titles are
        loaded from those source files as well.
        """

        # Redirect from the old names for the commands.
        if filename == 'pub-install.html':
            raise cherrypy.HTTPRedirect('/doc/pub-get.html')

        if filename == 'pub-update.html':
            raise cherrypy.HTTPRedirect('/doc/pub-upgrade.html')

        if filename == '': filename = 'index.html'
        root = os.path.join(os.path.dirname(__file__), '..')

        html_path = os.path.join(root, 'views', 'doc', filename)
        if not os.path.isfile(html_path):
            handlers.http_error(404)

        markdown_filename = re.sub("\.html$", ".markdown", filename)
        markdown_path = os.path.join(root, 'doc', markdown_filename)
        with codecs.open(markdown_path, encoding='utf-8') as f:
            frontmatter = self._frontmatter(f)

        with codecs.open(html_path, encoding='utf-8') as f:
            html = """<article>
                <h1>%s</h1>
                %s
                </article>""" % (frontmatter['title'], f.read())
            return handlers.layout(html, title=frontmatter['title'])
Пример #4
0
    def show(self, filename):
        """Display a documentation page.

        Each page is static HTML wrapped in a dynamic layout. The HTML is
        generated offline from Markdown source files in /doc; the titles are
        loaded from those source files as well.
        """

        if filename == "":
            filename = "index.html"
        root = os.path.join(os.path.dirname(__file__), "..")

        html_path = os.path.join(root, "views", "doc", filename)
        if not os.path.isfile(html_path):
            handlers.http_error(404)

        markdown_filename = re.sub("\.html$", ".markdown", filename)
        markdown_path = os.path.join(root, "doc", markdown_filename)
        with codecs.open(markdown_path, encoding="utf-8") as f:
            frontmatter = self._frontmatter(f)

        with codecs.open(html_path, encoding="utf-8") as f:
            html = """<article>
                <h1>%s</h1>
                %s
                </article>""" % (
                frontmatter["title"],
                f.read(),
            )
            return handlers.layout(html, title=frontmatter["title"])
Пример #5
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))
Пример #6
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))
Пример #7
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))
Пример #8
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)
Пример #9
0
    def show(self, filename):
        """Display a documentation page.

        Each page is static HTML wrapped in a dynamic layout. The HTML is
        generated offline from Markdown source files in /doc; the titles are
        loaded from those source files as well.
        """

        if filename == '': filename = 'index.html'
        root = os.path.join(os.path.dirname(__file__), '..')

        html_path = os.path.join(root, 'views', 'doc', filename)
        if not os.path.isfile(html_path):
            handlers.http_error(404)

        markdown_filename = re.sub("\.html$", ".markdown", filename)
        markdown_path = os.path.join(root, 'doc', markdown_filename)
        with codecs.open(markdown_path, encoding='utf-8') as f:
            frontmatter = self._frontmatter(f)

        with codecs.open(html_path, encoding='utf-8') as f:
            html = """<article>
                <h1>%s</h1>
                %s
                </article>""" % (frontmatter['title'], f.read())
            return handlers.layout(html, title=frontmatter['title'])
Пример #10
0
    def upload(
        self, package_id, file, key, acl=None, policy=None, signature=None, success_action_redirect=None, **kwargs
    ):
        """A development-only action for uploading a package archive.

        In production, package archives are uploaded directly to cloud storage,
        using a signed form for authentication. The signed form doesn't work for
        the development server, since it uses a local database in place of cloud
        storage, so this action emulates it by manually saving the file to the
        development database.
        """

        if handlers.is_production():
            raise handlers.http_error(404)
        if PrivateKey.sign(policy) != signature:
            raise handlers.http_error(403)

        write_path = files.gs.create("/gs/" + key, acl=acl)
        with files.open(write_path, "a") as f:
            f.write(file.file.read())
        files.finalize(write_path)

        if success_action_redirect:
            raise cherrypy.HTTPRedirect(success_action_redirect)
        cherrypy.response.status = 204
        return ""
Пример #11
0
    def upload(self,
               file,
               key,
               acl=None,
               policy=None,
               signature=None,
               success_action_redirect=None,
               **kwargs):
        """A development-only action for uploading a package archive.

        In production, package archives are uploaded directly to cloud storage,
        using a signed form for authentication. The signed form doesn't work for
        the development server, since it uses a local database in place of cloud
        storage, so this action emulates it by manually saving the file to the
        development database.
        """

        if handlers.is_production(): raise handlers.http_error(404)
        if PrivateKey.sign(policy) != signature: raise handlers.http_error(403)

        write_path = files.gs.create('/gs/' + key, acl=acl)
        with files.open(write_path, 'a') as f:
            f.write(file.file.read())
        files.finalize(write_path)

        if success_action_redirect:
            raise cherrypy.HTTPRedirect(success_action_redirect)
        cherrypy.response.status = 204
        return ""
Пример #12
0
 def create(self, key):
     """Set the private key."""
     if not users.is_current_user_admin():
         handlers.http_error(403, "Only admins may set the private key.")
     PrivateKey.set(key)
     handlers.flash("Private key set successfully.")
     raise cherrypy.HTTPRedirect("/admin#tab-private-key")
Пример #13
0
 def reload(self, package_id):
     """Reload all package versions from their tarballs."""
     if not users.is_current_user_admin(): handlers.http_error(403)
     versions_to_reload = 0
     for key in PackageVersion.all(keys_only=True).run():
         versions_to_reload += 1
         deferred.defer(self._reload_version, key)
     memcache.set('versions_to_reload', versions_to_reload)
     memcache.set('versions_reloaded', 0)
     raise cherrypy.HTTPRedirect('/admin#tab-packages')
Пример #14
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()
Пример #15
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()
Пример #16
0
    def reload(self):
        """Reload all package versions from their tarballs."""
        if not handlers.is_current_user_dogfooder(): handlers.http_error(403)
        query = PackageVersion.all(keys_only=True)
        memcache.set('versions_to_reload', query.count())
        memcache.set('versions_reloaded', 0)

        for key in query.run():
            name = 'reload-%s-%s' % (int(time.time()), key)
            deferred.defer(self._reload_version, key, _name=name)
        raise cherrypy.HTTPRedirect('/admin#tab-packages')
Пример #17
0
    def reload(self, package_id):
        """Reload all package versions from their tarballs."""
        if not handlers.is_current_user_dogfooder(): handlers.http_error(403)
        query = PackageVersion.all(keys_only=True)
        memcache.set('versions_to_reload', query.count())
        memcache.set('versions_reloaded', 0)

        for key in query.run():
            name = 'reload-%s-%s' % (int(time.time()), key)
            deferred.defer(self._reload_version, key, _name=name)
        raise cherrypy.HTTPRedirect('/admin#tab-packages')
Пример #18
0
    def serve(self, filename):
        """Serves a cloud storage file for the development server."""

        if handlers.is_production(): return handlers.http_error(404)

        cherrypy.response.headers['Content-Type'] = 'application/octet-stream'
        cherrypy.response.headers['Content-Disposition'] = \
            'attachment; filename=%s' % os.path.basename(filename)

        try:
            with cloud_storage.open(filename) as f: return f.read()
        except KeyError, ExistenceError:
            handlers.http_error(404)
Пример #19
0
    def reload_status(self, format):
        """Return the status of the current package reload.

        This is a JSON map. If the reload is finished, it will contain only a
        'done' key with value true. If the reload is in progress, it will
        contain 'count' and 'total' keys, indicating the total number of
        packages to reload and the number that have been reloaded so far,
        respectively.
        """
        if not users.is_current_user_admin():
            handlers.http_error(403, "Permission denied.")
        reload_status = PackageVersion.get_reload_status()
        return json.dumps(reload_status or {'done': True})
Пример #20
0
    def reload_status(self, package_id, format):
        """Return the status of the current package reload.

        This is a JSON map. If the reload is finished, it will contain only a
        'done' key with value true. If the reload is in progress, it will
        contain 'count' and 'total' keys, indicating the total number of
        packages to reload and the number that have been reloaded so far,
        respectively.
        """
        if not users.is_current_user_admin():
            handlers.http_error(403, "Permission denied.")
        reload_status = PackageVersion.get_reload_status()
        return json.dumps(reload_status or {'done': True})
Пример #21
0
    def serve(self, filename):
        """Serves a cloud storage file for the development server."""

        if handlers.is_production(): return handlers.http_error(404)

        cherrypy.response.headers['Content-Type'] = 'application/octet-stream'
        cherrypy.response.headers['Content-Disposition'] = \
            'attachment; filename=%s' % os.path.basename(filename)

        try:
            with cloud_storage.open(filename) as f: return f.read()
        except KeyError, ExistenceError:
            handlers.http_error(404)
Пример #22
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}
            )
Пример #23
0
    def show(self, path):
        """Redirect to a documentation page on dartlang.org.

        Pub used to host its own docs, but no longer does. These redirects
        allow existing links to the docs to map to the new ones.
        """

        # Only redirect the known paths.
        try:
            redirect = _REDIRECTS[path]
            raise cherrypy.HTTPRedirect(
                'https://www.dartlang.org/tools/pub/' + redirect)
        except KeyError:
            handlers.http_error(404)
Пример #24
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)
Пример #25
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)
Пример #26
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)
Пример #27
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))
Пример #28
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))
Пример #29
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))
Пример #30
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)
Пример #31
0
    def _query_search(self, query, page):
        """Call the Custom Search API with the given query and get the given
        page of results.

        Return a tuple. The first element is the list of search results for the
        requested page. Each result is a map that can be passed to the search
        result template. The second element is the total count of search
        results. The count will always be the total count of all results
        starting from the beginning, regardless of the page being requested.

        For each search result, this looks up the actual package in the data
        store and returns its live metadata. If the URL cannot be parsed or
        the package cannot be found in the datastore, it is not included in the
        results.

        Arguments:
          query: The search query to perform.
          page: The page of results to return. If out of bounds, returns an
            empty collection of results.
        """
        global _search_service

        if page < 1 or page > MAX_RESULTS / RESULTS_PER_PAGE:
            handlers.http_error(400, "Page \"%d\" is out of bounds." % page)

        start = (page - 1) * RESULTS_PER_PAGE + 1

        results = []
        if _mock_resource:
            resource = _mock_resource
        else:
            if not _search_service:
                _search_service = build("customsearch", "v1",
                                        developerKey=PrivateKey.get_api())
            resource = _search_service.cse().list(q=query, cx=CUSTOM_SEARCH_ID,
                num=RESULTS_PER_PAGE, start=start).execute()

        if "items" in resource:
            for item in resource["items"]:
                result = self._get_result(item)
                if result: results.append(result)

        count = int(resource["searchInformation"]["totalResults"])
        return results, count
Пример #32
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.
        """
        if PrivateKey.get() is None:
            handlers.http_error(500, 'No private key set.')

        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()
Пример #33
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))
Пример #34
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)
Пример #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 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)
Пример #37
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)
Пример #38
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)
Пример #39
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)
Пример #40
0
    def admin(self):
        """Retrieve a page for performing administrative tasks."""

        if not users.get_current_user():
            raise cherrypy.HTTPRedirect(users.create_login_url(cherrypy.url()))
        elif not users.is_current_user_admin():
            raise handlers.http_error(403)

        reload_status = PackageVersion.get_reload_status()
        if reload_status is not None:
            reload_status['percentage'] = '%d%%' % (
                100.0 * reload_status['count'] / reload_status['total'])

        return handlers.render('admin',
               reload_status=reload_status,
               private_keys_set=PrivateKey.get_oauth() is not None,
               production=handlers.is_production(),
               layout={'title': 'Admin Console'})
Пример #41
0
    def admin(self):
        """Retrieve a page for performing administrative tasks."""

        if not users.get_current_user():
            raise cherrypy.HTTPRedirect(users.create_login_url(cherrypy.url()))
        elif not users.is_current_user_admin():
            raise handlers.http_error(403)

        reload_status = PackageVersion.get_reload_status()
        if reload_status is not None:
            reload_status['percentage'] = '%d%%' % (
                100.0 * reload_status['count'] / reload_status['total'])

        return handlers.render('admin',
               reload_status=reload_status,
               private_keys_set=PrivateKey.get_oauth() is not None,
               production=handlers.is_production(),
               layout={'title': 'Admin Console'})
Пример #42
0
    def admin(self):
        """Retrieve a page for performing administrative tasks."""

        if not users.get_current_user():
            raise cherrypy.HTTPRedirect(users.create_login_url(cherrypy.url()))
        elif not users.is_current_user_admin():
            raise handlers.http_error(403)

        reload_status = PackageVersion.get_reload_status()
        if reload_status is not None:
            reload_status["percentage"] = "%d%%" % (100.0 * reload_status["count"] / reload_status["total"])

        return handlers.render(
            "admin",
            reload_status=reload_status,
            private_key_set=PrivateKey.get() is not None,
            production=handlers.is_production(),
            layout={"title": "Admin Console"},
        )
Пример #43
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.http_error(403, "You aren't an uploader for package '%s'." % package.name)
            elif not handlers.is_current_user_admin():
                handlers.http_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.http_error(403, "Package upload " + id + " does not exist.")

            if version.package.is_saved():
                if handlers.get_current_user() not in version.package.uploaders:
                    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)
                    if is_json:
                        handlers.http_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:
                return handlers.json_success(message)

            handlers.flash(message)
            raise cherrypy.HTTPRedirect("/packages/%s" % version.package.name)
        finally:
            cloud_storage.delete_object("tmp/" + id)
Пример #44
0
                        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)

        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)

    def _count_download(self, key):
        """Increment the download count for a package version."""
        with models.transaction():
            version = PackageVersion.get(key)
            version.downloads += 1
            version.package.downloads += 1
            version.put()
            version.package.put()

    @handlers.json_action
    @handlers.requires_private_key
    @handlers.requires_uploader
    def new_dartdoc(self, package_id, id, format):
        """Retrieve the form for uploading dartdoc for this package version."""
Пример #45
0
def modify_object(obj,
                  content_encoding=None,
                  content_type=None,
                  content_disposition=None,
                  acl=None,
                  copy_source=None,
                  copy_source_if_match=None,
                  copy_source_if_none_match=None,
                  copy_source_if_modified_since=None,
                  copy_source_if_unmodified_since=None,
                  copy_metadata=True,
                  metadata={}):
    """Modifies or copies a cloud storage object.

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

    * The copy_metadata argument can be True, indicating that the metadata
      should be copied, or False, indicating that it should be replaced.
    * 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.
    """

    if not handlers.is_production():
        # The only way to modify an existing object using only the Python API
        # seems to be to copy it over itself. It's not a big deal since this is
        # only for development.
        if copy_source is None: copy_source = obj
        contents = None
        with files.open(_appengine_object_path(copy_source), 'r') as f:
            contents = f.read()

        if content_type is None: content_type = 'application/octet-stream'
        write_path = files.gs.create(_appengine_object_path(obj),
                                     mime_type=content_type,
                                     acl=acl,
                                     content_encoding=content_encoding,
                                     content_disposition=content_disposition,
                                     user_metadata=metadata)
        with files.open(write_path, 'a') as f: f.write(contents)
        files.finalize(write_path)
        return

    auth = "OAuth " + app_identity.get_access_token(_FULL_CONTROL_SCOPE)[0]
    headers = {
        "Authorization": auth,
        "Content-Encoding": content_encoding,
        "Content-Type": content_type,
        "Content-Disposition": content_disposition,
        "x-goog-api-version": "2",
        "x-goog-acl": acl,
        "x-goog-copy-source": _object_path(copy_source),
        "x-goog-copy-source-if-match": copy_source_if_match,
        "x-goog-copy-source-if-none-match": copy_source_if_none_match,
        "x-goog-copy-source-if-modified-since": copy_source_if_modified_since,
        "x-goog-copy-source-if-unmodified-since":
            copy_source_if_unmodified_since,
        "x-goog-copy-metadata-directive":
            "COPY" if copy_metadata else "REPLACE"
    }
    for (key, value) in metadata.iteritems():
        headers["x-goog-meta-" + key] = value
    headers = {key: value for key, value in headers.iteritems()
               if value is not None}

    response = urlfetch.fetch("https://storage.googleapis.com/" +
                                urllib.quote(_object_path(obj)),
                              method="PUT",
                              headers=headers,
                              follow_redirects=True)
    if response.status_code == 200: return

    xml = ElementTree.XML(response.content)
    raise handlers.http_error(500, "Cloud storage %s error: %s\n%s" % (
        response.status_code,
        xml.find('Code').text,
        xml.find('Message').text
    ))
Пример #46
0
def modify_object(obj,
                  content_encoding=None,
                  content_type=None,
                  content_disposition=None,
                  acl=None,
                  copy_source=None,
                  copy_source_if_match=None,
                  copy_source_if_none_match=None,
                  copy_source_if_modified_since=None,
                  copy_source_if_unmodified_since=None,
                  copy_metadata=True,
                  metadata={}):
    """Modifies or copies a cloud storage object.

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

    * The copy_metadata argument can be True, indicating that the metadata
      should be copied, or False, indicating that it should be replaced.
    * 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.
    """

    if not handlers.is_production():
        # The only way to modify an existing object using only the Python API
        # seems to be to copy it over itself. It's not a big deal since this is
        # only for development.
        if copy_source is None: copy_source = obj
        contents = None
        with files.open(_appengine_object_path(copy_source), 'r') as f:
            contents = f.read()

        if content_type is None: content_type = 'application/octet-stream'
        write_path = files.gs.create(_appengine_object_path(obj),
                                     mime_type=content_type,
                                     acl=acl,
                                     content_encoding=content_encoding,
                                     content_disposition=content_disposition,
                                     user_metadata=metadata)
        with files.open(write_path, 'a') as f:
            f.write(contents)
        files.finalize(write_path)
        return

    auth = "OAuth " + app_identity.get_access_token(_FULL_CONTROL_SCOPE)[0]
    headers = {
        "Authorization": auth,
        "Content-Encoding": content_encoding,
        "Content-Type": content_type,
        "Content-Disposition": content_disposition,
        "x-goog-api-version": "2",
        "x-goog-acl": acl,
        "x-goog-copy-source": _object_path(copy_source),
        "x-goog-copy-source-if-match": copy_source_if_match,
        "x-goog-copy-source-if-none-match": copy_source_if_none_match,
        "x-goog-copy-source-if-modified-since": copy_source_if_modified_since,
        "x-goog-copy-source-if-unmodified-since":
        copy_source_if_unmodified_since,
        "x-goog-copy-metadata-directive":
        "COPY" if copy_metadata else "REPLACE"
    }
    for (key, value) in metadata.iteritems():
        headers["x-goog-meta-" + key] = value
    headers = {
        key: value
        for key, value in headers.iteritems() if value is not None
    }

    response = urlfetch.fetch("https://storage.googleapis.com/" +
                              urllib.quote(_object_path(obj)),
                              method="PUT",
                              headers=headers,
                              follow_redirects=True)
    if response.status_code == 200: return

    xml = ElementTree.XML(response.content)
    raise handlers.http_error(
        500, "Cloud storage %s error: %s\n%s" %
        (response.status_code, xml.find('Code').text,
         xml.find('Message').text))