ATLAS Offline Software
Loading...
Searching...
No Matches
IOVDbAutoCfgFlags.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3import os
4
5from AthenaCommon.Logging import logging
6log = logging.getLogger('IOVDbAutoCfgFlags')
7
8def getLastGlobalTag(prevFlags):
9 if not prevFlags.Input.Files:
10 return ""
11
12 from AthenaConfiguration.AutoConfigFlags import GetFileMD
13 globaltag = GetFileMD(prevFlags.Input.Files).get("IOVDbGlobalTag", None)
14 if globaltag is None:
15 return ""
16
17 if isinstance(globaltag, list): # if different tags have been used at different steps
18 globaltag = globaltag[-1]
19
20
21 #With the migration to crest, the global tag name also changed,
22 #even for tags that are straight copies from COOL. Adjust global
23 #tag names read from input metadata
24 if prevFlags.IOVDb.UseCREST and globaltag in ("OFLCOND-MC21-SDR-RUN4-05",
25 "OFLCOND-MC21-SDR-RUN4-06"):
26 globaltag=globaltag[3:]
27
28 return globaltag
29
30
32 # MC
33 if flags.Input.isMC:
34 return "OFLP200"
35
36 # real-data
37 try:
38 year = int(flags.Input.ProjectName[4:6])
39 except Exception:
40 log.warning("Failed to extract year from project tag %s. Assuming CONDBR2.", flags.Input.ProjectName)
41 return "CONDBR2"
42
43 if year > 13:
44 return "CONDBR2"
45 else:
46 return "COMP200"
47
48
50 """Return default Crest server URL (can be set via ${CREST_SERVER})"""
51 from urllib.parse import urlsplit
52
53 url = os.getenv("CREST_SERVER", "").strip()
54 if not url:
55 return "https://crest.cern.ch"
56
57 # Ensure we have a protocol, default to https
58 if not urlsplit(url).scheme:
59 url = "https://" + url.lstrip("/")
60
61 return url
62
63
65 """Return default Crest API version"""
66 return "api-v6.0"
67
68
69def getCrestConnection(server=None, api=None):
70 """Build connection string for Crest server based on server name and api.
71 If server is a valid file name, return that instead."""
72
73 from urllib.parse import urljoin, urlsplit
74
75 # Split URL assuming default protocol
76 url = urlsplit(server or getCrestServer(), scheme='https')
77
78 # Valid URL with server name
79 if url.netloc:
80 return urljoin(url.geturl(), api or getCrestAPI())
81
82 # Or check if there is a local file
83 if os.access(url.path, os.F_OK):
84 return url.path
85
86 raise RuntimeError(f"Invalid URL or non-existent file: {server}")
87
88
89#
90# Unit tests:
91#
92if __name__ == '__main__':
93 import tempfile
94 import unittest
95 from unittest.mock import patch
96
97 class Test(unittest.TestCase):
98
99 def test_no_envvar(self):
100 with patch.dict(os.environ):
101 os.environ.pop("CREST_SERVER", None)
102 self.assertEqual(getCrestServer(), "https://crest.cern.ch")
103
104 @patch.dict(os.environ, {"CREST_SERVER": ""})
105 def test_empty(self):
106 self.assertEqual(getCrestServer(), "https://crest.cern.ch")
107 self.assertEqual(getCrestConnection(), "https://crest.cern.ch/" + getCrestAPI())
108
109 @patch.dict(os.environ, {"CREST_SERVER": "myserver.com"})
111 self.assertEqual(getCrestServer(), "https://myserver.com")
112 self.assertEqual(getCrestConnection(), "https://myserver.com/" + getCrestAPI())
113
114 @patch.dict(os.environ, {"CREST_SERVER": "http://myserver.com/"})
116 self.assertEqual(getCrestServer(), "http://myserver.com/")
117 self.assertEqual(getCrestConnection(), "http://myserver.com/" + getCrestAPI())
118
120 with self.assertRaises(RuntimeError):
121 getCrestConnection("mydb.txt")
122
124 with tempfile.NamedTemporaryFile() as f:
125 self.assertEqual(getCrestConnection(f.name), f.name)
126
127 unittest.main()
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
getCrestConnection(server=None, api=None)