1
|
1 |
|
import os, sys |
2
|
1 |
|
from rsudp import COLOR, printM, printW, printE |
3
|
1 |
|
import socket |
4
|
1 |
|
import json |
5
|
|
|
|
6
|
1 |
|
SENDER = 'test.py' |
7
|
1 |
|
TEST = { |
8
|
|
|
# permissions |
9
|
|
|
'p_log_dir': ['log directory ', False], |
10
|
|
|
'p_log_std': ['stdout logging ', False], |
11
|
|
|
'p_log_file': ['logging to file ', False], |
12
|
|
|
'p_output_dirs': ['output directory structure ', False], |
13
|
|
|
'p_screenshot_dir': ['screenshot directory ', False], |
14
|
|
|
'p_data_dir': ['data directory ', False], |
15
|
|
|
|
16
|
|
|
# network |
17
|
|
|
'n_port': ['port ', False], |
18
|
|
|
'n_internet': ['internet ', False], |
19
|
|
|
'n_inventory': ['inventory (RS FDSN server) ', False], |
20
|
|
|
|
21
|
|
|
# core |
22
|
|
|
'x_packetize': ['packetizing data ', False], |
23
|
|
|
'x_send': ['sending data ', False], |
24
|
|
|
'x_data': ['receiving data ', False], |
25
|
|
|
'x_masterqueue': ['master queue ', False], |
26
|
|
|
'x_processing': ['processing data ', False], |
27
|
|
|
'x_ALARM': ['ALARM message ', False], |
28
|
|
|
'x_RESET': ['RESET message ', False], |
29
|
|
|
'x_IMGPATH': ['IMGPATH message ', False], |
30
|
|
|
'x_TERM': ['TERM message ', False], |
31
|
|
|
|
32
|
|
|
# dependencies |
33
|
|
|
'd_pydub': ['pydub dependencies ', False], |
34
|
|
|
'd_matplotlib': ['matplotlib backend ', False], |
35
|
|
|
|
36
|
|
|
# consumers |
37
|
|
|
'c_plot': ['plot ', False], |
38
|
|
|
'c_write': ['miniSEED write ', False], |
39
|
|
|
'c_miniseed': ['miniSEED data ', False], |
40
|
|
|
'c_print': ['print data ', False], |
41
|
|
|
'c_alerton': ['alert trigger on ', False], |
42
|
|
|
'c_alertoff': ['alert trigger off ', False], |
43
|
|
|
'c_play': ['play sound ', False], |
44
|
|
|
'c_img': ['screenshot exists ', False], |
45
|
|
|
'c_tweet': ['Twitter text message ', False], |
46
|
|
|
'c_tweetimg': ['Twitter image message ', False], |
47
|
|
|
'c_telegram': ['Telegram text message ', False], |
48
|
|
|
'c_telegramimg': ['Telegram image ', False], |
49
|
|
|
'c_forward': ['forwarding ', False], |
50
|
|
|
'c_rsam': ['RSAM transmission ', False], |
51
|
|
|
'c_custom': ['custom code execution ', False], |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
TRANS = { |
55
|
|
|
True: COLOR['green'] + 'PASS' + COLOR['white'], |
56
|
|
|
False: COLOR['red'] + 'FAIL' + COLOR['white'] |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
PORT = 18888 |
60
|
|
|
|
61
|
1 |
|
def make_test_settings(settings, inet=False): |
62
|
|
|
''' |
63
|
|
|
Get the default settings and return settings for testing. |
64
|
|
|
|
65
|
|
|
The default settings are modified in the following way: |
66
|
|
|
|
67
|
|
|
======================================== =================== |
68
|
|
|
Setting Value |
69
|
|
|
======================================== =================== |
70
|
|
|
``settings['settings']['station']`` ``'R24FA'`` |
71
|
|
|
``settings['printdata']['enabled']`` ``True`` |
72
|
|
|
``settings['alert']['threshold']`` ``2`` |
73
|
|
|
``settings['alert']['reset']`` ``0.5`` |
74
|
|
|
``settings['alert']['lowpass']`` ``9`` |
75
|
|
|
``settings['alert']['highpass']`` ``0.8`` |
76
|
|
|
``settings['plot']['channels']`` ``['all']`` |
77
|
|
|
``settings['plot']['duration']`` ``60`` |
78
|
|
|
``settings['plot']['deconvolve']`` ``True`` |
79
|
|
|
``settings['plot']['units']`` ``'CHAN'`` |
80
|
|
|
``settings['plot']['eq_screenshots']`` ``True`` |
81
|
|
|
``settings['write']['enabled']`` ``True`` |
82
|
|
|
``settings['write']['channels']`` ``['all']`` |
83
|
|
|
``settings['tweets']['enabled']`` ``True`` |
84
|
|
|
``settings['telegram']['enabled']`` ``True`` |
85
|
|
|
``settings['alertsound']['enabled']`` ``True`` |
86
|
|
|
``settings['rsam']['enabled']`` ``True`` |
87
|
|
|
``settings['rsam']['debug']`` ``True`` |
88
|
|
|
``settings['rsam']['interval']`` ``10`` |
89
|
|
|
======================================== =================== |
90
|
|
|
|
91
|
|
|
.. note:: |
92
|
|
|
|
93
|
|
|
If there is no internet connection detected, the station |
94
|
|
|
name will default to ``'Z0000'`` so that no time is wasted |
95
|
|
|
trying to download an inventory from the Raspberry Shake |
96
|
|
|
FDSN service. |
97
|
|
|
|
98
|
|
|
:param dict settings: settings dictionary (will be modified from :ref:`defaults`) |
99
|
|
|
:param bool inet: whether or not the internet test passed |
100
|
|
|
:rtype: dict |
101
|
|
|
:return: settings dictionary to test with |
102
|
|
|
''' |
103
|
1 |
|
settings = json.loads(settings) |
104
|
|
|
|
105
|
1 |
|
settings['settings']['port'] = PORT |
106
|
1 |
|
if inet: |
107
|
1 |
|
settings['settings']['station'] = 'R24FA' |
108
|
|
|
else: |
109
|
|
|
settings['settings']['station'] = 'Z0000' |
110
|
|
|
|
111
|
1 |
|
settings['printdata']['enabled'] = True |
112
|
|
|
|
113
|
1 |
|
settings['alert']['threshold'] = 2 |
114
|
1 |
|
settings['alert']['reset'] = 0.5 |
115
|
1 |
|
settings['alert']['lowpass'] = 9 |
116
|
1 |
|
settings['alert']['highpass'] = 0.8 |
117
|
|
|
|
118
|
1 |
|
settings['plot']['channels'] = ['all'] |
119
|
1 |
|
settings['plot']['duration'] = 60 |
120
|
1 |
|
settings['plot']['deconvolve'] = True |
121
|
1 |
|
settings['plot']['units'] = 'CHAN' |
122
|
1 |
|
settings['plot']['eq_screenshots'] = True |
123
|
|
|
|
124
|
1 |
|
settings['write']['enabled'] = True |
125
|
1 |
|
settings['write']['channels'] = ['all'] |
126
|
|
|
|
127
|
1 |
|
settings['telegram']['enabled'] = True |
128
|
1 |
|
settings['tweets']['enabled'] = True |
129
|
|
|
|
130
|
1 |
|
settings['alertsound']['enabled'] = True |
131
|
|
|
|
132
|
1 |
|
settings['forward']['enabled'] = True |
133
|
|
|
|
134
|
1 |
|
settings['rsam']['enabled'] = True |
135
|
1 |
|
settings['rsam']['quiet'] = False |
136
|
1 |
|
settings['rsam']['interval'] = 10 |
137
|
|
|
|
138
|
1 |
|
return settings |
139
|
|
|
|
140
|
|
|
|
141
|
1 |
|
def cancel_tests(settings, MPL, plot, quiet): |
142
|
|
|
''' |
143
|
|
|
Cancel some tests if they don't need to be run. |
144
|
|
|
|
145
|
|
|
:param dict settings: the dictionary of settings for program execution |
146
|
|
|
:param bool plot: whether or not to plot (``False`` == no plot) |
147
|
|
|
:param bool quiet: whether or not to play sounds (``True`` == no sound) |
148
|
|
|
:rtype: dict |
149
|
|
|
:return: settings dictionary to test with |
150
|
|
|
''' |
151
|
|
|
global TEST |
152
|
|
|
|
153
|
1 |
|
if plot: |
154
|
1 |
|
if MPL: |
155
|
1 |
|
TEST['d_matplotlib'][1] = True |
156
|
|
|
else: |
157
|
|
|
printW('matplotlib backend failed to load') |
158
|
|
|
else: |
159
|
|
|
settings['plot']['enabled'] = False |
160
|
|
|
del TEST['d_matplotlib'] |
161
|
|
|
del TEST['c_IMGPATH'] |
162
|
|
|
del TEST['c_img'] |
163
|
|
|
printM('Plot is disabled') |
164
|
|
|
|
165
|
1 |
|
if quiet: |
166
|
|
|
settings['alertsound']['enabled'] = False |
167
|
|
|
del TEST['d_pydub'] |
168
|
|
|
printM('Alert sound is disabled') |
169
|
|
|
|
170
|
1 |
|
if not settings['custom']['enabled']: |
171
|
1 |
|
del TEST['c_custom'] |
172
|
|
|
|
173
|
1 |
|
return settings |
174
|
|
|
|
175
|
|
|
|
176
|
1 |
|
def permissions(dp): |
177
|
|
|
''' |
178
|
|
|
Test write permissions for the specified directory. |
179
|
|
|
|
180
|
|
|
:param str dp: the directory path to test permissions for |
181
|
|
|
:rtype: bool |
182
|
|
|
:return: if ``True``, the test was successful, ``False`` otherwise |
183
|
|
|
''' |
184
|
1 |
|
dp = os.path.join(dp, 'test') |
185
|
1 |
|
try: |
186
|
1 |
|
with open(dp, 'w') as f: |
187
|
1 |
|
f.write('testing\n') |
188
|
1 |
|
os.remove(dp) |
189
|
1 |
|
return True |
190
|
|
|
except Exception as e: |
191
|
|
|
printE(e) |
192
|
|
|
return False |
193
|
|
|
|
194
|
1 |
|
def datadir_permissions(testdir): |
195
|
|
|
''' |
196
|
|
|
Test write permissions in the data directory (``./data`` by default) |
197
|
|
|
|
198
|
|
|
:param str testdir: The directory to test permissions for |
199
|
|
|
:rtype: bool |
200
|
|
|
:return: the output of :py:func:`rsudp.test.permissions` |
201
|
|
|
|
202
|
|
|
''' |
203
|
1 |
|
return permissions('%s/data/' % testdir) |
204
|
|
|
|
205
|
1 |
|
def ss_permissions(testdir): |
206
|
|
|
''' |
207
|
|
|
Test write permissions in the screenshots directory (``./screenshots`` by default) |
208
|
|
|
|
209
|
|
|
:param str testdir: The directory to test permissions for |
210
|
|
|
:rtype: bool |
211
|
|
|
:return: the output of :py:func:`rsudp.test.permissions` |
212
|
|
|
|
213
|
|
|
''' |
214
|
1 |
|
return permissions('%s/screenshots/' % testdir) |
215
|
|
|
|
216
|
1 |
|
def logdir_permissions(logdir='/tmp/rsudp'): |
217
|
|
|
''' |
218
|
|
|
Test write permissions in the log directory (``/tmp/rsudp`` by default) |
219
|
|
|
|
220
|
|
|
:param str logdir: The log directory to test permissions for |
221
|
|
|
:rtype: bool |
222
|
|
|
:return: the output of :py:func:`rsudp.test.permissions` |
223
|
|
|
''' |
224
|
1 |
|
return permissions(logdir) |
225
|
|
|
|
226
|
1 |
|
def is_connected(hostname): |
227
|
|
|
''' |
228
|
|
|
Test for an internet connection. |
229
|
|
|
|
230
|
|
|
:param str hostname: The hostname to test with |
231
|
|
|
:rtype: bool |
232
|
|
|
:return: ``True`` if connection is successful, ``False`` otherwise |
233
|
|
|
''' |
234
|
1 |
|
try: |
235
|
|
|
# see if we can resolve the host name -- tells us if there is |
236
|
|
|
# a DNS listening |
237
|
1 |
|
host = socket.gethostbyname(hostname) |
238
|
|
|
# connect to the host -- tells us if the host is actually |
239
|
|
|
# reachable |
240
|
1 |
|
s = socket.create_connection((host, 80), 2) |
241
|
1 |
|
s.close() |
242
|
1 |
|
return True |
243
|
|
|
except: |
244
|
|
|
pass |
245
|
|
|
return False |
246
|
|
|
|
247
|
|
|
|