|
1 | 1 | #!/bin/env python3 |
2 | 2 |
|
3 | | -"""This script downloads AO2Ds from an ALICE hyperloop train run""" |
| 3 | +"""This module downloads AO2Ds from an ALICE hyperloop train run""" |
4 | 4 |
|
5 | 5 | import argparse |
6 | 6 | import os |
7 | 7 | from pathlib import PurePosixPath |
8 | | -import sys |
9 | 8 |
|
10 | 9 | import requests # pylint: disable=import-error |
| 10 | +from alienpy import alien, xrd_core |
11 | 11 |
|
12 | | -try: |
13 | | - from alienpy import alien, xrd_core |
14 | | -except ImportError: |
15 | | - print("Failed to import alien -> no alien support") |
16 | | - |
17 | | -if __name__ == "__main__": |
18 | | - parser = argparse.ArgumentParser(description="Download AO2Ds from hyperloop train") |
19 | | - parser.add_argument("train_id", type=int, help="train ID") |
20 | | - parser.add_argument("--prefix", "-p", default="/data2/MLhep/trains/") |
21 | | - parser.add_argument("--dry-run", "-n", action="store_true", help="dry run") |
22 | | - args = parser.parse_args() |
23 | 12 |
|
| 13 | +def get_train_spec(train_id: int): |
| 14 | + """Retrieve train spec from hyperloop interface""" |
24 | 15 | # https://alimonitor.cern.ch/hyperloop/train-run/131050 |
25 | 16 | URL = f"https://alimonitor.cern.ch/alihyperloop-data/trains/train.jsp?train_id={args.train_id}" |
26 | 17 | try: |
27 | | - train_spec = requests.get( |
| 18 | + return requests.get( |
28 | 19 | URL, |
29 | 20 | verify=False, |
30 | 21 | cert=(f"/tmp/tokencert_{os.getuid()}.pem", f"/tmp/tokenkey_{os.getuid()}.pem"), |
31 | 22 | timeout=10, |
32 | 23 | ) |
33 | 24 | except requests.exceptions.SSLError as e: |
34 | 25 | print(f"SSL Error: {e}") |
35 | | - sys.exit(1) |
36 | | - outputdirs = [d["outputdir"] for d in train_spec.json()["jobResults"]] |
| 26 | + raise |
37 | 27 |
|
38 | | - TBASE = PurePosixPath(args.prefix) / str(args.train_id) |
39 | 28 |
|
40 | | - a = alien.AliEn() |
| 29 | +def find_ao2ds(a: alien.AliEn, dir: str) -> list[str]: |
| 30 | + """Find AO2Ds in train output directory""" |
| 31 | + CMD_FIND = f"find {PurePosixPath(dir) / 'AOD'} AO2D.root" |
| 32 | + ret = a.run(CMD_FIND) |
| 33 | + if ret.exitcode == 0: |
| 34 | + return ret.out.split() |
| 35 | + else: |
| 36 | + print(f"Failed to run search: {CMD_FIND}\n{ret.out}") |
| 37 | + return [] |
| 38 | + |
41 | 39 |
|
42 | | - for outputdir in outputdirs: |
43 | | - PATH = PurePosixPath(f"{outputdir}") / "AOD" |
44 | | - CMD_FIND = f"find {PATH} AO2D.root" |
45 | | - ret = a.run(CMD_FIND) |
46 | | - if ret.exitcode == 0: |
47 | | - SRC = ret.out.split() |
48 | | - DST = ["file:" + str(PurePosixPath(TBASE) / file.lstrip("/")) for file in SRC] |
49 | | - for s, d in zip(SRC, DST): |
50 | | - print(f"Copying {s} to {d}") |
51 | | - if not args.dry_run: |
52 | | - xrd_core.DO_XrootdCp(a.wb(), api_src=SRC, api_dst=DST) |
53 | | - else: |
54 | | - print(f"Failed to run search: {CMD_FIND}\n{ret.out}") |
55 | | - continue |
| 40 | +if __name__ == "__main__": |
| 41 | + parser = argparse.ArgumentParser(description="Download AO2Ds from hyperloop train") |
| 42 | + parser.add_argument("train_id", type=int, help="train ID") |
| 43 | + parser.add_argument("--prefix", "-p", default="/data2/MLhep/trains/") |
| 44 | + parser.add_argument("--dry-run", "-n", action="store_true", help="dry run") |
| 45 | + args = parser.parse_args() |
| 46 | + |
| 47 | + train_spec = get_train_spec(args.train_id) |
| 48 | + outputdirs = [d["outputdir"] for d in train_spec.json()["jobResults"]] |
| 49 | + |
| 50 | + a = alien.AliEn() |
| 51 | + SRC = [dir for outputdir in outputdirs for dir in find_ao2ds(a, outputdir)] |
| 52 | + DST = ["file:" + str(PurePosixPath(args.prefix) / str(args.train_id) / file.lstrip("/")) for file in SRC] |
| 53 | + for s, d in zip(SRC, DST): |
| 54 | + print(f"Copying {s} to {d}") |
| 55 | + if not args.dry_run: |
| 56 | + xrd_core.DO_XrootdCp(a.wb(), api_src=SRC, api_dst=DST) |
0 commit comments