Skip to content

Commit b97657f

Browse files
committed
robotnix_common: use pathlib to simplify get_store_path
1 parent 6d64f18 commit b97657f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

scripts/robotnix_common.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import subprocess
99
import sys
10+
from pathlib import Path
1011

1112

1213
ROBOTNIX_GIT_MIRRORS = os.environ.get('ROBOTNIX_GIT_MIRRORS', '')
@@ -31,19 +32,24 @@ def save(filename: str, data: Any) -> None:
3132

3233
def get_store_path(path):
3334
"""Get actual path to a Nix store path; supports handling local remotes"""
34-
prefix = os.getenv("NIX_REMOTE", "/")
35-
if not prefix.startswith("/"):
35+
prefix = os.getenv("NIX_REMOTE")
36+
37+
if not prefix:
38+
return path
39+
40+
prefix = Path(prefix)
41+
42+
if not prefix.is_absolute():
3643
raise Exception(f"Must be run on a local Nix store. Current Nix store: {prefix}")
3744

38-
# Needs to be relative or otherwise join will ignore all the paths before it (WTF?)
39-
normalised_path = os.path.normpath(path).removeprefix('/')
40-
if normalised_path == ".":
41-
normalised_path = ""
42-
return os.path.join(prefix, normalised_path)
45+
path = Path(path).resolve()
46+
remote_path = prefix.resolve().joinpath(path.relative_to(f"{path.drive}{path.root}"))
47+
48+
return str(remote_path)
4349

4450
class GitCheckoutInfoDict(TypedDict):
4551
"""Container for output from nix-prefetch-git"""
46-
url: str
52+
url: str
4753
rev: str
4854
date: str
4955
path: str

0 commit comments

Comments
 (0)