forked from wfarah/frb_detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
executable file
·191 lines (167 loc) · 5.33 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import sys
import atexit
from signal import SIGTERM,SIGKILL
import time
import logging
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
"""
def parse_cfg(cfg_file,tags):
config_dict = {}
for tag in tags:
config_dict[tag] = ["/home/wfarah/realtime_model"]
return config_dict
"""
###############################################################################
#
# Turn the calling process into a daemon
#
def daemonize(pidfile, logfile):
# standard input will always be directed to /dev/null
stdin = "/dev/null"
stdout = logfile
stderr = logfile
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit("Fork #1")
except OSError, e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit("Fork #2")
except OSError, e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(stdin, 'r')
so = file(stdout, 'a+')
se = file(stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
atexit.register(delpid,pidfile)
# write pidfile, enable a function to cleanup pid file upon crash
pid = str(os.getpid())
logging.debug("Writing pid file (pid %s)",pid)
file(pidfile,'w+').write("%s\n" % pid)
def parse_cfg(cfg_file,tags=None):
"""Function that returns config file with given tags as dictionar
Args:
cfg_file (str): full directory to config file
tags (list): list of tags to search the cgf_file
Returns:
config_dict (dict): dictionary with keys given in tags, and values
extracted from cfg_file. If one tag doesn't exist,
value corresponded will be None, else value is of
type str, or list if multiple values exist for
same key.
"""
if tags == None:
tags = []
with open(cfg_file) as o:
for line in o:
if line[0] in ["\n","#"]: continue
tags.append(line.split()[0])
config_dict = {}
with open(cfg_file) as o:
for line in o:
if line[0] in ["\n","#"]: continue
for tag in tags:
if tag in line:
i = line.split()
assert tag == i[0]
config_dict[tag] = []
for ii in i[1:]:
if "#" in ii: break
config_dict[tag].append(ii)
if len(config_dict[tag]) == 1:
config_dict[tag] = config_dict[tag][0]
tags.remove(tag)
for tag in tags:
logging.warning("Couldn't parse <"+tag+"> from "+cfg_file)
config_dict[tag] = None
return config_dict
def control_monitor(control_dir,script_name):
""" Function that writes pid into control folder, and constantly lists
directory for .quit file, and kills the script
Args:
control_dir (str): control directory specified in mopsr.cfg
script_name (str): name of script as it should show in ctrl direc
(without extension)
pid (int): process id
"""
while True:
time.sleep(0.5)
lst = os.listdir(control_dir)
if script_name+".quit" in lst:
logging.critical("Read .quit file, cleaning and exiting")
stop_daemon(control_dir+"/"+script_name+".pid")
def client_control_monitor(control_dir,script_name,bf_numb):
""" Same as control_monitor, but edited to check for the quit file
without the bp number as suffix """
script_name_suffix = script_name+"_"+str(bf_numb)
while True:
time.sleep(0.5)
lst = os.listdir(control_dir)
if script_name+".quit" in lst or\
script_name_suffix+".quit" in lst:
logging.critical("Read .quit file, cleaning and exiting")
stop_daemon(control_dir+"/"+script_name_suffix+".pid")
def delpid(pidfile):
logging.critical("Deleting pidfile")
os.remove(pidfile)
def stop_daemon(pidfile):
pf = file(pidfile,'r')
pid = int(pf.read().rstrip())
pf.close()
logging.critical("Trying to kill main thread %s ",pid)
os.kill(pid,SIGTERM)
time.sleep(4)
logging.critical("Main thread %s is still alive, sending SIGKILL",pid)
os.kill(pid,SIGKILL)
def sigHandler(signo,frame):
logging.critical("%s Recieved a SIGTERM, cleaning...",os.getpid())
sys.exit(1)
def create_xml_elem(msg_type,dump_dict=None):
if msg_type == "ok":
ok_tag = Element('frb_detector_message')
child = SubElement(ok_tag, 'reply')
child.text = "ok"
return tostring(ok_tag,encoding ='ISO-8859-1').replace("\n","")
elif msg_type == "fail":
fail_tag = Element('frb_detector_message')
child = SubElement(fail_tag, 'reply')
child.text = "fail"
return tostring(fail_tag,encoding='utf8').replace("\n","")
elif msg_type == "dump":
dump_tag = Element('frb_detector_message')
cmd = SubElement(dump_tag,'cmd')
cmd.text = 'dump'
start_utc = SubElement(dump_tag,'start_utc')
start_utc.text = dump_dict['start_utc']
end_utc = SubElement(dump_tag,'end_utc')
end_utc.text = dump_dict['end_utc']
dm = SubElement(dump_tag,'dm')
dm.text = dump_dict['dm']
beam_number = SubElement(dump_tag,'beam_number')
beam_number.text = dump_dict['beam_number']
utc = SubElement(dump_tag,'utc')
utc.text = dump_dict['utc']
snr = SubElement(dump_tag,'snr')
snr.text = dump_dict['snr']
probability = SubElement(dump_tag,'probability')
probability.text = dump_dict['probability']
return tostring(dump_tag,encoding='ISO-8859-1').replace("\n","")