Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] implement apps sdk #13

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# data files
*.json


.venv
55 changes: 22 additions & 33 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
from pathlib import Path
import shutil
from typing import List
from syftbox.lib import Client, SyftPermission
from syftbox_sdk import SyftBoxContext
from pydantic import BaseModel
from pydantic_core import from_json
import shutil

RING_APP_PATH = Path(os.path.abspath(__file__)).parent
INIT_DATA = Path(RING_APP_PATH / "data.json")


class RingData(BaseModel):
Expand All @@ -26,18 +27,17 @@ def load_json(cls, file):

class RingRunner:
def __init__(self):
self.client = Client.load()

self.my_email: str = self.client.email
self.ctx = SyftBoxContext.load()

# this is where all the app state goes
self.ring_pipeline_path: Path = (
Path(self.client.datasite_path) / "app_pipelines" / "ring"
)
self.ring_data = self.ctx.get_app_data("ring")

# this is where the pending inputs go
self.running_folder: Path = self.ring_pipeline_path / "running"
self.running_folder: Path = self.ring_data / "running"

# this is where the final result goes of a completed ring
self.done_folder: Path = self.ring_pipeline_path / "done"
self.done_folder: Path = self.ring_data / "done"

# this is your personal secret
self.secret_file: Path = RING_APP_PATH / "secret.txt"

Expand Down Expand Up @@ -73,43 +73,32 @@ def cleanup(self, file_path: Path) -> None:

def setup_folders(self) -> None:
print("Setting up the necessary folders.")
self.ctx.make_dirs(self.running_folder, self.done_folder)

if not self.running_folder.is_dir():
for folder in [self.running_folder, self.done_folder]:
folder.mkdir(parents=True, exist_ok=True)
with open(str(folder) + "/dummy", "w") as dummy_file:
dummy_file.write("\n")
shutil.move("data.json", str(self.running_folder.parent) + "/data.json")
# # set writable to everyone (default)
# but maybe we would want to restrict writes to ring participants?
# self.ctx.set_writable(self.ring_data, ring_data["participants"])
self.ctx.set_writable(self.ring_data)

# after this there will be files (so we can sync)
permission = SyftPermission.mine_with_public_write(self.my_email)
permission.ensure(self.ring_pipeline_path)
shutil.copy(INIT_DATA, self.running_folder / "data.json")

def my_secret(self):
def my_secret(self) -> int:
with open(self.secret_file, "r") as secret_file:
return int(secret_file.read().strip())

def pending_inputs_files(self) -> List[Path]:
return [
self.running_folder / file for file in self.running_folder.glob("*.json")
]
return [path.absolute() for path in self.running_folder.glob("*.json")]

def write_json(self, file_path: Path, result: RingData) -> None:
print(f"Writing to {file_path}.")
file_path.parent.mkdir(parents=True, exist_ok=True)
with open(file_path, "w") as f:
f.write(result.model_dump_json())
f.write(result.model_dump_json(indent=2))

def send_data(self, email: str, data: RingData) -> None:
destination_datasite_path = Path(self.client.sync_folder) / email
dest = (
destination_datasite_path
/ "app_pipelines"
/ "ring"
/ "running"
/ "data.json"
)
self.write_json(dest, data)
their_ring_data = self.ctx.get_app_data("ring", datasite=email)
their_running = their_ring_data / "running"
self.write_json(their_running / "data.json", data)

def terminate_ring(self, data: RingData) -> None:
print(f"Terminating ring, writing back to {self.done_folder}")
Expand Down
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

10 changes: 8 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
uv venv

# install requirements for the project
uv pip install -r requirements.txt
uv pip install -e ../syft/sdk

. .venv/bin/activate

which python

# run app using python from venv
uv run main.py
python main.py

deactivate