|
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,43 @@ 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 | + # Validate that file passed is from the gain reference directory |
| 339 | + gain_ref_dir = machine_config.get("gain_reference_directory", "") |
| 340 | + if not safe_gain_path.startswith(gain_ref_dir): |
| 341 | + raise ValueError( |
| 342 | + "Gain reference file does not originate from the gain reference directory " |
| 343 | + f"{gain_ref_dir!r}" |
| 344 | + ) |
| 345 | + |
| 346 | + # Return the rsync URL if set, otherwise assume you are syncing via Murfey |
| 347 | + rsync_url = urlparse(str(machine_config.get("rsync_url", _get_murfey_url()))) |
| 348 | + rsync_module = machine_config.get("rsync_module", "data") |
| 349 | + rsync_path = f"{rsync_url.hostname}::{rsync_module}/{safe_visit_path}/{safe_destination_dir}/{secure_filename(gain_reference.gain_path.name)}" |
| 350 | + |
| 351 | + # Run rsync subprocess to transfer gain reference |
335 | 352 | cmd = [
|
336 | 353 | "rsync",
|
337 | 354 | 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)}", |
| 355 | + rsync_path, |
339 | 356 | ]
|
340 |
| - gain_rsync = subprocess.run(cmd) |
| 357 | + gain_rsync = subprocess.run( |
| 358 | + cmd, |
| 359 | + capture_output=True, |
| 360 | + text=True, |
| 361 | + ) |
341 | 362 | if gain_rsync.returncode:
|
342 | 363 | logger.warning(
|
343 |
| - f"Gain reference file {safe_gain_path} was not successfully transferred to {safe_visit_path}/processing" |
| 364 | + f"Failed to transfer gain reference file {safe_gain_path!r} to {f'{safe_visit_path}/processing'!r} \n" |
| 365 | + f"Executed the following command: {' '.join(cmd)!r} \n" |
| 366 | + f"Returned the following error: \n" |
| 367 | + f"{gain_rsync.stderr}" |
344 | 368 | )
|
345 | 369 | return {"success": False}
|
346 | 370 | return {"success": True}
|
|
0 commit comments