ATLAS Offline Software
TrigARTUtils.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 #
4 
5 '''Functions useful for dealing with Trigger ART test scripts'''
6 
7 import os
8 import re
9 from contextlib import contextmanager
10 
11 
12 def package_prefix(package):
13  '''Returns a prefix included in names of all tests from the given package'''
14  from TrigValTools.TrigValSteering.Common import package_prefix_dict
15  if package == 'ALL':
16  return '({})'.format('|'.join(package_prefix_dict.values()))
17  elif package in package_prefix_dict.keys():
18  return package_prefix_dict[package]
19  else:
20  return None
21 
22 
23 def duplicate_filename(list, filename):
24  for path in list:
25  if os.path.basename(path) == filename:
26  return True
27  return False
28 
29 
30 def find_scripts(patterns):
31  scripts = []
32  for path in os.environ['PATH'].split(os.pathsep):
33  try:
34  files = os.listdir(path)
35  except OSError:
36  continue
37  for filename in files:
38  matched = True
39  for patt in patterns:
40  if re.search(patt, filename) is None:
41  matched = False
42  break
43  if matched and not duplicate_filename(scripts, filename):
44  scripts.append(os.path.join(path, filename))
45  scripts.sort()
46  return scripts
47 
48 
49 @contextmanager
51  '''Simple pushd/popd replacement from https://stackoverflow.com/a/169112'''
52  curdir = os.getcwd()
53  try:
54  yield
55  finally:
56  os.chdir(curdir)
57 
58 
59 def first_existing_file(file_list):
60  '''
61  Returns the first file name from the list which corresponds to an existing file.
62  Returns None if none of the files in the list exist.
63  '''
64  for file_name in file_list:
65  if os.path.isfile(file_name):
66  return file_name
67  return None
68 
69 
70 def newest_file(pattern):
71  '''
72  Returns the newest file (by modification date) in the current directory
73  with a name matching the pattern. Returns None if no file is matched.
74  '''
75  all_files = os.listdir('.')
76  rx = re.compile(pattern)
77  matched_files = [f for f in all_files if re.search(rx, f)]
78  if not matched_files:
79  return None
80  matched_files.sort(key=lambda f: os.stat(f).st_mtime)
81  return matched_files[-1]
vtune_athena.format
format
Definition: vtune_athena.py:14
python.TrigARTUtils.package_prefix
def package_prefix(package)
Definition: TrigARTUtils.py:12
python.TrigARTUtils.newest_file
def newest_file(pattern)
Definition: TrigARTUtils.py:70
python.TrigARTUtils.find_scripts
def find_scripts(patterns)
Definition: TrigARTUtils.py:30
python.TrigARTUtils.first_existing_file
def first_existing_file(file_list)
Definition: TrigARTUtils.py:59
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.TrigARTUtils.duplicate_filename
def duplicate_filename(list, filename)
Definition: TrigARTUtils.py:23
python.TrigARTUtils.remember_cwd
def remember_cwd()
Definition: TrigARTUtils.py:50
Trk::split
@ split
Definition: LayerMaterialProperties.h:38