DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
utils.py
Go to the documentation of this file.
1import glob
2import logging
3import os
4import random
5import socket
6from rich.logging import RichHandler
7
8
9log_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
10
11
12def setup_logging(level:str="INFO"):
13 level = level.upper()
14
15 loglevel = logging.INFO
16
17 match level:
18 case "DEBUG":
19 loglevel = logging.DEBUG
20 case "INFO":
21 loglevel = logging.INFO
22 case "WARNING":
23 loglevel = logging.WARNING
24 case "ERROR":
25 loglevel = logging.ERROR
26 case "CRITICAL":
27 loglevel = logging.CRITICAL
28 case _:
29 loglevel = logging.INFO
30
31 FORMAT = "%(message)s"
32 logging.basicConfig(
33 level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
34 )
35 logging.getLogger().setLevel(loglevel)
36
37
38def find_oksincludes(includes:list[str], extra_dirs:list[str] = []):
39 includefiles = []
40
41 searchdirs = [path for path in os.environ["DUNEDAQ_DB_PATH"].split(":")]
42 for dir in extra_dirs:
43 searchdirs.append(dir)
44
45 for inc in includes:
46 # print (f"Searching for {inc}")
47 match = False
48 inc = inc.removesuffix(".xml")
49 if inc.endswith(".data"):
50 sub_dirs = ["config", "data"]
51 elif inc.endswith(".schema"):
52 sub_dirs = ["schema"]
53 else:
54 sub_dirs = ["*"]
55 inc = inc + "*"
56 for path in searchdirs:
57 # print (f" {path}/{inc}.xml")
58 matches = glob.glob(f"{inc}.xml", root_dir=path)
59 if len(matches) == 0:
60 for search_dir in sub_dirs:
61 # print (f" {path}/{search_dir}/{inc}.xml")
62 matches = glob.glob(f"{search_dir}/{inc}.xml", root_dir=path)
63 for filename in matches:
64 if filename not in includefiles:
65 print(f"Adding {filename} to include list")
66 includefiles.append(filename)
67 #else:
68 # print(f"{filename} already in include list")
69 match = True
70 break
71 if match:
72 break
73 if match:
74 break
75 else:
76 for filename in matches:
77 if filename not in includefiles:
78 print(f"Adding {filename} to include list")
79 includefiles.append(filename)
80 #else:
81 # print(f"{filename} already in include list")
82 match = True
83 break
84
85 if not match:
86 print(f"Error could not find include file for {inc}")
87 return [False, []]
88
89 return [True, includefiles]
90
91# This function returns a random available network port. Users can optionally
92# specify a range that should be used.
93def find_free_port(min_port_num:int=0, max_port_num:int=65535):
94 # If the user didn't specify a minimum port number (or deliberately specified
95 # zero), we can simply ask the system for an available port.
96 if min_port_num == 0:
97 with socket.socket() as s:
98 s.bind(("", 0))
99 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
100 port = s.getsockname()[1]
101 s.close()
102 return port
103 # If the user specified a minimum port number, use the specified range.
104 else:
105 if min_port_num < 1024:
106 min_port_num = 1024
107 while True:
108 port = random.randint(min_port_num, max_port_num)
109 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
110 try:
111 s.bind(("0.0.0.0", port))
112 return port
113 except OSError:
114 continue
find_oksincludes(list[str] includes, list[str] extra_dirs=[])
Definition utils.py:38
setup_logging(str level="INFO")
Definition utils.py:12
find_free_port(int min_port_num=0, int max_port_num=65535)
Definition utils.py:93