-
Notifications
You must be signed in to change notification settings - Fork 12
/
sloshy.py
executable file
·859 lines (739 loc) · 30.4 KB
/
sloshy.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
#!/usr/bin/env python3
"""
Sloshy the Thawman - main class
"""
from datetime import datetime, timedelta
from os import environ
import random
from time import sleep
import platform
import logging
import yaml
from chatexchange.client import Client as ChExClient, ChatActionError
from requests.exceptions import RequestException
from scrape_chat import Transcript, TranscriptFrozenDeletedException, RepScrape
DEFAULT_MAX_AGE = {'days': 12}
AGGRESSIVE_MAX_MSG_THRESHOLD = 15
DEFAULT_AGGRESSIVE_MAX_AGE = {'days': 5}
class SchemaError(Exception):
pass
class LocalClientRequestQueue:
"Quick hack to support logout() method"
def empty(self):
return True
def td_no_us(then: datetime, when: datetime|None = None) -> timedelta:
"""Return a timedelta between then and now (or when, if specified)
without microseconds"""
if when is None:
when = datetime.now()
td = when - then
return td - timedelta(microseconds=td.microseconds)
class LocalClient:
"""
Simple mock ChatExchange.client with no actual networking functionality
"""
def __init__(self, host='stackexchange.com', email=None, password=None):
self.host = host
self.email = email
self.password = password
self.room = None
self._request_queue = LocalClientRequestQueue()
def login(self, email=None, password=None):
logging.info('local - not logging in to %s', self.host)
def get_room(self, id: int):
logging.info('local - not fetching %s/rooms/%s', self.host, id)
self.room = id
return self
def join(self):
assert self.room is not None
logging.info('local - not joining %s/rooms/%i', self.host, self.room)
def send_message(self, message: str):
assert self.room is not None
logging.info(
'local - %s/rooms/%i: not sending message', self.host, self.room)
if message:
print(message)
else:
print('(Not printing any message, just checking ability to join)')
def logout(self):
logging.info('local - %s/rooms/%i: logging out', self.host, self.room)
class Chatclients:
"""
Singleton to keep track of chat servers, with one client for each.
"""
def __init__(self, local=False):
"""
Create a dispatcher for the servers we know about.
With local=True, create a LocalClient instead of a real
chatexchange one.
"""
self.servers = dict()
self.email = None
self.password = None
self.local = local
self.rep_check = None
def authnz(self, email: str, password: str):
self.email = email
self.password = password
def get_client_for_server(self, server: str) -> ChExClient:
assert self.email is not None
assert self.password is not None
if server not in self.servers:
assert server.startswith('chat.')
site = server[5:]
if self.local:
client = LocalClient(site)
else:
self.perform_rep_check()
client = ChExClient(site)
client.login(self.email, self.password)
self.servers[server] = client
return self.servers[server]
def perform_rep_check(self) -> RepScrape:
"""
Fetch Sloshy's network profile page's accounts tab
and check that relevant accounts have enough reputation
points to participate in chat (requires 30 rep).
"""
if self.rep_check is None:
self.rep_check = RepScrape(21818820)
return self.rep_check
def logout(self):
"""
Log out from all the chat clients.
"""
for server, client in self.servers.items():
logging.info("Logging out from %s", server)
client.logout()
queue = client._request_queue
while not queue.empty():
logging.info(
"Waiting for %s queue to drain (%i items in queue)",
server, queue.qsize())
sleep(15)
self.servers = dict()
class Room:
"""
Chat room with server, id, and a name for human-readable messages.
Also, include Sloshy's chat id for this server, and a handle to a
Chatclients object which instantiates clients as necessary.
"""
def __init__(
self,
server: str,
id: int,
name: str,
sloshy_id: int | None,
clients: Chatclients
):
self.server = server
self.id = id
self.name = name
self.log_id = 'https://%s/rooms/%i' % (server, id)
self.escaped_name = name.replace('[', r'\[').replace(']', r'\]')
self.linked_name = '[%s](%s)' % (self.escaped_name, self.log_id)
# sloshy_id can be None when migrating an old schema
self.sloshy_id = sloshy_id
self.clients = clients
self.homeroom = False
self.ccroom = False
self._chatroom = None
self._scan_start = None
self._scan_end = None
def scan_duration(self, no_ms: bool = False) -> timedelta:
"""Return scan duration as a timedelta. If no_ms is True,
return without ms."""
if self._scan_start is None or self._scan_end is None:
return timedelta(0)
if no_ms:
diff = td_no_us(self._scan_start, self._scan_end)
else:
diff = self._scan_end - self._scan_start
return diff
def set_as_home_room(self):
self.homeroom = True
def is_home_room(self) -> bool:
return self.homeroom
def set_as_cc_room(self):
self.ccroom = True
def is_cc_room(self):
return self.ccroom
def transcript_url(self) -> str:
return 'https://%s/transcript/%i' % (self.server, self.id)
def send_message(self, message: str) -> None:
if self._chatroom is None:
chat = self.clients.get_client_for_server(self.server)
room = chat.get_room(self.id)
room.join()
self._chatroom = room
else:
room = self._chatroom
if message:
room.send_message(message)
def get_sloshy_id(self) -> int:
return self.sloshy_id
class Sloshy:
def __init__(
self,
conffile=None,
local=False,
verbose=False,
nodename=None,
location_extra=None
):
"""
Create a Sloshy instance by parsing the supplied YAML file.
With local=True, don't connect to chat rooms.
With verbose=True, emit room status messages on standard output.
With nodename, specify a node name to display in the greeting,
overriding whatever the configuration file says. (The default is
the output from nodename().)
With location_extra, specify additional location information in
the greeting. This is intended to allow you to indicate a platform,
like for example Github Actions or CircleCI, where the nodename
alone is rather uninformative. (The default is None.)
"""
self.conffile = conffile
self.local = local
self.verbose = verbose
self.rooms = []
self.homeroom = None
self.ccrooms = []
self.chatclients = None
self.email = ''
self.password = ''
if conffile:
self.load_conf(conffile)
if 'SLOSHY_EMAIL' in environ:
self.email = environ.get('SLOSHY_EMAIL')
if 'SLOSHY_PASSWORD' in environ:
self.password = environ.get('SLOSHY_PASSWORD')
self.chatclients.authnz(self.email, self.password)
if nodename is not None:
self.config['nodename'] = nodename
if location_extra is not None:
self.config['location_extra'] = location_extra
def load_conf(self, conffile=None):
"""
Load YAML config from self.conffile, or the given file if specified
"""
if conffile is None:
assert self.conffile is not None
conffile = self.conffile
with open(conffile, 'r') as filehandle:
config = yaml.safe_load(filehandle)
warning = None
if 'schema' not in config:
warning = 'Config file does not contain key "schema". ' \
' Run with --migrate?'
if config['schema'] != 20230827:
warning = 'Config file uses old schema %i; run with --migrate?' % (
config['schema'])
if warning is not None:
logging.error(warning)
raise SchemaError(warning)
assert 'servers' in config
self.config = config
self.rooms = []
self.homeroom = None
self.ccrooms = []
if not self.local and 'local' in config:
self.local = bool(config['local'])
clients = Chatclients(local=self.local)
self.chatclients = clients
for server in self.config['servers']:
if server in self.chatclients.servers:
logging.warning(
'Skipping duplicate server %s in %s', server, conffile)
continue
assert 'sloshy_id' in self.config['servers'][server]
sloshy_id = self.config['servers'][server]['sloshy_id']
assert 'rooms' in self.config['servers'][server]
seen = set()
for room in self.config['servers'][server]['rooms']:
assert 'id' in room
assert 'name' in room
if room['id'] in seen:
logging.warning(
'Skipping duplicate room %s:%s (%s) in %s',
server, room['id'], room['name'], conffile)
continue
seen.add(room['id'])
roomobj = Room(
server, room['id'], room['name'], sloshy_id, clients)
self.rooms.append(roomobj)
if 'role' in room:
if room['role'] == 'home':
assert self.homeroom is None
self.homeroom = roomobj
roomobj.set_as_home_room()
elif room['role'] == 'cc':
roomobj.set_as_cc_room()
self.ccrooms.append(roomobj)
assert self.homeroom is not None
if 'auth' in config:
self.email = config['auth']['email']
self.password = config['auth']['password']
# Gripe a bit, we don't want users to embed authnz in the file
logging.warning('Chat username and password read from config')
def write_conf(self, config=None):
"""
Write Sloshy's configuration, or the specified configuration,
to self.conffile as YAML.
"""
if config is None:
config = self.config
with open(self.conffile, 'w', encoding='utf-8') as newconf:
yaml.dump(config, newconf)
def migrate(self):
"""
Rewrite older-format configuration file using new YAML schema.
The meat is in the migrated_config method which reads the old
config and returns the new format.
"""
self.write_conf(self.migrated_config())
def migrated_config(self):
"""
No-op in the base class. Subclasses should inherit and provide
a migration path to the new base class.
"""
warning = 'Already at the newest version of the schema %s' % (
self.config['schema'])
logging.error(warning)
raise SchemaError(warning)
# return self.config
def nodename(self):
"""
Produce a string which identifies where Sloshy is running,
for the startup message in the monitoring room.
If the configuration file contains a node name, use that;
otherwise, return platform.node()
"""
if 'nodename' not in self.config:
self.config['nodename'] = platform.node()
base_nodename = self.config['nodename']
if 'location_extra' in self.config:
return '%s (%s)' % (base_nodename, self.config['location_extra'])
return base_nodename
def generate_chat_message(self):
"""
Output a random witty, witty unfreeze message.
Starting with PR#23 (December 2021), include a tag to make these
easier to search for.
"""
return '[tag:unfreeze] ' + random.choice((
'thaw',
'sprinkling antifreeze',
'!freeze',
'♫ the heat never bothered me anyway🎶',
'Kilr^WSloshy the Thawman was here!'))
def send_chat_message(self, room: Room, message: str):
"""
Send chat message. If not self.local, sleep after sending.
"""
assert self.chatclients is not None
room.send_message(message)
if not self.local:
sleep(3)
def startup_notice(self, room: Room, message: str):
"""
Send a startup message to the specified room.
The startup message has a link to the Github repo and
includes the output from self.nodename().
"""
self.send_chat_message(
room, '[Sloshy](%s) startup: %s on %s' % (
'https://github.com/tripleee/sloshy',
message,
self.nodename()))
def notice(self, room: Room):
"""
Drop a thawing notice in room
"""
self.send_chat_message(room, self.generate_chat_message())
def log_notice(self, message: str, log_emit=logging.info, cc: bool = False):
"""
Emit a log message to the home room, and as a warning.
With cc=True, also emit to cc room(s).
"""
log_emit(message)
self.send_chat_message(self.homeroom, message)
if self.verbose:
print(str(datetime.now()), message)
if cc:
for ccroom in self.ccrooms:
self.send_chat_message(ccroom, message)
log_warn = lambda self, x, cc=False: self.log_notice(
x, log_emit=logging.warning, cc=True)
log_error = lambda self, x, cc=False: self.log_notice(
x, log_emit=logging.error, cc=True)
def test_rooms(self, announce: None|str = None, randomize: bool = False):
"""
Test write access in all the rooms in the configuration.
If announce is missing / None, simply try to enter each room
in turn, but don't post any message.
Otherwise, check if Sloshy has already has posted a message to
each room in turn; if we have, regard it as tested. If not,
attempt to write the announcement message to the room in question.
If randomize = True, randomize the order of servers and rooms.
"""
start_time = datetime.now()
fetcher = Transcript()
counter = {'server': set(), 'id': set(), 'fail': set()}
self.startup_notice(self.homeroom, announce or "room test")
if randomize:
random.shuffle(self.rooms)
for room in self.rooms:
sloshy_id = room.get_sloshy_id()
if announce is None:
logging.info('Joining %s', room.linked_name)
try:
room.send_message("") # just join, don't send anything
except KeyError as exception:
# the immediate problem from a closed room is
# ...
# File "./sloshy.py", line 156, in send_message
# room.join()
# File ".../chatexchange/rooms.py", line 50, in join
# return self._client._join_room(self.id)
# File ".../chatexchange/client.py", line 340, in _join_room
# self._br.join_room(room_id)
# File ".../chatexchange/browser.py", line 265, in join_room
# eventtime = response.json()['time']
# KeyError: 'time'
self.log_error(
'** Error: could not join %s' % room.linked_name)
self.log_error(repr(exception))
counter['fail'].add(room.log_id)
sleep(30)
counter['server'].add(room.server)
counter['id'].add(room.log_id)
continue
found = None
for phrase in (
'tagged/unfreeze',
# Fall back to static search for keywords in legacy notices
'thaw', 'antifreeze', 'freeze', 'heat', 'thawman'):
try:
found = fetcher.search(
room.server, room.id, sloshy_id, phrase)
except RequestException as exception:
self.log_error(
'** Error: could not fetch transcript for %s'
% room.linked_name)
self.log_error(repr(exception))
counter['fail'].add(room.log_id)
if '429' in str(exception):
logging.info('Sleeping 600s after 429 errors')
sleep(600)
else:
logging.info('Sleeping 30s after error')
sleep(30)
break
if found:
logging.info('Found: %s', found)
break
# Sleeps established experimentally
# The site starts emitting 409 errors if we go too fast
# but it's not really clear what it regards as too fast
logging.info('Sleeping between searches ...')
sleep(3)
logging.info('Sleeping between searches ...')
sleep(3)
if not found:
try:
self.send_chat_message(room, '[tag:unfreeze] %s' % announce)
except KeyError as exception:
self.log_error(
'** Error: could not announce presence in %s'
% room.linked_name)
self.log_error(repr(exception))
counter['fail'].add(room.log_id)
continue
self.log_notice(
'announced presence in %s' % room.linked_name, cc=True)
if announce is None:
self.log_notice(
'Permissions check done in %s; '
'scanned %i rooms on %i servers' % (
td_no_us(start_time),
len(counter['id']), len(counter['server'])))
if len(counter['fail']) > 0:
raise ValueError('failed to process rooms %s' % counter['fail'])
def scan_rooms(
self,
startup_message: None|str = None,
slow_summary: bool = True,
randomize: bool = False):
"""
Main entry point for scanning: Visit room transcripts,
check if they are in danger of being frozen; if so, join
and post a message.
The startup_message is included in the notification in the
monitoring room when Sloshy starts up.
If it is empty, missing, or None, it defaults to "manual run".
If slow_summary is not true, skip summarizing the slowest rooms.
If randomize = True, randomize the order of servers and rooms.
"""
if not startup_message:
startup_message = "manual run"
start_time = datetime.now()
failures = set()
if randomize:
random.shuffle(self.rooms)
# Default freeze schedule is 14 days; thaw a little before that,
# just to be on the safe side.
if 'threshold' in self.config:
if isinstance(self.config['threshold'], str):
maxage = timedelta(*tuple(
int(x.strip())
for x in self.config['threshold'].split(",")))
else:
maxage = timedelta(self.config['threshold'])
else:
maxage = timedelta(**DEFAULT_MAX_AGE)
max_aggressive_age = timedelta(**DEFAULT_AGGRESSIVE_MAX_AGE)
fetcher = Transcript()
homeroom = self.homeroom
servers = set()
self.startup_notice(homeroom, startup_message)
for room in self.rooms:
if room.is_home_room() and 'scan_homeroom' not in self.config:
continue
servers.add(room.server)
room._scan_start = datetime.now()
try:
room_latest = fetcher.latest(room.id, room.server)
except (TranscriptFrozenDeletedException, RequestException
) as exception:
self.log_error(
'** Error: could not fetch transcript for %s'
% room.linked_name)
self.log_error(repr(exception))
if '429' in str(exception):
logging.info('Sleeping 120s after 429 errors')
sleep(120)
else:
logging.info('Sleeping 30s after error')
sleep(30)
failures.add(room.log_id)
continue
if room_latest:
when = room_latest['when']
age = td_no_us(when, start_time)
msg = '[%s](%s): latest activity %s (%s hours ago)' % (
room.escaped_name, room_latest['url'], when, age)
else:
msg = '[%s](%s): no non-feed, non-admin activity ever' % (
room.escaped_name, room.transcript_url())
age = maxage + timedelta(days=1)
self.log_notice(msg)
quiet = False
if age <= maxage and age > max_aggressive_age:
if room.sloshy_id is None:
logging.warning(
"Room %s (%i) has no Sloshy ID", room.name, room.id)
activity = fetcher.usercount(
room.id, room.server, userlimit=2,
messagelimit=AGGRESSIVE_MAX_MSG_THRESHOLD,
sloshy_id=room.sloshy_id)
quiet = not activity
if age > maxage or (quiet and age > max_aggressive_age):
try:
self.notice(room)
self.log_notice(
'Age threshold exceeded; sending a%s thawing notice '
'to %s' % ('' if age>maxage else 'n expedited',
room.linked_name), cc=True)
except ChatActionError as err:
self.log_error(
'%s: Age threshold exceeded, but failed to thaw: %s'
% (room.linked_name, err))
failures.add(room.id)
room._scan_end = datetime.now()
if failures:
raise ValueError('Failed to process %i rooms: %s' % (
len(failures), failures))
self.log_notice(
'Room scan done in %s; scanned %i rooms on %i servers' % (
td_no_us(start_time), len(self.rooms), len(servers)))
if slow_summary:
slowest = {room: room.scan_duration() for room in self.rooms}
for room in sorted(slowest, key=slowest.get, reverse=True)[0:5]:
if slowest[room] <= timedelta(seconds=60):
break
self.log_notice(
'Relatively slow room: %s (%s)' % (
room.linked_name, room.scan_duration(no_ms=True)))
def perform_scan(
self,
startup_message: None|str = None,
slow_summary: bool = True,
randomize: bool = False
):
"""
Entry point for a full scan: Perform room scan, then logout().
startup_message, slow_summary, and randomize are passed to scan_rooms().
"""
try:
self.scan_rooms(startup_message, slow_summary, randomize=randomize)
except Exception as exception:
raise
finally:
self.chatclients.logout()
class SloshyLegacyConfig20211215(Sloshy):
"""
Child class with the original configuration file processing
and a migration method for updating to the new format.
This actually now returns the 20230827 schema but the only
difference is the support for "role: cc" which simply didn't
exist in the earlier schemas, so we do not write it anywhere
when migrating.
"""
def load_conf(self, conffile=None):
"""
Load YAML config from self.conffile, or the given file if specified
"""
if conffile is None:
assert self.conffile is not None
conffile = self.conffile
with open(conffile, 'r') as filehandle:
config = yaml.safe_load(filehandle)
assert 'rooms' in config
self.config = config
self.rooms = []
self.homeroom = None
if not self.local and 'local' in config:
self.local = bool(config['local'])
clients = Chatclients(local=self.local)
self.chatclients = clients
for item in self.config['rooms']:
for server, rooms in item.items():
if server in self.chatclients.servers:
logging.warning(
'Duplicate server %s in %s', server, conffile)
seen = set()
for idx in range(len(item[server])):
room = item[server][idx]
assert 'id' in room
assert 'name' in room
if room['id'] in seen:
logging.warning(
'Skipping duplicate room %s:%s (%s) in %s',
server, room['id'], room['name'], conffile)
continue
seen.add(room['id'])
roomobj = Room(
server, room['id'], room['name'], None, clients)
self.rooms.append(roomobj)
if 'role' in room and room['role'] == 'home':
assert self.homeroom is None
self.homeroom = roomobj
roomobj.set_as_home_room()
assert self.homeroom is not None
if 'auth' in config:
self.email = config['auth']['email']
self.password = config['auth']['password']
# Gripe a bit, we don't want users to embed authnz in the file
logging.warning('Chat username and password read from config')
def migrated_config(self):
# Stupidly hard-code information which should be in the config
sloshy_id = {
'chat.stackoverflow.com': 16115299,
'chat.stackexchange.com': 514718,
'chat.meta.stackexchange.com': 1018361
}
servers = []
for serverdict in self.config['rooms']:
servers.extend(serverdict.keys())
for server in servers:
if server in sloshy_id:
continue
logging.warning(
'Server %s not known to migration code - abort', server)
assert all(x in sloshy_id for x in servers)
serverconfigs = dict()
for serverdict in self.config['rooms']:
for server in serverdict.keys():
serverconfigs[server] = {
'sloshy_id': sloshy_id[server],
'rooms': serverdict[server]
}
return {'schema': 20230827, 'servers': serverconfigs}
def main():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument(
'--migrate', action='store_true',
help='Migrate config from old schema to the current one, then exit.')
parser.add_argument(
'--announce',
help='Announce presence in any rooms where Sloshy has not appeared,'
' then exit. Requires announcement string as argument.')
parser.add_argument(
'--test-rooms', action='store_true',
help='Test access to rooms without announcing presence, then exit.')
parser.add_argument(
'--local', action='store_true',
help='Run locally; do not emit chat messages.')
parser.add_argument(
'--verbose', action='store_true',
help='Emit room status messages on standard output.')
parser.add_argument(
'--randomize', action='store_true',
help='Randomize list of servers and lists of rooms.')
parser.add_argument(
'--loglevel', default='info',
help='Logging level: debug, info (default), warning, error')
parser.add_argument(
'--nodename',
help='Specify nodename for Sloshy (overrides config file)')
parser.add_argument(
'--location_extra',
help='Provide additional context for location (like Github/CircleCI)')
parser.add_argument(
'--no-slow-summary', action='store_true',
help='Do not display a summary of slow rooms at the end')
parser.add_argument(
'conffile', nargs='?', default='',
help='Configuration file for Sloshy. Default is sloshy.yaml for'
' regular run, test.yaml for --test-rooms')
parser.add_argument(
'startup_message', nargs='?', default=None,
help='Message to display in the home room when starting up'
' (default: "manual run").')
args = parser.parse_args()
logging.basicConfig(
format='%(module)s:%(asctime)s:%(levelname)s:%(message)s',
level={
"debug": logging.DEBUG,
"info": logging.INFO,
"warn": logging.WARNING,
"error": logging.ERROR
}[args.loglevel])
if args.migrate:
SloshyLegacyConfig20211215(args.conffile or "sloshy.yaml").migrate()
exit(0)
conffile = args.conffile or "test.yaml"
me = Sloshy(
conffile, local=args.local, verbose=args.verbose,
nodename=args.nodename, location_extra=args.location_extra)
if args.announce or args.test_rooms:
if args.local:
raise ValueError(
'--local is incompatible with --announce and --test-rooms')
if args.announce and args.test_rooms:
raise ValueError(
'--announce and --test-rooms are mutually exclusive')
if args.no_slow_summary:
raise ValueError(
'--no-slow-summary is incompatible with --announce '
'and --test-rooms')
me.test_rooms(args.announce, randomize=args.randomize)
else:
me.perform_scan(
args.startup_message,
not args.no_slow_summary,
randomize=args.randomize)
if __name__ == '__main__':
main()