def package_version(db, user, channel_name, package_name, dao: Dao): channel_data = Channel(name=channel_name, private=False) package_data = Package(name=package_name) channel = dao.create_channel(channel_data, user.id, "owner") package = dao.create_package(channel_name, package_data, user.id, "owner") package_format = "tarbz2" package_info = "{}" version = dao.create_version( channel_name, package_name, package_format, "linux-64", "0.1", 0, "", "", package_info, user.id, ) yield version db.delete(version) db.delete(package) db.delete(channel) db.commit()
def patch_channel( channel_data: rest_models.Channel, dao: Dao = Depends(get_dao), auth: authorization.Rules = Depends(get_rules), channel: db_models.Channel = Depends(get_channel_or_fail), db=Depends(get_db), ): auth.assert_update_channel_info(channel.name) user_attrs = channel_data.dict(exclude_unset=True) if "size_limit" in user_attrs: auth.assert_set_channel_size_limit() changeable_attrs = ["private", "size_limit", "metadata"] for attr_ in user_attrs.keys(): if attr_ not in changeable_attrs: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=f"attribute '{attr_}' of channel can not be changed", ) for attr_, value_ in user_attrs.items(): if attr_ == "metadata": metadata = channel.load_channel_metadata() metadata.update(value_) setattr(channel, "channel_metadata", json.dumps(metadata)) else: setattr(channel, attr_, value_) db.commit() return channel
def public_channel(dao: Dao, user, channel_role, channel_name, db): channel_data = Channel(name=channel_name, private=False) channel = dao.create_channel(channel_data, user.id, channel_role) yield channel db.delete(channel) db.commit()
def private_channel(dao, other_user): channel_name = "private-channel" channel_data = Channel(name=channel_name, private=True) channel = dao.create_channel(channel_data, other_user.id, "owner") return channel
def public_channel(dao: Dao, user, channel_role): channel_name = "public-channel" channel_data = Channel(name=channel_name, private=False) channel = dao.create_channel(channel_data, user.id, channel_role) return channel
def package_with_versions(channel_name, package_name, dao, user, db): channel_data = Channel(name=channel_name, private=False) package_data = Package(name=package_name) dao.create_channel(channel_data, user.id, "owner") package = dao.create_package(channel_name, package_data, user.id, "owner") package_format = "tarbz2" package_info = "{}" versions = [ ("0.1.0", 0), ("1.0.0", 0), ("0.0.1", 0), ("0.0.2", 0), ("0.0.3", 0), ("1.0.0", 1), ("1.0.0", 2), ("0.1.0", 5), ("0.1.0", 2), ] package_versions = [] for ver, build_str in versions: package_version = dao.create_version( channel_name, package_name, package_format, "linux-64", ver, build_str, "", f"{package_name}-{ver}-{build_str}.tar.bz2", package_info, user.id, size=0, ) package_versions.append(package_version) yield package for package_version in package_versions: db.delete(package_version) db.commit()
def test_package_version(db, dao: Dao, user, channel_name, package_name): channel_data = Channel(name=channel_name, private=False) package_data = Package(name=package_name) dao.create_channel(channel_data, user.id, "owner") package = dao.create_package(channel_name, package_data, user.id, "owner") package_format = "tarbz2" package_info = "{}" versions = [ ("0.1.0", 0), ("1.0.0", 0), ("0.0.1", 0), ("0.0.2", 0), ("0.0.3", 0), ("1.0.0", 1), ("1.0.0", 2), ("0.1.0", 5), ("0.1.0", 2), ] for ver, build_str in versions: dao.create_version( channel_name, package_name, package_format, "linux-64", ver, build_str, "", f"{package_name}-{ver}-{build_str}.tar.bz2", package_info, user.id, size=0, ) res = dao.get_package_versions(package) res_versions = [(VersionOrder(x[0].version), x[0].build_number) for x in res] assert sorted(res_versions, reverse=True) == res_versions
def channel(dao: "quetz.dao.Dao", channel_name, user): channel_data = Channel(name=channel_name, private=False) channel = dao.create_channel(channel_data, user.id, "owner") return channel
def post_channel( request: Request, new_channel: rest_models.Channel, background_tasks: BackgroundTasks, mirror_api_key: Optional[str] = None, register_mirror: bool = False, dao: Dao = Depends(get_dao), auth: authorization.Rules = Depends(get_rules), task: Task = Depends(get_tasks_worker), config=Depends(get_config), session: requests.Session = Depends(get_remote_session), ): user_id = auth.assert_user() existing_channel = dao.get_channel(new_channel.name) if existing_channel: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail=f"Channel {new_channel.name} exists", ) if not new_channel.mirror_channel_url: auth.assert_create_channel() is_mirror = new_channel.mirror_channel_url and new_channel.mirror_mode == "mirror" is_proxy = new_channel.mirror_channel_url and new_channel.mirror_mode == "proxy" if is_mirror: auth.assert_create_mirror_channel() if is_proxy: auth.assert_create_proxy_channel() if new_channel.actions is None: if is_mirror: actions = [ChannelActionEnum.synchronize_repodata] else: actions = [] else: actions = new_channel.actions includelist = new_channel.metadata.includelist excludelist = new_channel.metadata.excludelist if includelist is not None and excludelist is not None: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Cannot use both `includelist` and `excludelist` together.", ) user_attrs = new_channel.dict(exclude_unset=True) if "size_limit" in user_attrs: auth.assert_set_channel_size_limit() size_limit = new_channel.size_limit else: if config.configured_section("quotas"): size_limit = config.quotas_channel_quota else: size_limit = None channel = dao.create_channel(new_channel, user_id, authorization.OWNER, size_limit) pkgstore.create_channel(new_channel.name) indexing.update_indexes(dao, pkgstore, new_channel.name) # register mirror if is_mirror and register_mirror: mirror_url = str(new_channel.mirror_channel_url) mirror_url = mirror_url.replace("get", "api/channels") headers = {"x-api-key": mirror_api_key} if mirror_api_key else {} api_endpoint = str( request.url.replace(query=None)) + '/' + new_channel.name request.url response = session.post( mirror_url + '/mirrors', json={ "url": api_endpoint.replace("api/channels", "get"), "api_endpoint": api_endpoint, "metrics_endpoint": api_endpoint.replace("api", "metrics"), }, headers=headers, ) if response.status_code != 201: logger.warning( f"could not register mirror due to error {response.text}") for action in actions: task.execute_channel_action( action, channel, )
def channel(dao, user, channel_name): channel_data = Channel(name=channel_name, private=False) channel = dao.create_channel(channel_data, user.id, "owner") return channel