Skip to content

Commit

Permalink
Revert "Added logic for assign separate directories to different sess…
Browse files Browse the repository at this point in the history
…ions and delete the directory after a session is stoped."

This reverts commit f8ff957.
  • Loading branch information
AdamZou committed May 16, 2024
1 parent f8ff957 commit 296ef2b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
2 changes: 0 additions & 2 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/codeboxapi/box/codebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 9 additions & 20 deletions src/codeboxapi/box/localbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import asyncio
import json
import os
import shutil
import signal
import subprocess
import sys
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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]:
Expand All @@ -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()
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 296ef2b

Please sign in to comment.