Skip to content

Commit

Permalink
Init ignore file when one doesn't exist (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpetrovic authored Jan 24, 2024
1 parent e60ea43 commit b151892
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ make setup-sdk

### Using the SDK

After you've setup the server and SDK, you can now start accessing the system.

TODO: Add walk through. Should this be a separate readme?
After you've setup the server and SDK, check out the SDK readme [here](sdk/README.md).

## Community & Support

Expand Down
42 changes: 33 additions & 9 deletions sdk/src/beta9/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import os
import uuid
import zipfile
from typing import Generator, NamedTuple, Union
from pathlib import Path
from typing import Generator, NamedTuple, Optional

from beta9 import terminal
from beta9.clients.gateway import (
Expand All @@ -15,6 +16,17 @@
)

IGNORE_FILE_NAME = ".beta9ignore"
IGNORE_FILE_CONTENTS = """# Generated by Beta9 SDK
.beta9ignore
.git
.idea
.python-version
.vscode
.venv
venv
*.pyc
__pycache__
"""


class FileSyncResult(NamedTuple):
Expand All @@ -29,17 +41,28 @@ def __init__(
root_dir=".",
):
self.loop = asyncio.get_event_loop()
self.root_dir = os.path.abspath(root_dir)
self.root_dir = Path(root_dir).absolute()
self.gateway_stub: GatewayServiceStub = gateway_stub

@property
def ignore_file_path(self) -> Path:
return self.root_dir / IGNORE_FILE_NAME

def _init_ignore_file(self) -> None:
if self.ignore_file_path.exists():
return

terminal.detail(f"Writing {IGNORE_FILE_NAME} file")
with self.ignore_file_path.open(mode="w") as f:
f.writelines(IGNORE_FILE_CONTENTS)

def _read_ignore_file(self) -> list:
terminal.detail(f"Reading {IGNORE_FILE_NAME} file")

ignore_file = os.path.join(self.root_dir, IGNORE_FILE_NAME)
patterns = []

if os.path.isfile(ignore_file):
with open(ignore_file, "r") as file:
if self.ignore_file_path.is_file():
with self.ignore_file_path.open() as file:
patterns = [line.strip() for line in file.readlines() if line.strip()]

return patterns
Expand Down Expand Up @@ -70,6 +93,7 @@ def _collect_files(self) -> Generator[str, None, None]:
def sync(self) -> FileSyncResult:
terminal.header("Syncing files")

self._init_ignore_file()
self.ignore_patterns = self._read_ignore_file()
temp_zip_name = f"/tmp/{uuid.uuid4()}"

Expand All @@ -90,12 +114,12 @@ def sync(self) -> FileSyncResult:
head_response: HeadObjectResponse = self.loop.run_until_complete(
self.gateway_stub.head_object(hash=hash)
)
put_response: Union[PutObjectResponse, None] = None
put_response: Optional[PutObjectResponse] = None
if not head_response.exists:
metadata = ObjectMetadata(name=hash, size=size)

with terminal.progress("Uploading"):
put_response: PutObjectResponse = self.loop.run_until_complete(
put_response = self.loop.run_until_complete(
self.gateway_stub.put_object(
object_content=object_content,
object_metadata=metadata,
Expand All @@ -109,8 +133,8 @@ def sync(self) -> FileSyncResult:

os.remove(temp_zip_name)

if not put_response.ok:
if put_response is None or not put_response.ok:
terminal.error("File sync failed ☠️")

terminal.header("Files synced")
return FileSyncResult(success=True, object_id=put_response.object_id)
return FileSyncResult(success=True, object_id=put_response.object_id) # pyright: ignore[reportOptionalMemberAccess]

0 comments on commit b151892

Please sign in to comment.