Skip to content

Commit

Permalink
🚚 Move transform settings into settings (#1498)
Browse files Browse the repository at this point in the history
  • Loading branch information
falexwolf authored Mar 14, 2024
1 parent 919acda commit be5eccd
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 47 deletions.
4 changes: 2 additions & 2 deletions docs/annotate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"source": [
"bt.settings.auto_save_parents = False # ignores ontological hierarchy\n",
"ln.settings.verbosity = \"info\"\n",
"ln.transform.stem_uid = \"sU0y1kF3igep\"\n",
"ln.transform.version = \"0\""
"ln.settings.transform.stem_uid = \"sU0y1kF3igep\"\n",
"ln.settings.transform.version = \"0\""
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions docs/faq/key.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"WIwaNDvlEkwS\"\n",
"ln.transform.version = \"1\"\n",
"ln.settings.transform.stem_uid = \"WIwaNDvlEkwS\"\n",
"ln.settings.transform.version = \"1\"\n",
"ln.track()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/faq/setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"2lhqA4uTKSFP\"\n",
"ln.transform.version = \"0\"\n",
"ln.settings.transform.stem_uid = \"2lhqA4uTKSFP\"\n",
"ln.settings.transform.version = \"0\"\n",
"ln.track()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/faq/track-run-inputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"Rx2s9aPTMQLY\"\n",
"ln.transform.version = \"1\"\n",
"ln.settings.transform.stem_uid = \"Rx2s9aPTMQLY\"\n",
"ln.settings.transform.version = \"1\"\n",
"ln.track()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"ln.setup.init(schema=\"bionty\", storage=\"./lamin-intro\")\n",
"\n",
"# tag your code with auto-generated identifiers for a script or notebook\n",
"ln.transform.stem_uid = \"FPnfDtJz8qbE\"\n",
"ln.transform.version = \"1\"\n",
"ln.settings.transform.stem_uid = \"FPnfDtJz8qbE\"\n",
"ln.settings.transform.version = \"1\"\n",
"\n",
"# track the execution of a transform with a global run context\n",
"ln.track()"
Expand Down
4 changes: 2 additions & 2 deletions docs/meta.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"vldHzF3aTAiW\"\n",
"ln.transform.version = \"1\"\n",
"ln.settings.transform.stem_uid = \"vldHzF3aTAiW\"\n",
"ln.settings.transform.version = \"1\"\n",
"ln.track()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/track.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"9priar0hoE5u\"\n",
"ln.transform.version = \"0\"\n",
"ln.settings.transform.stem_uid = \"9priar0hoE5u\"\n",
"ln.settings.transform.version = \"0\"\n",
"ln.track()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/transfer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"ITeOtm7bhtdq\"\n",
"ln.transform.version = \"1\"\n",
"ln.settings.transform.stem_uid = \"ITeOtm7bhtdq\"\n",
"ln.settings.transform.version = \"1\"\n",
"ln.track()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"NJvdsWWbJlZS\"\n",
"ln.transform.version = \"0\"\n",
"ln.settings.transform.stem_uid = \"NJvdsWWbJlZS\"\n",
"ln.settings.transform.version = \"0\"\n",
"ln.track()"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"dMtrt8YMSdl6\"\n",
"ln.transform.version = \"1\"\n",
"ln.settings.transform.stem_uid = \"dMtrt8YMSdl6\"\n",
"ln.settings.transform.version = \"1\"\n",
"\n",
"ln.track()"
]
Expand Down
2 changes: 1 addition & 1 deletion lamindb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __getattr__(name):
from ._view import view
from .core._run_context import run_context
from .core._settings import settings
from .core._transform_settings import transform_settings as transform
from .core._transform_settings import transform # backward compat

# schema modules
if not _os.environ.get("LAMINDB_MULTI_INSTANCE") == "true":
Expand Down
10 changes: 5 additions & 5 deletions lamindb/core/_run_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from lnschema_core.types import TransformType
from lnschema_core.users import current_user_id

from lamindb.core._transform_settings import transform_settings
from lamindb.core._transform_settings import transform as transform_settings

from ._settings import settings
from ._sync_git import get_transform_reference_from_git_repo
Expand Down Expand Up @@ -77,7 +77,7 @@ def get_stem_uid_and_version_from_file(file_path: str) -> Tuple[str, str]:

if stem_uid is None or version is None:
raise SystemExit(
f"ln.transform.stem_uid and ln.transform.version aren't set in {file_path}\n"
f"ln.settings.transform.stem_uid and ln.settings.transform.version aren't set in {file_path}\n"
"Call ln.track() and copy/paste the output into the notebook"
)
return stem_uid, version
Expand Down Expand Up @@ -124,7 +124,7 @@ def update_stem_uid_or_version(
updated = new_version != version
if updated:
new_metadata = (
f'ln.transform.stem_uid = "{new_stem_uid}"\nln.transform.version ='
f'ln.settings.transform.stem_uid = "{new_stem_uid}"\nln.settings.transform.version ='
f' "{new_version}"\n'
)
raise SystemExit(
Expand Down Expand Up @@ -167,8 +167,8 @@ def get_notebook_name_colab() -> str:

MESSAGE = """To track this {transform_type}, set the following two global variables:
ln.transform.stem_uid = "{stem_uid}"
ln.transform.version = "{version}"
ln.settings.transform.stem_uid = "{stem_uid}"
ln.settings.transform.version = "{version}"
"""


Expand Down
7 changes: 7 additions & 0 deletions lamindb/core/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from lamindb_setup.core._settings_instance import sanitize_git_repo_url
from upath import UPath

from ._transform_settings import TransformSettings, transform

VERBOSITY_TO_INT = {
"error": 0, # 40
"warning": 1, # 30
Expand Down Expand Up @@ -91,6 +93,11 @@ def _storage_settings(self) -> ln_setup.dev.StorageSettings:
storage_settings = ln_setup.dev.StorageSettings(root=self._using_storage)
return storage_settings

@property
def transform(self) -> TransformSettings:
"""Transform settings."""
return transform

@property
def sync_git_repo(self) -> Optional[str]:
"""Sync transforms with scripts in git repository.
Expand Down
40 changes: 28 additions & 12 deletions lamindb/core/_sync_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from ._settings import sanitize_git_repo_url, settings


class BlobHashNotFound(SystemExit):
pass


def get_git_repo_from_remote() -> Path:
repo_url = settings.sync_git_repo
repo_dir = setup_settings.storage.cache_dir / repo_url.split("/")[-1]
Expand All @@ -27,14 +31,25 @@ def get_git_repo_from_remote() -> Path:
return repo_dir


def check_remote_git_url_matches_setting():
def check_local_git_repo() -> bool:
result = subprocess.run(
"git config --get remote.origin.url",
shell=True,
capture_output=True,
)
remote_url = sanitize_git_repo_url(result.stdout.decode().strip())
assert remote_url == settings.sync_git_repo
result_str = result.stdout.decode().strip()
if result_str == "":
# running-not-in-a-git-repo
return False
else:
remote_url = sanitize_git_repo_url(result_str)
if remote_url == settings.sync_git_repo:
# running-in-correct-git-repo
return True
else:
# running-in-wrong-git-repo
logger.warning("running in wrong git repo")
return False


def get_git_commit_hash(
Expand Down Expand Up @@ -91,18 +106,19 @@ def get_filepath_within_git_repo(

def get_transform_reference_from_git_repo(path: Path) -> str:
blob_hash = hash_code(path).hexdigest()
commit_hash = get_git_commit_hash(blob_hash)
if commit_hash is not None:
logger.warning("found script in local repository")
check_remote_git_url_matches_setting()
commit_hash = None
if check_local_git_repo():
repo_dir = None
else:
repo_dir = get_git_repo_from_remote()
commit_hash = get_git_commit_hash(blob_hash, repo_dir=repo_dir)
if commit_hash is None:
raise RuntimeError(
f"Did not find blob hash {blob_hash} of {path} in git repo {repo_dir}"
)
commit_hash = get_git_commit_hash(blob_hash, repo_dir=repo_dir)
if commit_hash is None:
if repo_dir is None:
repo_dir = Path.cwd()
raise BlobHashNotFound(
f"❌ Did not find blob hash {blob_hash} in git repo ({settings.sync_git_repo}) {repo_dir}\n"
f"Did you commit the script? -> {path}"
)
gitpath = get_filepath_within_git_repo(commit_hash, blob_hash, repo_dir)
reference = f"{settings.sync_git_repo}/blob/{commit_hash}/{gitpath}"
return reference
6 changes: 2 additions & 4 deletions lamindb/core/_transform_settings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import Optional


# user-facing name
class transform:
class TransformSettings:
stem_uid: Optional[None] = None
version: Optional[None] = None


# internal name
transform_settings = transform
transform = TransformSettings()
2 changes: 1 addition & 1 deletion sub/lamindb-setup
4 changes: 2 additions & 2 deletions tests/notebooks/no-title.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"metadata": {},
"outputs": [],
"source": [
"ln.transform.stem_uid = \"Irn3xQyQ40GU\"\n",
"ln.transform.version = \"1\"\n",
"ln.settings.transform.stem_uid = \"Irn3xQyQ40GU\"\n",
"ln.settings.transform.version = \"1\"\n",
"\n",
"with pytest.raises(NoTitleError):\n",
" ln.track()"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_run_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ def test_sync_git_repo():
transform = ln.Transform.filter(name="initialized.py").one()
assert (
transform.reference
== "https://github.com/laminlabs/lamin-cli/blob/39fb29b1b3ccc891a025b5a631d6294413b6ee45/tests/scripts/initialized.py"
== "https://github.com/laminlabs/lamin-cli/blob/4244dadaf8e189549d614c54d49c78e35b00da85/tests/scripts/initialized.py"
)
assert transform.reference_type == "url"

0 comments on commit be5eccd

Please sign in to comment.