10def resolve_asset_file(data_file: str, verbose: bool = False) -> str:
11 """
12 Resolves a data file URI to an absolute file path. The data file can be specified as
13 - An asset URI (e.g., asset://?name=frames)
14 - A file URI (e.g., file:///path/to/frames.bin)
15 - A local file path (e.g., /path/to/frames.bin)
16
17 Args:
18 data_file (str): The data file URI or path to resolve.
19 verbose (bool): If True, prints additional information during resolution.
20
21 Returns:
22 str: The absolute path to the resolved data file.
23
24 Raises:
25 RuntimeError: If the data file cannot be found or resolved.
26 """
27 data_file_url = urlparse(data_file)
28
29 if verbose:
30 print(f"Checking asset URI {data_file_url}")
31
32 if data_file_url.scheme == 'asset':
33 asset_query = dict(parse_qsl(data_file_url.query))
34 asset_db = Database(
35 '/cvmfs/dunedaq.opensciencegrid.org/assets/dunedaq-asset-db.sqlite'
36 )
37 asset_query['status'] = 'valid'
38
39 try:
40 files = asset_db.get_files(asset_query)
41 if not files:
42 raise RuntimeError(
43 f"Couldn\'t find a valid asset for the query {data_file_url.query}"
44 )
45
46 elif len(files)>1:
47 print(
48 f"Found {len(files)} assets in {dirname(asset_db.database_file)}, "
49 "taking the first one"
50 )
51
52 if verbose:
53 print(f"Found asset in {dirname(asset_db.database_file)}")
54
55 root_dir = dirname(asset_db.database_file)
56 return f'{root_dir}/{files[0]["path"]}/{files[0]["name"]}'
57
58 except OperationalError:
59 raise RuntimeError(f"Couldn\'t find the asset {data_file}")
60
61
62 elif data_file_url.scheme == 'file':
63 filename = abspath(data_file_url.netloc+data_file_url.path)
64
65 if not exists(filename):
66 raise RuntimeError(f'Cannot find the frames.bin file {filename}')
67
68 if verbose:
69 print(f"Found asset in {dirname(filename)}")
70
71 return filename
72
73 resolved_data_file = abspath(expandvars(data_file))
74 if resolved_data_file != '' and not exists(resolved_data_file):
75 raise RuntimeError(f'Cannot find the frames.bin file {data_file}')
76
77 if verbose:
78 print(f"Found asset in {dirname(resolved_data_file)}")
79
80 return resolved_data_file