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

Change how we use the rsync module specified in the machine configuration #476

Merged
merged 3 commits into from
Jan 31, 2025
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
3 changes: 0 additions & 3 deletions src/murfey/client/contexts/clem.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ def _file_transferred_to(
# rsync basepath and modules are set in the microscope's configuration YAML file
return (
Path(machine_config.get("rsync_basepath", ""))
/ (
machine_config.get("rsync_module", "data") or "data"
) # Add "data" if it wasn't set
/ str(datetime.now().year)
/ source.name
/ file_path.relative_to(source)
Expand Down
2 changes: 1 addition & 1 deletion src/murfey/client/contexts/spa.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _file_transferred_to(
return (
Path(machine_config.get("rsync_basepath", ""))
/ Path(environment.default_destinations[source])
/ file_path.relative_to(source)
/ file_path.relative_to(source) # need to strip out the rsync_module name
)
return (
Path(machine_config.get("rsync_basepath", ""))
Expand Down
9 changes: 6 additions & 3 deletions src/murfey/client/multigrid_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
session_id: int
murfey_url: str = "http://localhost:8000"
rsync_url: str = ""
rsync_module: str = "data"

Check warning on line 35 in src/murfey/client/multigrid_control.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/multigrid_control.py#L35

Added line #L35 was not covered by tests
demo: bool = False
processing_enabled: bool = True
do_transfer: bool = True
Expand Down Expand Up @@ -59,12 +60,13 @@
f"{self.murfey_url}/instruments/{self.instrument_name}/machine"
).json()
self.rsync_url = machine_data.get("rsync_url", "")
self.rsync_module = machine_data.get("rsync_module", "data")

Check warning on line 63 in src/murfey/client/multigrid_control.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/multigrid_control.py#L63

Added line #L63 was not covered by tests
self._environment = MurfeyInstanceEnvironment(
url=urlparse(self.murfey_url, allow_fragments=False),
client_id=0,
murfey_session=self.session_id,
software_versions=machine_data.get("software_versions", {}),
default_destination=f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}",
default_destination=f"{datetime.now().year}",
demo=self.demo,
visit=self.visit,
data_collection_parameters=self.data_collection_parameters,
Expand Down Expand Up @@ -118,7 +120,7 @@
break
else:
self._environment.default_destinations[source] = (
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}"
f"{datetime.now().year}"
)
destination = determine_default_destination(
self._environment.visit,
Expand Down Expand Up @@ -200,7 +202,7 @@
rsync_cmd = [
"rsync",
f"{posix_path(self._environment.gain_ref)!r}", # '!r' will print strings in ''
f"{self._environment.url.hostname}::{visit_path}/processing",
f"{self._environment.url.hostname}::{self.rsync_module}/{visit_path}/processing",
]
# Wrap in bash shell
cmd = [
Expand All @@ -218,6 +220,7 @@
self.rsync_processes[source] = RSyncer(
source,
basepath_remote=Path(destination),
rsync_module=self.rsync_module,
server_url=(
urlparse(self.rsync_url)
if self.rsync_url
Expand Down
7 changes: 6 additions & 1 deletion src/murfey/client/rsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
self,
basepath_local: Path,
basepath_remote: Path,
rsync_module: str,
server_url: ParseResult,
stop_callback: Callable = lambda *args, **kwargs: None,
local: bool = False,
Expand All @@ -66,6 +67,7 @@
super().__init__()
self._basepath = basepath_local.absolute()
self._basepath_remote = basepath_remote
self._rsync_module = rsync_module

Check warning on line 70 in src/murfey/client/rsync.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/rsync.py#L70

Added line #L70 was not covered by tests
self._do_transfer = do_transfer
self._remove_files = remove_files
self._required_substrings_for_removal = required_substrings_for_removal
Expand All @@ -76,7 +78,9 @@
if local:
self._remote = str(basepath_remote)
else:
self._remote = f"{server_url.hostname}::{basepath_remote}/"
self._remote = (

Check warning on line 81 in src/murfey/client/rsync.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/rsync.py#L81

Added line #L81 was not covered by tests
f"{server_url.hostname}::{self._rsync_module}/{basepath_remote}/"
)
# For local tests you can use something along the lines of
# self._remote = f"wra62962@ws133:/dls/tmp/wra62962/junk/{basepath_remote}"
# to avoid having to set up an rsync daemon
Expand Down Expand Up @@ -116,6 +120,7 @@
return cls(
rsyncer._basepath,
rsyncer._basepath_remote,
rsyncer._rsync_module,
rsyncer._server_url,
local=kwarguments_from_rsyncer["local"],
status_bar=kwarguments_from_rsyncer["status_bar"],
Expand Down
5 changes: 3 additions & 2 deletions src/murfey/client/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _start_rsyncer_multigrid(
break
else:
self._environment.default_destinations[source] = (
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}"
f"{datetime.now().year}"
)
destination = determine_default_destination(
self._visit,
Expand Down Expand Up @@ -214,7 +214,7 @@ def _start_rsyncer(
rsync_cmd = [
"rsync",
f"{posix_path(self._environment.gain_ref)!r}",
f"{self._url.hostname}::{visit_path}/processing",
f"{self._url.hostname}::{self._machine_config.get('rsync_module', 'data')}/{visit_path}/processing",
]
# Encase in bash shell
cmd = [
Expand All @@ -232,6 +232,7 @@ def _start_rsyncer(
self.rsync_processes[source] = RSyncer(
source,
basepath_remote=Path(destination),
rsync_module=self._machine_config.get("rsync_module", "data"),
server_url=urlparse(rsync_url) if rsync_url else self._url,
# local=self._environment.demo,
status_bar=self._statusbar,
Expand Down
19 changes: 7 additions & 12 deletions src/murfey/client/tui/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@
elif machine_data.get("data_directories"):
for data_dir in machine_data["data_directories"]:
if source.resolve() == Path(data_dir):
_default = destination + f"/{visit}"
_default = (

Check warning on line 94 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L94

Added line #L94 was not covered by tests
destination
+ f"{machine_data.get('rsync_module') or 'data'}/{visit}"
)
break
else:
try:
Expand Down Expand Up @@ -310,12 +313,7 @@
if source.is_relative_to(s):
return
self.app._environment.sources.append(source)
machine_data = requests.get(
f"{self.app._environment.url.geturl()}/machine"
).json()
self.app._default_destinations[source] = (
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}"
)
self.app._default_destinations[source] = f"{datetime.now().year}"

Check warning on line 316 in src/murfey/client/tui/screens.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/tui/screens.py#L316

Added line #L316 was not covered by tests
if self._launch_btn:
self._launch_btn.disabled = False
self.query_one("#selected-directories").write(str(source) + "\n")
Expand Down Expand Up @@ -889,7 +887,7 @@
rsync_cmd = [
"rsync",
f"{posix_path(self._dir_tree._gain_reference)!r}",
f"{self.app._environment.url.hostname}::{visit_path}/processing/{secure_filename(self._dir_tree._gain_reference.name)}",
f"{self.app._environment.url.hostname}::{self.app._machine_config.get('rsync_module', 'data')}/{visit_path}/processing/{secure_filename(self._dir_tree._gain_reference.name)}",
]
# Encase in bash shell
cmd = ["bash", "-c", " ".join(rsync_cmd)]
Expand Down Expand Up @@ -1011,13 +1009,10 @@
and d.name
not in machine_config["create_directories"].values()
):
machine_data = requests.get(
f"{self.app._environment.url.geturl()}/machine"
).json()
dest = determine_default_destination(
self.app._visit,
s,
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}",
f"{datetime.now().year}",
self.app._environment,
self.app.analysers,
touch=True,
Expand Down
14 changes: 11 additions & 3 deletions src/murfey/instrument_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,23 @@
gain_destination_dir: str = "processing"


@router.post("/sessions/{session_id}/upload_gain_reference")
def upload_gain_reference(session_id: MurfeySessionID, gain_reference: GainReference):
@router.post(

Check warning on line 279 in src/murfey/instrument_server/api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/instrument_server/api.py#L279

Added line #L279 was not covered by tests
"/instruments/{instrument_name}/sessions/{session_id}/upload_gain_reference"
)
def upload_gain_reference(

Check warning on line 282 in src/murfey/instrument_server/api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/instrument_server/api.py#L282

Added line #L282 was not covered by tests
instrument_name: str, session_id: MurfeySessionID, gain_reference: GainReference
):
safe_gain_path = sanitise(str(gain_reference.gain_path))
safe_visit_path = sanitise(gain_reference.visit_path)
safe_destination_dir = sanitise(gain_reference.gain_destination_dir)
machine_config = requests.get(

Check warning on line 288 in src/murfey/instrument_server/api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/instrument_server/api.py#L288

Added line #L288 was not covered by tests
f"{_get_murfey_url()}/instruments/{sanitise_nonpath(instrument_name)}/machine",
headers={"Authorization": f"Bearer {tokens[session_id]}"},
).json()
Dismissed Show dismissed Hide dismissed
cmd = [
"rsync",
safe_gain_path,
f"{urlparse(_get_murfey_url(), allow_fragments=False).hostname}::{safe_visit_path}/{safe_destination_dir}/{secure_filename(gain_reference.gain_path.name)}",
f"{urlparse(_get_murfey_url(), allow_fragments=False).hostname}::{machine_config.get('rsync_module', 'data')}/{safe_visit_path}/{safe_destination_dir}/{secure_filename(gain_reference.gain_path.name)}",
]
gain_rsync = subprocess.run(cmd)
if gain_rsync.returncode:
Expand Down
5 changes: 1 addition & 4 deletions src/murfey/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1628,10 +1628,7 @@ def _register_class_selection(message: dict, _db=murfey_db, demo: bool = False):
def _find_initial_model(visit: str, machine_config: MachineConfig) -> Path | None:
if machine_config.initial_model_search_directory:
visit_directory = (
machine_config.rsync_basepath
/ (machine_config.rsync_module or "data")
/ str(datetime.now().year)
/ visit
machine_config.rsync_basepath / str(datetime.now().year) / visit
)
possible_models = [
p
Expand Down
4 changes: 0 additions & 4 deletions src/murfey/server/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,6 @@ async def process_gain(
safe_path_name = secure_filename(gain_reference_params.gain_ref.name)
filepath = (
Path(machine_config.rsync_basepath)
/ (machine_config.rsync_module or "data")
/ str(datetime.datetime.now().year)
/ secure_filename(visit_name)
/ machine_config.gain_directory_name
Expand All @@ -1464,7 +1463,6 @@ async def process_gain(
filepath_prev = filepath
filepath = (
Path(machine_config.rsync_basepath)
/ (machine_config.rsync_module or "data")
/ str(datetime.datetime.now().year - 1)
/ secure_filename(visit_name)
/ machine_config.gain_directory_name
Expand Down Expand Up @@ -1541,7 +1539,6 @@ async def write_eer_fractionation_file(
else:
file_path = (
Path(machine_config.rsync_basepath)
/ (machine_config.rsync_module or "data")
/ str(datetime.datetime.now().year)
/ secure_filename(visit_name)
/ "processing"
Expand Down Expand Up @@ -1586,7 +1583,6 @@ async def make_gif(
]
output_dir = (
Path(machine_config.rsync_basepath)
/ (machine_config.rsync_module or "data")
/ secure_filename(year)
/ secure_filename(visit_name)
/ "processed"
Expand Down
4 changes: 2 additions & 2 deletions src/murfey/server/api/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@
instrument_name
]
visit = db.exec(select(Session).where(Session.id == session_id)).one().visit
visit_path = f"{machine_config.rsync_module or 'data'}/{datetime.datetime.now().year}/{visit}"
visit_path = f"{datetime.datetime.now().year}/{visit}"

Check warning on line 237 in src/murfey/server/api/instrument.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/instrument.py#L237

Added line #L237 was not covered by tests
data = {}
if machine_config.instrument_server_url:
async with aiohttp.ClientSession() as session:
async with session.post(
f"{machine_config.instrument_server_url}/sessions/{session_id}/upload_gain_reference",
f"{machine_config.instrument_server_url}/instruments/{instrument_name}/sessions/{session_id}/upload_gain_reference",
json={
"gain_path": str(gain_reference_request.gain_path),
"visit_path": visit_path,
Expand Down
5 changes: 1 addition & 4 deletions src/murfey/server/api/spa.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ def _cryolo_model_path(visit: str, instrument_name: str) -> Path:
]
if machine_config.model_search_directory:
visit_directory = (
machine_config.rsync_basepath
/ (machine_config.rsync_module or "data")
/ str(datetime.now().year)
/ visit
machine_config.rsync_basepath / str(datetime.now().year) / visit
)
possible_models = list(
(visit_directory / machine_config.model_search_directory).glob("*.h5")
Expand Down
2 changes: 0 additions & 2 deletions src/murfey/server/demo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,6 @@ async def process_gain(
if machine_config.get("rsync_basepath"):
filepath = (
Path(machine_config["rsync_basepath"])
/ (machine_config.get("rsync_module") or "data")
/ str(datetime.datetime.now().year)
/ visit_name
)
Expand Down Expand Up @@ -1681,7 +1680,6 @@ async def write_eer_fractionation_file(
) -> dict:
file_path = (
Path(machine_config["rsync_basepath"])
/ (machine_config["rsync_module"] or "data")
/ str(datetime.datetime.now().year)
/ secure_filename(visit_name)
/ secure_filename(fractionation_params.fractionation_file_name)
Expand Down