def package_versions(context, request): """ Render the links for all versions of a package """ normalized_name = normalize_name(context.name) fallback = request.registry.fallback can_update_cache = request.access.can_update_cache() if fallback == 'mirror' and can_update_cache: pkgs = get_fallback_packages(request, context.name) if pkgs: return {'pkgs': pkgs} # When no upstream packages are found, intentionally fall through to # return any cached packages packages = request.db.all(normalized_name) if packages: if not request.access.has_permission(normalized_name, 'read'): return request.forbid() pkgs = {} for package in packages: pkgs[package.filename] = package.get_url(request) return {'pkgs': pkgs} elif request.registry.fallback == 'cache': if not can_update_cache: return request.forbid() pkgs = get_fallback_packages(request, context.name) if pkgs: return {'pkgs': pkgs} elif request.registry.fallback == 'redirect': redirect_url = "%s/%s/" % ( request.registry.fallback_url.rstrip('/'), context.name) return HTTPFound(location=redirect_url) return HTTPNotFound()
def upload(request, content, name=None, version=None, summary=None, requires_python=None): """ Handle update commands """ action = request.param(":action", "file_upload") # Direct uploads from the web UI go here, and don't have a name/version if name is None or version is None: name, version = parse_filename(content.filename) else: name = normalize_name(name) if action == "file_upload": if not request.access.has_permission(name, "write"): return request.forbid() try: return request.db.upload( content.filename, content.file, name=name, version=version, summary=summary, requires_python=requires_python or None, ) except ValueError as e: return HTTPConflict(*e.args) else: return HTTPBadRequest("Unknown action '%s'" % action)
def package_versions(context, request): """ Render the links for all versions of a package """ normalized_name = normalize_name(context.name) packages = request.db.all(normalized_name) if packages: if not request.access.has_permission(normalized_name, 'read'): return request.forbid() pkgs = {} for package in packages: pkgs[package.filename] = package.get_url(request) return {'pkgs': pkgs} elif request.registry.fallback == 'cache': if not request.access.can_update_cache(): return request.forbid() locator = FilenameScrapingLocator(request.registry.fallback_url) dists = locator.get_project(context.name) if not dists: return HTTPNotFound() pkgs = {} for dist in six.itervalues(dists): filename = posixpath.basename(dist.source_url) url = request.app_url('api', 'package', dist.name, filename) pkgs[filename] = url return {'pkgs': pkgs} elif request.registry.fallback == 'redirect': redirect_url = "%s/%s/" % ( request.registry.fallback_url.rstrip('/'), context.name) return HTTPFound(location=redirect_url) else: return HTTPNotFound()
def package_versions(context, request): """ Render the links for all versions of a package """ normalized_name = normalize_name(context.name) # filename -> url pkgs = {} # packages in database packages = request.db.all(normalized_name) if packages and request.access.has_permission(normalized_name, 'read'): for package in packages: pkgs[package.filename] = package.get_url(request) # packages in fallback if request.registry.fallback == 'cache' and request.access.can_update_cache(): locator = FilenameScrapingLocator(request.registry.fallback_url) dists = locator.get_project(context.name) for dist in six.itervalues(dists): filename = posixpath.basename(dist.source_url) url = request.app_url('api', 'package', dist.name, filename) if filename not in pkgs: pkgs[filename] = url # just redirect if len(pkgs) == 0: if request.registry.fallback == 'redirect': redirect_url = "%s/%s/" % ( request.registry.fallback_url.rstrip('/'), context.name) return HTTPFound(location=redirect_url) else: return HTTPNotFound() else: return {"pkgs": pkgs}
def upload(request, content, name=None, version=None, summary=None): """ Handle update commands """ action = request.param(":action", "file_upload") # Direct uploads from the web UI go here, and don't have a name/version if name is None or version is None: name, version = parse_filename(content.filename) else: name = normalize_name(name) if action == "file_upload": if not request.access.has_permission(name, "write"): return request.forbid() print("TYPE", type(content.file)) try: file_content = content.file.read() return request.db.upload( content.filename, file_content, name=name, digest=hashlib.sha256(file_content).hexdigest(), version=version, summary=summary, ) except ValueError as e: return HTTPConflict(*e.args) else: return HTTPBadRequest("Unknown action '%s'" % action)
def package_versions(context, request): """ List all unique package versions """ normalized_name = normalize_name(context.name) versions = request.db.all(normalized_name) return { 'packages': versions, 'write': request.access.has_permission(normalized_name, 'write'), }
def upload( self, filename: str, data: BinaryIO, name: Optional[str] = None, version: Optional[str] = None, summary: Optional[str] = None, requires_python: Optional[str] = None, ) -> Package: """ Save this package to the storage mechanism and to the cache Parameters ---------- filename : str Name of the package file data : file File-like readable object name : str, optional The name of the package (if not provided, will be parsed from filename) version : str, optional The version number of the package (if not provided, will be parsed from filename) summary : str, optional The summary of the package requires_python : str, optional The Python version requirement Returns ------- package : :class:`~pypicloud.models.Package` The Package object that was uploaded Raises ------ e : ValueError If the package already exists and allow_overwrite = False """ if version is None or name is None: name, version = parse_filename(filename, name) name = normalize_name(name) filename = posixpath.basename(filename) old_pkg = self.fetch(filename) metadata = {"requires_python": requires_python} if old_pkg is not None and not self.allow_overwrite: raise ValueError("Package '%s' already exists!" % filename) if self.calculate_hashes: file_data = data.read() metadata["hash_sha256"] = hashlib.sha256(file_data).hexdigest() metadata["hash_md5"] = hashlib.md5(file_data).hexdigest() data = BytesIO(file_data) new_pkg = self.new_package(name, version, filename, summary=summary, **metadata) self.storage.upload(new_pkg, data) self.save(new_pkg) return new_pkg
def upload( self, filename, data, name=None, version=None, summary=None, requires_python=None, ): """ Save this package to the storage mechanism and to the cache Parameters ---------- filename : str Name of the package file data : file File-like readable object name : str, optional The name of the package (if not provided, will be parsed from filename) version : str, optional The version number of the package (if not provided, will be parsed from filename) summary : str, optional The summary of the package requires_python : str, optional The Python version requirement Returns ------- package : :class:`~pypicloud.models.Package` The Package object that was uploaded Raises ------ e : ValueError If the package already exists and allow_overwrite = False """ if version is None: name, version = parse_filename(filename, name) name = normalize_name(name) filename = posixpath.basename(filename) old_pkg = self.fetch(filename) if old_pkg is not None and not self.allow_overwrite: raise ValueError("Package '%s' already exists!" % filename) new_pkg = self.package_class(name, version, filename, summary=summary, requires_python=requires_python) self.storage.upload(new_pkg, data) self.save(new_pkg) return new_pkg
def _simple_serve(context, request): """ Service /simple with fallback=none """ normalized_name = normalize_name(context.name) if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return HTTPNotFound("No packages found named %r" % normalized_name) else: return request.request_login() packages = request.db.all(normalized_name) return _pkg_response(packages_to_dict(request, packages))
def _simple_serve(context, request): """ Service /simple with fallback=none """ normalized_name = normalize_name(context.name) if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return HTTPNotFound("No packages found named %r" % normalized_name) else: return request.request_login() packages = request.db.all(normalized_name) return _pkg_response(_packages_to_dict(request, packages))
def _simple_redirect(context, request): """ Service /simple with fallback=redirect """ normalized_name = normalize_name(context.name) packages = request.db.all(normalized_name) if packages: if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return _redirect(context, request) else: return request.request_login() else: return _pkg_response(packages_to_dict(request, packages)) else: return _redirect(context, request)
def upload(request, name, version, content): """ Handle update commands """ action = request.param(':action') name = normalize_name(name) if action == 'file_upload': if not request.access.has_permission(name, 'write'): return request.forbid() try: return request.db.upload(content.filename, content.file, name=name, version=version) except ValueError as e: return HTTPBadRequest(*e.args) else: return HTTPBadRequest("Unknown action '%s'" % action)
def _simple_redirect(context, request): """ Service /simple with fallback=redirect """ normalized_name = normalize_name(context.name) packages = request.db.all(normalized_name) if packages: if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return _redirect(context, request) else: return request.request_login() else: return _pkg_response(_packages_to_dict(request, packages)) else: return _redirect(context, request)
def upload(request, content, name=None, version=None): """ Handle update commands """ action = request.param(':action', 'file_upload') # Direct uploads from the web UI go here, and don't have a name/version if name is None or version is None: name, version = parse_filename(content.filename) else: name = normalize_name(name) if action == 'file_upload': if not request.access.has_permission(name, 'write'): return request.forbid() try: return request.db.upload(content.filename, content.file, name=name, version=version) except ValueError as e: return HTTPBadRequest(*e.args) else: return HTTPBadRequest("Unknown action '%s'" % action)
def _simple_redirect_always_show(context, request): """ Service /simple with fallback=redirect """ normalized_name = normalize_name(context.name) packages = request.db.all(normalized_name) if packages: if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return _redirect(context, request) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name) stored_pkgs = packages_to_dict(request, packages) # Overwrite existing package urls for filename, url in six.iteritems(stored_pkgs): pkgs[filename] = url return _pkg_response(pkgs) else: return _redirect(context, request)
def _simple_redirect_always_show(context, request): """ Service /simple with fallback=redirect """ normalized_name = normalize_name(context.name) packages = request.db.all(normalized_name) if packages: if not request.access.has_permission(normalized_name, "read"): if request.is_logged_in: return _redirect(context, request) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name) stored_pkgs = packages_to_dict(request, packages) # Overwrite existing package urls for filename, url in six.iteritems(stored_pkgs): pkgs[filename] = url return _pkg_response(pkgs) else: return _redirect(context, request)
def upload(self, filename, data, name=None, version=None, summary=None): """ Save this package to the storage mechanism and to the cache Parameters ---------- filename : str Name of the package file data : file File-like readable object name : str, optional The name of the package (if not provided, will be parsed from filename) version : str, optional The version number of the package (if not provided, will be parsed from filename) summary : str, optional The summary of the package Returns ------- package : :class:`~pypicloud.models.Package` The Package object that was uploaded Raises ------ e : ValueError If the package already exists and allow_overwrite = False """ if version is None: name, version = parse_filename(filename, name) name = normalize_name(name) filename = posixpath.basename(filename) old_pkg = self.fetch(filename) if old_pkg is not None and not self.allow_overwrite: raise ValueError("Package '%s' already exists!" % filename) new_pkg = self.package_class(name, version, filename, summary=summary) self.storage.upload(new_pkg, data) self.save(new_pkg) return new_pkg
def _simple_cache(context, request): """ Service /simple with fallback=cache """ normalized_name = normalize_name(context.name) if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return HTTPNotFound("No packages found named %r" % normalized_name) else: return request.request_login() packages = request.db.all(normalized_name) if packages: return _pkg_response(_packages_to_dict(request, packages)) if not request.access.can_update_cache(): if request.is_logged_in: return HTTPNotFound("No packages found named %r" % normalized_name) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name, False) return _pkg_response(pkgs)
def _simple_cache(context, request): """ Service /simple with fallback=cache """ normalized_name = normalize_name(context.name) if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return HTTPNotFound("No packages found named %r" % normalized_name) else: return request.request_login() packages = request.db.all(normalized_name) if packages: return _pkg_response(packages_to_dict(request, packages)) if not request.access.can_update_cache(): if request.is_logged_in: return HTTPNotFound("No packages found named %r" % normalized_name) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name, False) return _pkg_response(pkgs)
def _simple_cache_always_show(context, request): """Service /simple with fallback=mirror""" normalized_name = normalize_name(context.name) if not request.access.has_permission(normalized_name, "read"): if request.is_authenticated: return _redirect(context, request) else: return request.request_login() packages = request.db.all(normalized_name) if packages: if not request.access.can_update_cache(): if request.is_authenticated: pkgs = get_fallback_packages(request, context.name) stored_pkgs = packages_to_dict(request, packages) # Overwrite existing package urls for filename, data in stored_pkgs.items(): pkgs[filename] = data return _pkg_response(pkgs) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name, False) stored_pkgs = packages_to_dict(request, packages) # Overwrite existing package urls for filename, data in stored_pkgs.items(): pkgs[filename] = data return _pkg_response(pkgs) else: if not request.access.can_update_cache(): if request.is_authenticated: return _redirect(context, request) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name, False) return _pkg_response(pkgs)
def _simple_mirror(context, request): """ Service /simple with fallback=mirror """ normalized_name = normalize_name(context.name) if not request.access.has_permission(normalized_name, 'read'): if request.is_logged_in: return _redirect(context, request) else: return request.request_login() packages = request.db.all(normalized_name) if packages: if not request.access.can_update_cache(): if request.is_logged_in: pkgs = get_fallback_packages(request, context.name) stored_pkgs = _packages_to_dict(request, packages) # Overwrite existing package urls for filename, url in six.iteritems(stored_pkgs): pkgs[filename] = url return _pkg_response(pkgs) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name, False) stored_pkgs = _packages_to_dict(request, packages) # Overwrite existing package urls for filename, url in six.iteritems(stored_pkgs): pkgs[filename] = url return _pkg_response(pkgs) else: if not request.access.can_update_cache(): if request.is_logged_in: return _redirect(context, request) else: return request.request_login() else: pkgs = get_fallback_packages(request, context.name, False) return _pkg_response(pkgs)
def test_normalize_namespace_package(self): """ Namespace packages must be normalized according to PEP503 """ self.assertEqual(util.normalize_name("repoze.lru"), "repoze-lru")
def test_normalize_namespace_package(self): """ Namespace packages must be normalized according to PEP503 """ self.assertEqual(util.normalize_name('repoze.lru'), 'repoze-lru')