From 296ef2b61289bc8fbaaa17ec6e4249b0b66cda0b Mon Sep 17 00:00:00 2001 From: AdamZou Date: Fri, 17 May 2024 07:13:52 +0800 Subject: [PATCH] Revert "Added logic for assign separate directories to different sessions and delete the directory after a session is stoped." This reverts commit f8ff957e832aed7c6d361605e12f5d327408bbef. --- requirements.lock | 2 -- src/codeboxapi/box/codebox.py | 1 - src/codeboxapi/box/localbox.py | 29 +++++++++-------------------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/requirements.lock b/requirements.lock index 87e6a29..cb83081 100644 --- a/requirements.lock +++ b/requirements.lock @@ -14,8 +14,6 @@ aiosignal==1.3.1 # via aiohttp annotated-types==0.6.0 # via pydantic -async-timeout==4.0.3 - # via aiohttp attrs==23.2.0 # via aiohttp certifi==2024.2.2 diff --git a/src/codeboxapi/box/codebox.py b/src/codeboxapi/box/codebox.py index 941b744..77e63fb 100644 --- a/src/codeboxapi/box/codebox.py +++ b/src/codeboxapi/box/codebox.py @@ -52,7 +52,6 @@ class CodeBox(BaseBox): """ def __new__(cls, *args, **kwargs): - print("Using Adam's customized codebox.") if ( kwargs.pop("local", False) or settings.CODEBOX_API_KEY is None diff --git a/src/codeboxapi/box/localbox.py b/src/codeboxapi/box/localbox.py index 9d48b8e..b2956be 100644 --- a/src/codeboxapi/box/localbox.py +++ b/src/codeboxapi/box/localbox.py @@ -8,7 +8,6 @@ import asyncio import json import os -import shutil import signal import subprocess import sys @@ -65,9 +64,8 @@ def __init__(self, /, **kwargs) -> None: def start(self) -> CodeBoxStatus: self.session_id = uuid4() - self.directory_path = f".codebox/{str(self.session_id)}" - os.makedirs(self.directory_path, exist_ok=True) - print(f"made directory {self.directory_path}") + os.makedirs(".codebox", exist_ok=True) + self._check_port() if settings.VERBOSE: print("Starting kernel...") out = None @@ -87,7 +85,7 @@ def start(self) -> CodeBoxStatus: ], stdout=out, stderr=out, - cwd=self.directory_path, + cwd=".codebox", ) self._jupyter_pids.append(self.jupyter.pid) except FileNotFoundError: @@ -168,9 +166,7 @@ def _check_installed(self) -> None: async def astart(self) -> CodeBoxStatus: self.session_id = uuid4() - self.directory_path = f".codebox/{str(self.session_id)}" - os.makedirs(self.directory_path, exist_ok=True) - print(f"made directory {self.directory_path}") + os.makedirs(".codebox", exist_ok=True) self.aiohttp_session = aiohttp.ClientSession() await self._acheck_port() if settings.VERBOSE: @@ -190,7 +186,7 @@ async def astart(self) -> CodeBoxStatus: f"--KernelGatewayApp.port={self.port}", stdout=out, stderr=out, - cwd=self.directory_path, + cwd=".codebox", ) self._jupyter_pids.append(self.jupyter.pid) except Exception as e: @@ -521,10 +517,8 @@ async def arun( return CodeBoxOutput(type="error", content=error) def upload(self, file_name: str, content: bytes) -> CodeBoxStatus: - if not hasattr(self, 'directory_path'): - print("Directory path not found. Make sure to start the codebox session first.") - - with open(os.path.join(self.directory_path, file_name), "wb") as f: + os.makedirs(".codebox", exist_ok=True) + with open(os.path.join(".codebox", file_name), "wb") as f: f.write(content) return CodeBoxStatus(status=f"{file_name} uploaded successfully") @@ -533,7 +527,7 @@ async def aupload(self, file_name: str, content: bytes) -> CodeBoxStatus: return await asyncio.to_thread(self.upload, file_name, content) def download(self, file_name: str) -> CodeBoxFile: - with open(os.path.join(self.directory_path, file_name), "rb") as f: + with open(os.path.join(".codebox", file_name), "rb") as f: content = f.read() return CodeBoxFile(name=file_name, content=content) @@ -556,7 +550,7 @@ async def ainstall(self, package_name: str) -> CodeBoxStatus: def list_files(self) -> List[CodeBoxFile]: return [ CodeBoxFile(name=file_name, content=None) - for file_name in os.listdir(self.directory_path) + for file_name in os.listdir(".codebox") ] async def alist_files(self) -> List[CodeBoxFile]: @@ -574,9 +568,6 @@ async def arestart(self) -> CodeBoxStatus: def stop(self) -> CodeBoxStatus: try: - print(f"deleting directory {self.directory_path}") - shutil.rmtree(self.directory_path) # Delete the session directory and all its contents - if self.jupyter is not None: if isinstance(self.jupyter, subprocess.Popen): self.jupyter.terminate() @@ -607,8 +598,6 @@ def stop(self) -> CodeBoxStatus: return CodeBoxStatus(status="stopped") async def astop(self) -> CodeBoxStatus: - print(f"deleting directory {self.directory_path}") - shutil.rmtree(self.directory_path) # Delete the session directory and all its contents if self.jupyter is not None: self.jupyter.terminate() await asyncio.create_subprocess_exec("kill", "-9", str(self.jupyter.pid))