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

Sharing fix #153

Merged
merged 4 commits into from
Aug 23, 2024
Merged
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
46 changes: 33 additions & 13 deletions cassini/sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
from collections import defaultdict
from typing import Any, Dict, List, Union, Optional, Tuple, Generic, TypeVar
from types import MethodType
from typing_extensions import Self, Annotated
from typing_extensions import Self
import datetime
from pathlib import Path
import shutil
from io import TextIOWrapper
import warnings

from pydantic import (
JsonValue,
BaseModel,
Field,
ConfigDict,
PlainSerializer,
AfterValidator,
GetCoreSchemaHandler,
)
from pydantic_core import CoreSchema, core_schema
Expand Down Expand Up @@ -410,6 +409,16 @@ def adjust_path(self, path: Path) -> Path:

return self.shared_project.requires_path / path.relative_to(self.base_path)

def process_tier_val(self, val: Any) -> Any:
if isinstance(val, Path):
return self.adjust_path(val)

if isinstance(val, SharedTier):
assert self.shared_project
return SharedTier.with_project(val.name, self.shared_project)

return val

def __getattr__(self, name: str) -> Any:
if self.shared_project is None:
raise RuntimeError(
Expand All @@ -419,21 +428,15 @@ def __getattr__(self, name: str) -> Any:
if name in self._accessed:
val = self._accessed[name]

if isinstance(val, Path):
val = self.adjust_path(val)

return val
return self.process_tier_val(val)
else:

def meth(*args, **kwargs):
args_kwargs = (args, tuple(kwargs))

val = self._called[name][args_kwargs]

if isinstance(val, Path):
val = self.adjust_path(val)

return val
return self.process_tier_val(val)

return meth

Expand Down Expand Up @@ -581,8 +584,8 @@ class ShareableProject:

def __new__(cls, *args, **kwargs) -> Self:
if env.shareable_project:
raise RuntimeError(
"Only one shareable project instance should be created per interpretter"
warnings.warn(
"Creating a new instance of SharingProject, when one has already been created. This can create unexpected behaviour."
)

obj = super().__new__(cls)
Expand Down Expand Up @@ -677,19 +680,34 @@ def make_shared(self) -> None:

path = self.location

print("Creating shared directory:", path)

path.mkdir(exist_ok=True)

print("Success")
print("Making Requires directory")

self.requires_path.mkdir(exist_ok=True)

print("Success")

for stier in self.shared_tiers:
print(f"Creating shared version of {stier.name}")

tier_dir, meta_file, frozen_file = self.make_paths(stier)
tier_dir.mkdir(exist_ok=True)

if stier.meta:
print("Copying Meta")
shutil.copy(stier.meta.file, meta_file)
print("Success")

with open(frozen_file, "w") as fs:
print("Freezing attributes/ calls")
stier.dump(fs)
print("Success")

print("Making a copy of required files")
for required in stier.find_paths():
if required.exists():
destination = self.requires_path / required.relative_to(
Expand All @@ -698,4 +716,6 @@ def make_shared(self) -> None:
directory = destination if required.is_dir() else destination.parent
directory.mkdir(exist_ok=True, parents=True)

print("Copying", required)
shutil.copy(required, destination)
print("Success")
2 changes: 2 additions & 0 deletions tests/test_sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def test_making_share(get_Project, tmp_path):

stier / 'data.txt'
child = stier['1']
child_href = child.href

shared_project.make_shared()

Expand All @@ -319,6 +320,7 @@ def test_making_share(get_Project, tmp_path):
assert shared_tier.description == 'description' == tier.description

assert shared_tier['1'].name == child.name
assert shared_tier['1'].href == child_href

shared_tier.description = 'new description'

Expand Down
Loading