|
7 | 7 | from functools import partial |
8 | 8 | from logging import getLogger |
9 | 9 | from pathlib import Path |
10 | | -from typing import Annotated, Dict, List, Optional, Union |
| 10 | +from typing import Annotated, Any, Dict, List, Optional, Union |
11 | 11 | from urllib.parse import urlparse |
12 | 12 |
|
13 | 13 | import requests |
@@ -328,19 +328,35 @@ def upload_gain_reference( |
328 | 328 | safe_gain_path = sanitise(str(gain_reference.gain_path)) |
329 | 329 | safe_visit_path = sanitise(gain_reference.visit_path) |
330 | 330 | safe_destination_dir = sanitise(gain_reference.gain_destination_dir) |
331 | | - machine_config = requests.get( |
| 331 | + |
| 332 | + # Load machine config and other needed properties |
| 333 | + machine_config: dict[str, Any] = requests.get( |
332 | 334 | f"{_get_murfey_url()}/instruments/{sanitise_nonpath(instrument_name)}/machine", |
333 | 335 | headers={"Authorization": f"Bearer {tokens[session_id]}"}, |
334 | 336 | ).json() |
| 337 | + |
| 338 | + # Return the rsync URL if set, otherwise assume you are syncing via Murfey |
| 339 | + rsync_url = urlparse(str(machine_config.get("rsync_url", _get_murfey_url()))) |
| 340 | + rsync_module = machine_config.get("rsync_module", "data") |
| 341 | + rsync_path = f"{rsync_url.hostname}::{rsync_module}/{safe_visit_path}/{safe_destination_dir}/{secure_filename(gain_reference.gain_path.name)}" |
| 342 | + |
| 343 | + # Run rsync subprocess to transfer gain reference |
335 | 344 | cmd = [ |
336 | 345 | "rsync", |
337 | 346 | posix_path(Path(safe_gain_path)), |
338 | | - 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)}", |
| 347 | + rsync_path, |
339 | 348 | ] |
340 | | - gain_rsync = subprocess.run(cmd) |
| 349 | + gain_rsync = subprocess.run( |
| 350 | + cmd, |
| 351 | + capture_output=True, |
| 352 | + text=True, |
| 353 | + ) |
341 | 354 | if gain_rsync.returncode: |
342 | 355 | logger.warning( |
343 | | - f"Gain reference file {safe_gain_path} was not successfully transferred to {safe_visit_path}/processing" |
| 356 | + f"Failed to transfer gain reference file {safe_gain_path!r} to {f'{safe_visit_path}/processing'!r} \n" |
| 357 | + f"Executed the following command: {' '.join(cmd)!r} \n" |
| 358 | + f"Returned the following error: \n" |
| 359 | + f"{gain_rsync.stderr}" |
344 | 360 | ) |
345 | 361 | return {"success": False} |
346 | 362 | return {"success": True} |
|
0 commit comments