ATLAS Offline Software
Loading...
Searching...
No Matches
TrigARTUtils.py
Go to the documentation of this file.
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
7import os
8import re
9from contextlib import contextmanager
10
11
12def 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
23def duplicate_filename(list, filename):
24 for path in list:
25 if os.path.basename(path) == filename:
26 return True
27 return False
28
29
30def 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
59def 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
70def 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]
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
first_existing_file(file_list)
duplicate_filename(list, filename)