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 return globaltag
21
22
24 # MC
25 if flags.Input.isMC:
26 return "OFLP200"
27
28 # real-data
29 try:
30 year = int(flags.Input.ProjectName[4:6])
31 except Exception:
32 log.warning("Failed to extract year from project tag %s. Assuming CONDBR2.", flags.Input.ProjectName)
33 return "CONDBR2"
34
35 if year > 13:
36 return "CONDBR2"
37 else:
38 return "COMP200"
39
40
42 """Return default Crest server URL (can be set via ${CREST_SERVER})"""
43 from urllib.parse import urlsplit
44
45 url = os.getenv("CREST_SERVER", "").strip()
46 if not url:
47 return "https://crest.cern.ch"
48
49 # Ensure we have a protocol, default to https
50 if not urlsplit(url).scheme:
51 url = "https://" + url.lstrip("/")
52
53 return url
54
55
57 """Return default Crest API version"""
58 return "api-v6.0"
59
60
61def getCrestConnection(server=None, api=None):
62 """Build connection string for Crest server based on server name and api.
63 If server is a valid file name, return that instead."""
64
65 from urllib.parse import urljoin, urlsplit
66
67 # Split URL assuming default protocol
68 url = urlsplit(server or getCrestServer(), scheme='https')
69
70 # Valid URL with server name
71 if url.netloc:
72 return urljoin(url.geturl(), api or getCrestAPI())
73
74 # Or check if there is a local file
75 if os.access(url.path, os.F_OK):
76 return url.path
77
78 raise RuntimeError(f"Invalid URL or non-existent file: {server}")
79
80
81#
82# Unit tests:
83#
84if __name__ == '__main__':
85 import tempfile
86 import unittest
87 from unittest.mock import patch
88
89 class Test(unittest.TestCase):
90
91 def test_no_envvar(self):
92 with patch.dict(os.environ):
93 os.environ.pop("CREST_SERVER", None)
94 self.assertEqual(getCrestServer(), "https://crest.cern.ch")
95
96 @patch.dict(os.environ, {"CREST_SERVER": ""})
97 def test_empty(self):
98 self.assertEqual(getCrestServer(), "https://crest.cern.ch")
99 self.assertEqual(getCrestConnection(), "https://crest.cern.ch/" + getCrestAPI())
100
101 @patch.dict(os.environ, {"CREST_SERVER": "myserver.com"})
103 self.assertEqual(getCrestServer(), "https://myserver.com")
104 self.assertEqual(getCrestConnection(), "https://myserver.com/" + getCrestAPI())
105
106 @patch.dict(os.environ, {"CREST_SERVER": "http://myserver.com/"})
108 self.assertEqual(getCrestServer(), "http://myserver.com/")
109 self.assertEqual(getCrestConnection(), "http://myserver.com/" + getCrestAPI())
110
112 with self.assertRaises(RuntimeError):
113 getCrestConnection("mydb.txt")
114
116 with tempfile.NamedTemporaryFile() as f:
117 self.assertEqual(getCrestConnection(f.name), f.name)
118
119 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)