async def _upload_event(path_to_package: str) -> None: summary = await get_summary(path_to_package) evt = events.PackageChanged(summary) evt.change_type = Event.Type.ADD await send_to_clients(evt) logger.info(f"Package '{summary.package_meta.name}' was added.")
async def rename_package_cb(req: rpc.RenamePackage.Request, ui: WsClient) -> None: target_path = os.path.join(PROJECT_PATH, req.args.package_id, "package.json") pm = read_package_meta(req.args.package_id) pm.name = req.args.new_name with open(target_path, "w") as pkg_file: pkg_file.write(pm.to_json()) evt = events.PackageChanged(await get_summary(os.path.join(PROJECT_PATH, req.args.package_id))) evt.change_type = Event.Type.UPDATE asyncio.ensure_future(send_to_clients(evt))
async def rename_package_cb(req: rpc.RenamePackage.Request, ui: WsClient) -> None: target_path = os.path.join(PROJECT_PATH, req.args.package_id, "package.json") pm = read_package_meta(req.args.package_id) old_name = pm.name pm.name = req.args.new_name async with aiofiles.open(target_path, mode="w") as pkg_file: await pkg_file.write(pm.to_json()) evt = events.PackageChanged(await get_summary(os.path.join(PROJECT_PATH, req.args.package_id))) evt.change_type = Event.Type.UPDATE logger.info(f"Package '{old_name}' renamed to '{pm.name}'.") asyncio.ensure_future(send_to_clients(evt))
async def delete_package_cb(req: rpc.DeletePackage.Request, ui: WsClient) -> None: if RUNNING_PACKAGE_ID and RUNNING_PACKAGE_ID == req.args.id: raise Arcor2Exception("Package is being executed.") target_path = os.path.join(PROJECT_PATH, req.args.id) package_summary = await get_summary(target_path) try: shutil.rmtree(target_path) except FileNotFoundError: raise Arcor2Exception("Not found.") evt = events.PackageChanged(package_summary) evt.change_type = Event.Type.REMOVE asyncio.ensure_future(send_to_clients(evt)) return None
async def delete_package_cb(req: rpc.DeletePackage.Request, ui: WsClient) -> None: if RUNNING_PACKAGE_ID and RUNNING_PACKAGE_ID == req.args.id: raise Arcor2Exception("Package is being executed.") target_path = os.path.join(PROJECT_PATH, req.args.id) package_summary = await get_summary(target_path) try: await run_in_executor(shutil.rmtree, target_path, propagate=[FileNotFoundError]) except FileNotFoundError: raise Arcor2Exception("Not found.") evt = events.PackageChanged(package_summary) evt.change_type = Event.Type.REMOVE asyncio.ensure_future(send_to_clients(evt)) logger.info(f"Package '{package_summary.package_meta.name}' was removed.") return None
async def _upload_package_cb(req: rpc.UploadPackage.Request, ui: WsClient) -> None: target_path = os.path.join(PROJECT_PATH, req.args.id) # TODO do not allow if there are manual changes? async with tempfile.TemporaryDirectory() as tmpdirname: zip_path = os.path.join(tmpdirname, "publish.zip") b64_bytes = req.args.data.encode() zip_content = base64.b64decode(b64_bytes) async with aiofiles.open(zip_path, mode="wb") as zip_file: await zip_file.write(zip_content) try: with zipfile.ZipFile(zip_path, "r") as zip_ref: zip_ref.extractall(tmpdirname) except zipfile.BadZipFile as e: logger.error(e) raise Arcor2Exception("Invalid zip file.") await aiofiles.os.remove(zip_path) try: await run_in_executor(shutil.rmtree, target_path, propagate=[FileNotFoundError]) except FileNotFoundError: pass await run_in_executor(shutil.copytree, tmpdirname, target_path) script_path = os.path.join(target_path, MAIN_SCRIPT_NAME) await check_script(script_path) evt = events.PackageChanged(await get_summary(target_path)) evt.change_type = Event.Type.ADD asyncio.ensure_future(send_to_clients(evt)) return None