Skip to content

Commit 3308176

Browse files
authored
Change how we use the rsync module specified in the machine configuration (#476)
Currently we assume that the rsync module name forms part of the path but this isn't necessarily the case. We should specify the whole rsync module path as the rsync_basepath in the configuration and insert the module name directly where we need it (at the point of construction of the rsync command).
1 parent 12e96fe commit 3308176

File tree

12 files changed

+38
-41
lines changed

12 files changed

+38
-41
lines changed

src/murfey/client/contexts/clem.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ def _file_transferred_to(
3434
# rsync basepath and modules are set in the microscope's configuration YAML file
3535
return (
3636
Path(machine_config.get("rsync_basepath", ""))
37-
/ (
38-
machine_config.get("rsync_module", "data") or "data"
39-
) # Add "data" if it wasn't set
4037
/ str(datetime.now().year)
4138
/ source.name
4239
/ file_path.relative_to(source)

src/murfey/client/contexts/spa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _file_transferred_to(
4646
return (
4747
Path(machine_config.get("rsync_basepath", ""))
4848
/ Path(environment.default_destinations[source])
49-
/ file_path.relative_to(source)
49+
/ file_path.relative_to(source) # need to strip out the rsync_module name
5050
)
5151
return (
5252
Path(machine_config.get("rsync_basepath", ""))

src/murfey/client/multigrid_control.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class MultigridController:
3232
session_id: int
3333
murfey_url: str = "http://localhost:8000"
3434
rsync_url: str = ""
35+
rsync_module: str = "data"
3536
demo: bool = False
3637
processing_enabled: bool = True
3738
do_transfer: bool = True
@@ -59,12 +60,13 @@ def __post_init__(self):
5960
f"{self.murfey_url}/instruments/{self.instrument_name}/machine"
6061
).json()
6162
self.rsync_url = machine_data.get("rsync_url", "")
63+
self.rsync_module = machine_data.get("rsync_module", "data")
6264
self._environment = MurfeyInstanceEnvironment(
6365
url=urlparse(self.murfey_url, allow_fragments=False),
6466
client_id=0,
6567
murfey_session=self.session_id,
6668
software_versions=machine_data.get("software_versions", {}),
67-
default_destination=f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}",
69+
default_destination=f"{datetime.now().year}",
6870
demo=self.demo,
6971
visit=self.visit,
7072
data_collection_parameters=self.data_collection_parameters,
@@ -123,7 +125,7 @@ def _start_rsyncer_multigrid(
123125
break
124126
else:
125127
self._environment.default_destinations[source] = (
126-
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}"
128+
f"{datetime.now().year}"
127129
)
128130
destination = determine_default_destination(
129131
self._environment.visit,
@@ -205,7 +207,7 @@ def _start_rsyncer(
205207
rsync_cmd = [
206208
"rsync",
207209
f"{posix_path(self._environment.gain_ref)!r}", # '!r' will print strings in ''
208-
f"{self._environment.url.hostname}::{visit_path}/processing",
210+
f"{self._environment.url.hostname}::{self.rsync_module}/{visit_path}/processing",
209211
]
210212
# Wrap in bash shell
211213
cmd = [
@@ -223,6 +225,7 @@ def _start_rsyncer(
223225
self.rsync_processes[source] = RSyncer(
224226
source,
225227
basepath_remote=Path(destination),
228+
rsync_module=self.rsync_module,
226229
server_url=(
227230
urlparse(self.rsync_url)
228231
if self.rsync_url

src/murfey/client/rsync.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(
5454
self,
5555
basepath_local: Path,
5656
basepath_remote: Path,
57+
rsync_module: str,
5758
server_url: ParseResult,
5859
stop_callback: Callable = lambda *args, **kwargs: None,
5960
local: bool = False,
@@ -66,6 +67,7 @@ def __init__(
6667
super().__init__()
6768
self._basepath = basepath_local.absolute()
6869
self._basepath_remote = basepath_remote
70+
self._rsync_module = rsync_module
6971
self._do_transfer = do_transfer
7072
self._remove_files = remove_files
7173
self._required_substrings_for_removal = required_substrings_for_removal
@@ -76,7 +78,9 @@ def __init__(
7678
if local:
7779
self._remote = str(basepath_remote)
7880
else:
79-
self._remote = f"{server_url.hostname}::{basepath_remote}/"
81+
self._remote = (
82+
f"{server_url.hostname}::{self._rsync_module}/{basepath_remote}/"
83+
)
8084
# For local tests you can use something along the lines of
8185
# self._remote = f"wra62962@ws133:/dls/tmp/wra62962/junk/{basepath_remote}"
8286
# to avoid having to set up an rsync daemon
@@ -116,6 +120,7 @@ def from_rsyncer(cls, rsyncer: RSyncer, **kwargs):
116120
return cls(
117121
rsyncer._basepath,
118122
rsyncer._basepath_remote,
123+
rsyncer._rsync_module,
119124
rsyncer._server_url,
120125
local=kwarguments_from_rsyncer["local"],
121126
status_bar=kwarguments_from_rsyncer["status_bar"],

src/murfey/client/tui/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _start_rsyncer_multigrid(
164164
break
165165
else:
166166
self._environment.default_destinations[source] = (
167-
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}"
167+
f"{datetime.now().year}"
168168
)
169169
destination = determine_default_destination(
170170
self._visit,
@@ -214,7 +214,7 @@ def _start_rsyncer(
214214
rsync_cmd = [
215215
"rsync",
216216
f"{posix_path(self._environment.gain_ref)!r}",
217-
f"{self._url.hostname}::{visit_path}/processing",
217+
f"{self._url.hostname}::{self._machine_config.get('rsync_module', 'data')}/{visit_path}/processing",
218218
]
219219
# Encase in bash shell
220220
cmd = [
@@ -232,6 +232,7 @@ def _start_rsyncer(
232232
self.rsync_processes[source] = RSyncer(
233233
source,
234234
basepath_remote=Path(destination),
235+
rsync_module=self._machine_config.get("rsync_module", "data"),
235236
server_url=urlparse(rsync_url) if rsync_url else self._url,
236237
# local=self._environment.demo,
237238
status_bar=self._statusbar,

src/murfey/client/tui/screens.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ def determine_default_destination(
9191
elif machine_data.get("data_directories"):
9292
for data_dir in machine_data["data_directories"]:
9393
if source.resolve() == Path(data_dir):
94-
_default = destination + f"/{visit}"
94+
_default = (
95+
destination
96+
+ f"{machine_data.get('rsync_module') or 'data'}/{visit}"
97+
)
9598
break
9699
else:
97100
try:
@@ -310,12 +313,7 @@ def _add_directory(self, directory: str, add_destination: bool = True):
310313
if source.is_relative_to(s):
311314
return
312315
self.app._environment.sources.append(source)
313-
machine_data = requests.get(
314-
f"{self.app._environment.url.geturl()}/machine"
315-
).json()
316-
self.app._default_destinations[source] = (
317-
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}"
318-
)
316+
self.app._default_destinations[source] = f"{datetime.now().year}"
319317
if self._launch_btn:
320318
self._launch_btn.disabled = False
321319
self.query_one("#selected-directories").write(str(source) + "\n")
@@ -889,7 +887,7 @@ def on_button_pressed(self, event):
889887
rsync_cmd = [
890888
"rsync",
891889
f"{posix_path(self._dir_tree._gain_reference)!r}",
892-
f"{self.app._environment.url.hostname}::{visit_path}/processing/{secure_filename(self._dir_tree._gain_reference.name)}",
890+
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)}",
893891
]
894892
# Encase in bash shell
895893
cmd = ["bash", "-c", " ".join(rsync_cmd)]
@@ -1011,13 +1009,10 @@ def compose(self):
10111009
and d.name
10121010
not in machine_config["create_directories"].values()
10131011
):
1014-
machine_data = requests.get(
1015-
f"{self.app._environment.url.geturl()}/machine"
1016-
).json()
10171012
dest = determine_default_destination(
10181013
self.app._visit,
10191014
s,
1020-
f"{machine_data.get('rsync_module') or 'data'}/{datetime.now().year}",
1015+
f"{datetime.now().year}",
10211016
self.app._environment,
10221017
self.app.analysers,
10231018
touch=True,

src/murfey/instrument_server/api.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,23 @@ class GainReference(BaseModel):
276276
gain_destination_dir: str = "processing"
277277

278278

279-
@router.post("/sessions/{session_id}/upload_gain_reference")
280-
def upload_gain_reference(session_id: MurfeySessionID, gain_reference: GainReference):
279+
@router.post(
280+
"/instruments/{instrument_name}/sessions/{session_id}/upload_gain_reference"
281+
)
282+
def upload_gain_reference(
283+
instrument_name: str, session_id: MurfeySessionID, gain_reference: GainReference
284+
):
281285
safe_gain_path = sanitise(str(gain_reference.gain_path))
282286
safe_visit_path = sanitise(gain_reference.visit_path)
283287
safe_destination_dir = sanitise(gain_reference.gain_destination_dir)
288+
machine_config = requests.get(
289+
f"{_get_murfey_url()}/instruments/{sanitise_nonpath(instrument_name)}/machine",
290+
headers={"Authorization": f"Bearer {tokens[session_id]}"},
291+
).json()
284292
cmd = [
285293
"rsync",
286294
safe_gain_path,
287-
f"{urlparse(_get_murfey_url(), allow_fragments=False).hostname}::{safe_visit_path}/{safe_destination_dir}/{secure_filename(gain_reference.gain_path.name)}",
295+
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)}",
288296
]
289297
gain_rsync = subprocess.run(cmd)
290298
if gain_rsync.returncode:

src/murfey/server/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,10 +1628,7 @@ def _register_class_selection(message: dict, _db=murfey_db, demo: bool = False):
16281628
def _find_initial_model(visit: str, machine_config: MachineConfig) -> Path | None:
16291629
if machine_config.initial_model_search_directory:
16301630
visit_directory = (
1631-
machine_config.rsync_basepath
1632-
/ (machine_config.rsync_module or "data")
1633-
/ str(datetime.now().year)
1634-
/ visit
1631+
machine_config.rsync_basepath / str(datetime.now().year) / visit
16351632
)
16361633
possible_models = [
16371634
p

src/murfey/server/api/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,6 @@ async def process_gain(
14531453
safe_path_name = secure_filename(gain_reference_params.gain_ref.name)
14541454
filepath = (
14551455
Path(machine_config.rsync_basepath)
1456-
/ (machine_config.rsync_module or "data")
14571456
/ str(datetime.datetime.now().year)
14581457
/ secure_filename(visit_name)
14591458
/ machine_config.gain_directory_name
@@ -1464,7 +1463,6 @@ async def process_gain(
14641463
filepath_prev = filepath
14651464
filepath = (
14661465
Path(machine_config.rsync_basepath)
1467-
/ (machine_config.rsync_module or "data")
14681466
/ str(datetime.datetime.now().year - 1)
14691467
/ secure_filename(visit_name)
14701468
/ machine_config.gain_directory_name
@@ -1541,7 +1539,6 @@ async def write_eer_fractionation_file(
15411539
else:
15421540
file_path = (
15431541
Path(machine_config.rsync_basepath)
1544-
/ (machine_config.rsync_module or "data")
15451542
/ str(datetime.datetime.now().year)
15461543
/ secure_filename(visit_name)
15471544
/ "processing"
@@ -1586,7 +1583,6 @@ async def make_gif(
15861583
]
15871584
output_dir = (
15881585
Path(machine_config.rsync_basepath)
1589-
/ (machine_config.rsync_module or "data")
15901586
/ secure_filename(year)
15911587
/ secure_filename(visit_name)
15921588
/ "processed"

src/murfey/server/api/instrument.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ async def request_gain_reference_upload(
234234
instrument_name
235235
]
236236
visit = db.exec(select(Session).where(Session.id == session_id)).one().visit
237-
visit_path = f"{machine_config.rsync_module or 'data'}/{datetime.datetime.now().year}/{visit}"
237+
visit_path = f"{datetime.datetime.now().year}/{visit}"
238238
data = {}
239239
if machine_config.instrument_server_url:
240240
async with aiohttp.ClientSession() as session:
241241
async with session.post(
242-
f"{machine_config.instrument_server_url}/sessions/{session_id}/upload_gain_reference",
242+
f"{machine_config.instrument_server_url}/instruments/{instrument_name}/sessions/{session_id}/upload_gain_reference",
243243
json={
244244
"gain_path": str(gain_reference_request.gain_path),
245245
"visit_path": visit_path,

0 commit comments

Comments
 (0)