Skip to content

Commit e441292

Browse files
authored
Improve dl_train.py (alisw#1004)
1 parent 2e4aeea commit e441292

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed
Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
#!/bin/env python3
22

3-
"""This script downloads AO2Ds from an ALICE hyperloop train run"""
3+
"""This module downloads AO2Ds from an ALICE hyperloop train run"""
44

55
import argparse
66
import os
77
from pathlib import PurePosixPath
8-
import sys
98

109
import requests # pylint: disable=import-error
10+
from alienpy import alien, xrd_core
1111

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()
2312

13+
def get_train_spec(train_id: int):
14+
"""Retrieve train spec from hyperloop interface"""
2415
# https://alimonitor.cern.ch/hyperloop/train-run/131050
2516
URL = f"https://alimonitor.cern.ch/alihyperloop-data/trains/train.jsp?train_id={args.train_id}"
2617
try:
27-
train_spec = requests.get(
18+
return requests.get(
2819
URL,
2920
verify=False,
3021
cert=(f"/tmp/tokencert_{os.getuid()}.pem", f"/tmp/tokenkey_{os.getuid()}.pem"),
3122
timeout=10,
3223
)
3324
except requests.exceptions.SSLError as e:
3425
print(f"SSL Error: {e}")
35-
sys.exit(1)
36-
outputdirs = [d["outputdir"] for d in train_spec.json()["jobResults"]]
26+
raise
3727

38-
TBASE = PurePosixPath(args.prefix) / str(args.train_id)
3928

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+
4139

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

Comments
 (0)