ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Database
IOVDbSvc
python
IOVDbAutoCfgFlags.py
Go to the documentation of this file.
1
# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3
import
os
4
5
from
AthenaCommon.Logging
import
logging
6
log = logging.getLogger(
'IOVDbAutoCfgFlags'
)
7
8
def
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
31
def
getDatabaseInstanceDefault
(flags):
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
49
def
getCrestServer
():
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
64
def
getCrestAPI
():
65
"""Return default Crest API version"""
66
return
"api-v6.0"
67
68
69
def
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
#
92
if
__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"})
110
def
test_no_protocol
(self):
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/"})
115
def
test_with_protocol
(self):
116
self.assertEqual(
getCrestServer
(),
"http://myserver.com/"
)
117
self.assertEqual(
getCrestConnection
(),
"http://myserver.com/"
+
getCrestAPI
())
118
119
def
test_file_nonexistent
(self):
120
with
self.assertRaises(RuntimeError):
121
getCrestConnection
(
"mydb.txt"
)
122
123
def
test_file_exists
(self):
124
with
tempfile.NamedTemporaryFile()
as
f:
125
self.assertEqual(
getCrestConnection
(f.name), f.name)
126
127
unittest.main()
SiliconTech::strip
@ strip
Definition
FPGATrackSimTypes.h:25
python.IOVDbAutoCfgFlags.Test
Definition
IOVDbAutoCfgFlags.py:97
python.IOVDbAutoCfgFlags.Test.test_file_exists
test_file_exists(self)
Definition
IOVDbAutoCfgFlags.py:123
python.IOVDbAutoCfgFlags.Test.test_no_protocol
test_no_protocol(self)
Definition
IOVDbAutoCfgFlags.py:110
python.IOVDbAutoCfgFlags.Test.test_empty
test_empty(self)
Definition
IOVDbAutoCfgFlags.py:105
python.IOVDbAutoCfgFlags.Test.test_no_envvar
test_no_envvar(self)
Definition
IOVDbAutoCfgFlags.py:99
python.IOVDbAutoCfgFlags.Test.test_with_protocol
test_with_protocol(self)
Definition
IOVDbAutoCfgFlags.py:115
python.IOVDbAutoCfgFlags.Test.test_file_nonexistent
test_file_nonexistent(self)
Definition
IOVDbAutoCfgFlags.py:119
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition
hcg.cxx:132
python.IOVDbAutoCfgFlags.getLastGlobalTag
getLastGlobalTag(prevFlags)
Definition
IOVDbAutoCfgFlags.py:8
python.IOVDbAutoCfgFlags.getCrestServer
getCrestServer()
Definition
IOVDbAutoCfgFlags.py:49
python.IOVDbAutoCfgFlags.getCrestAPI
getCrestAPI()
Definition
IOVDbAutoCfgFlags.py:64
python.IOVDbAutoCfgFlags.getDatabaseInstanceDefault
getDatabaseInstanceDefault(flags)
Definition
IOVDbAutoCfgFlags.py:31
python.IOVDbAutoCfgFlags.getCrestConnection
getCrestConnection(server=None, api=None)
Definition
IOVDbAutoCfgFlags.py:69
Generated on
for ATLAS Offline Software by
1.17.0