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
return
globaltag
21
22
23
def
getDatabaseInstanceDefault
(flags):
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
41
def
getCrestServer
():
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
56
def
getCrestAPI
():
57
"""Return default Crest API version"""
58
return
"api-v6.0"
59
60
61
def
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
#
84
if
__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"})
102
def
test_no_protocol
(self):
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/"})
107
def
test_with_protocol
(self):
108
self.assertEqual(
getCrestServer
(),
"http://myserver.com/"
)
109
self.assertEqual(
getCrestConnection
(),
"http://myserver.com/"
+
getCrestAPI
())
110
111
def
test_file_nonexistent
(self):
112
with
self.assertRaises(RuntimeError):
113
getCrestConnection
(
"mydb.txt"
)
114
115
def
test_file_exists
(self):
116
with
tempfile.NamedTemporaryFile()
as
f:
117
self.assertEqual(
getCrestConnection
(f.name), f.name)
118
119
unittest.main()
SiliconTech::strip
@ strip
Definition
FPGATrackSimTypes.h:25
python.IOVDbAutoCfgFlags.Test
Definition
IOVDbAutoCfgFlags.py:89
python.IOVDbAutoCfgFlags.Test.test_file_exists
test_file_exists(self)
Definition
IOVDbAutoCfgFlags.py:115
python.IOVDbAutoCfgFlags.Test.test_no_protocol
test_no_protocol(self)
Definition
IOVDbAutoCfgFlags.py:102
python.IOVDbAutoCfgFlags.Test.test_empty
test_empty(self)
Definition
IOVDbAutoCfgFlags.py:97
python.IOVDbAutoCfgFlags.Test.test_no_envvar
test_no_envvar(self)
Definition
IOVDbAutoCfgFlags.py:91
python.IOVDbAutoCfgFlags.Test.test_with_protocol
test_with_protocol(self)
Definition
IOVDbAutoCfgFlags.py:107
python.IOVDbAutoCfgFlags.Test.test_file_nonexistent
test_file_nonexistent(self)
Definition
IOVDbAutoCfgFlags.py:111
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:41
python.IOVDbAutoCfgFlags.getCrestAPI
getCrestAPI()
Definition
IOVDbAutoCfgFlags.py:56
python.IOVDbAutoCfgFlags.getDatabaseInstanceDefault
getDatabaseInstanceDefault(flags)
Definition
IOVDbAutoCfgFlags.py:23
python.IOVDbAutoCfgFlags.getCrestConnection
getCrestConnection(server=None, api=None)
Definition
IOVDbAutoCfgFlags.py:61
Generated on
for ATLAS Offline Software by
1.17.0