-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathscript.py
653 lines (526 loc) · 17.3 KB
/
script.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
# http://inamidst.com/saxo/
# Created by Sean B. Palmer
import argparse
import codecs
import os
import signal
import sys
import threading
import time
# Save PEP 3122!
if "." in __name__:
from . import common
else:
import common
sys.stderr = codecs.getwriter("utf-8")(sys.stderr.detach())
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
E_PYTHON_VERSION = """
Your version of the python programming language is too old to run saxo. Use a
newer version if available. Otherwise you can get a newer version from here:
http://www.python.org/
Or install one using your system's package manager. If you are using OS X for
example, python3 can be installed using homebrew:
http://mxcl.github.io/homebrew/
"""
try: import sqlite3
except ImportError:
print("Error: sqlite3 is not installed", file=sys.stderr)
print("Please build Python against the sqlite libraries", file=sys.stderr)
sys.exit(1)
else:
if not sqlite3.threadsafety:
print("Error: Your sqlite3 is not thread-safe", file=sys.stderr)
sys.exit(1)
if sys.version_info < (3, 3):
common.error("requires python 3.3 or later", E_PYTHON_VERSION)
def action(function):
action.names[function.__name__] = function
return function
action.names = {}
lock = threading.Lock()
def debug(*args, **kargs):
with lock:
try:
print(*args, **kargs)
sys.stdout.flush()
except BrokenPipeError:
sys.exit()
# Options
def version(args, v):
print("saxo %s" % v)
USAGE = """
Usage:
saxo -v
saxo create [ directory ]
saxo [ -f ] [ --log ] start [ directory ]
saxo stop [ directory ]
saxo active [ directory ]
Try `saxo -h` for more detailed usage
"""
def usage(args, v):
version(args, v)
print(USAGE.rstrip())
HELP = """
Usage:
saxo -v / --version
Show the current saxo version
saxo create [ directory ]
Create a default configuration file
saxo [ Options ] start [ directory ]
Starts a bot. Options:
-f / --foreground
Don't run the bot as a daemon
-l / --log
Save output to a log file in saxo base
saxo stop [ directory ]
Stops a bot
saxo active [ directory ]
Shows whether a bot is active
"""
def help(args, v):
version(args, v)
print(HELP.rstrip())
def base_option(args):
if args.directory is None:
return os.path.expanduser("~/.saxo")
return args.directory
# Actions
# eval $(./saxo bash)
@action
def bash(arg):
# Save PEP 3122!
if "." in __name__:
from . import saxo
else:
import saxo
import pipes
path = pipes.quote(os.path.join(saxo.path, "commands"))
print('export PATH=$PATH:%s' % path)
pythonpath = pipes.quote(saxo.path)
if "PYTHONPATH" in os.environ:
print('export PYTHONPATH=$PYTHONPATH:%s' % pythonpath)
else:
print('export PYTHONPATH=%s' % pythonpath)
@action
def create(args):
# Save PEP 3122!
if "." in __name__:
from . import create
else:
import create
# def default(base=None)
# — create.py
create.default(args.directory)
return 0
@action
def shell(args):
base = base_option(args)
# Save PEP 3122!
if "." in __name__:
from .saxo import path as saxo_path
else:
from saxo import path as saxo_path
def subshell(base, commands):
path = os.environ.get("PATH", "")
os.environ["PATH"] = commands + os.pathsep + path
os.environ["PYTHONPATH"] = saxo_path
os.environ["SAXO_SHELL"] = "1"
os.environ["SAXO_BASE"] = base
os.environ["SAXO_BOT"] = "saxo"
os.environ["SAXO_COMMANDS"] = commands
os.environ["SAXO_NICK"] = os.environ["USER"]
os.environ["SAXO_SENDER"] = os.environ["USER"]
shell = os.environ.get("SHELL", "sh")
os.system(shell)
commands = os.path.join(base, "commands")
subshell(base, commands)
return 0
@action
def console(args):
# TODO: Reduce code duplication
base = base_option(args)
# Save PEP 3122!
if "." in __name__:
from . import saxo
else:
import saxo
path = os.environ.get("PATH", "")
commands = os.path.join(base, "commands")
# TODO: Reload symlinks
# e.g. the pip-installation test changes them
os.environ["PATH"] = commands + os.pathsep + path
os.environ["PYTHONPATH"] = saxo.path
os.environ["SAXO_SHELL"] = "1"
os.environ["SAXO_BASE"] = base
os.environ["SAXO_BOT"] = "saxo"
os.environ["SAXO_COMMANDS"] = commands
os.environ["SAXO_NICK"] = os.environ["USER"]
os.environ["SAXO_SENDER"] = os.environ["USER"]
def do(cmd, arg):
import subprocess
cmd = os.path.join(os.environ["SAXO_COMMANDS"], cmd)
try: octets = subprocess.check_output([cmd, arg])
except Exception as err:
return "Error: %s" % err
return octets.decode("utf-8")
while True:
sys.stdout.write("> ")
sys.stdout.flush()
try: i = sys.stdin.readline()
except: break
i = i.rstrip("\r\n")
if " " in i:
cmd, arg = i.split(" ", 1)
else:
cmd, arg = i, ""
if cmd.startswith("."):
saxo.client(cmd[1:], *eval("(%s)" % arg))
elif cmd.startswith("!"):
print(do(cmd[1:], arg).rstrip("\r\n"))
elif cmd == "saxo.path":
print(saxo.path)
elif cmd == "saxo.base":
print(base)
elif not cmd:
break
else:
print("Unknown command:", repr(cmd))
@action
def log(args):
base = base_option(args)
print(os.path.join(base, "log"))
@action
def start(args):
import json
# Quit any previously connected instances
if "." in __name__:
from .saxo import client, data
else:
from saxo import client, data
base = base_option(args)
try: client("quit", base=base)
except FileNotFoundError as err:
...
except ConnectionRefusedError as err:
...
except PermissionError as err:
debug("Error: Unable to connect to internal socket", file=sys.stderr)
debug("Check permissions on the config dir", file=sys.stderr)
sys.stderr.flush()
sys.exit(1)
else:
debug("Warning: Client may already have been running!")
sys.stdout.flush()
if not os.path.isdir(base):
debug("Error: The saxo config dir does not exist", file=sys.stderr)
debug("Location: %s" % base, file=sys.stderr)
sys.stderr.flush()
sys.exit(1)
pidfile = os.path.join(base, "pid")
if not args.foreground:
# Save PEP 3122!
if "." in __name__:
from . import daemon
else:
import daemon
if args.log:
log = os.path.join(base, "log")
if os.path.exists(log):
import shutil
modified = time.gmtime(os.path.getmtime("saxo"))
log2 = time.strftime("log-%Y%m%d-%H%M%S.txt", modified)
shutil.move(log, os.path.join(base, log2))
output = open(log, "w")
else:
output = open(os.devnull, "w")
daemon.start(pidfile, output)
else:
# This is duplicated variatim from daemon.py
import atexit
with open(pidfile, "w") as f:
f.write(str(os.getpid()) + "\n")
def delete_pidfile():
if os.path.isfile(pidfile):
os.remove(pidfile)
atexit.register(delete_pidfile)
if args.action == "start":
# Otherwise you get recursion if args.action == "restart"
os.environ["SAXO_BASE"] = base
sys_args = json.dumps(args.original)
data("args", sys_args, command="script.py", check=False)
# Save PEP 3122!
if "." in __name__:
from . import irc
else:
import irc
irc.start(base)
return 0
@action
def restart(args):
import json
base = base_option(args)
os.environ["SAXO_BASE"] = base
if "." in __name__:
from .saxo import data, version
else:
from saxo import data, version
sys_args = json.loads(data("args", command="script.py", check=False))
if sys_args[0] == "restart":
debug("Recursion detected!", sys_args)
debug("Not restarting")
sys.exit(1)
debug("Stopping existing instance...")
stop(args)
argv = [sys.argv[0]] + sys_args
debug("Restarting using:", argv)
main(argv, version)
@action
def status(args):
base = base_option(args)
# Quit any previously connected instances
if "." in __name__:
from .saxo import client
else:
from saxo import client
try: client("noop", base=base)
except FileNotFoundError as err:
debug("not running")
except ConnectionRefusedError as err:
debug("not running")
else:
debug("running")
@action
def stop(args):
base = base_option(args)
if "." in __name__:
from .saxo import client
else:
from saxo import client
try: client("quit", base=base)
except: ...
pidfile = os.path.join(base, "pid")
if not os.path.exists(pidfile):
common.error("There is no bot currently running")
with open(pidfile, encoding="ascii") as f:
text = f.read()
pid = int(text.rstrip("\n"))
# TODO: Make this less crude
# This can give a ProcessLookupError: [Errno 3] No such process
try: os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
...
# os.kill(pid, signal.SIGKILL)
return 0
@action
def test(args):
if args.directory is not None:
common.error("Tests cannot be run in conjunction with a directory")
import queue
import shutil
import socket
import subprocess
import tempfile
# Save PEP 3122!
if "." in __name__:
from . import saxo
else:
import saxo
saxo_script = sys.modules["__main__"].__file__
saxo_test_server = os.path.join(saxo.path, "test", "server.py")
tmp = tempfile.mkdtemp()
outgoing = queue.Queue()
if not sys.executable:
common.error("Couldn't find the python executable")
if not os.path.isdir(tmp):
common.error("There is no %s directory" % tmp)
print("python executable:", sys.executable)
print("saxo path:", saxo.path)
print("saxo script:", saxo_script)
print("saxo test server:", saxo_test_server)
print()
sys.stdout.flush()
def run_server():
server = subprocess.Popen([sys.executable, "-u", saxo_test_server],
stdout=subprocess.PIPE)
for line in server.stdout:
line = line.decode("utf-8", "replace")
line = line.rstrip("\n")
outgoing.put("S: " + line)
outgoing.put("Server finished")
def run_client():
saxo_test = os.path.join(tmp, "saxo-test")
outgoing.put("Running in %s" % saxo_test)
cmd = [sys.executable, saxo_script, "create", saxo_test]
code = subprocess.call(cmd)
if code:
print("Error creating the client configuration")
sys.exit(1)
test_config = os.path.join(saxo.path, "test", "config")
saxo_test_config = os.path.join(saxo_test, "config")
with open(test_config) as f:
with open(saxo_test_config, "w") as w:
for line in f:
line = line.replace("localhost", socket.gethostname())
w.write(line)
# shutil.copy2(test_config, saxo_test_config)
client = subprocess.Popen([sys.executable, "-u",
saxo_script, "-f", "start", saxo_test],
stdout=subprocess.PIPE)
for line in client.stdout:
line = line.decode("utf-8", "replace")
line = line.rstrip("\n")
outgoing.put("C: " + line)
manifest01 = {"commands", "config", "database.sqlite3",
"pid", "plugins"}
manifest02 = manifest01 | {"client.sock"}
if set(os.listdir(saxo_test)) <= manifest01:
shutil.rmtree(saxo_test)
elif set(os.listdir(saxo_test)) <= manifest02:
outgoing.put("Warning: client.sock had not been removed")
shutil.rmtree(saxo_test)
else:
outgoing.put("Refusing to delete the saxo test directory")
outgoing.put("Data was found which does not match the manifest")
outgoing.put(saxo_test)
common.thread(run_server)
common.thread(run_client)
error = False
completed = False
client_buffer = []
while True:
line = outgoing.get()
if line.startswith("S: "):
print(line)
if line.startswith("S: ERROR"):
error = True
if line.startswith("S: Tests complete"):
completed = True
if not line.startswith("S: Test"):
for c in client_buffer:
print(c)
del client_buffer[:]
elif line.startswith("C: "):
client_buffer.append(line)
else:
print(line)
sys.stdout.flush()
if line == "Server finished":
break
if not os.listdir(tmp):
os.rmdir(tmp)
else:
print("Warning: Did not remove:", tmp)
if completed and (not error):
sys.exit(0)
else:
sys.exit(1)
@action
def soft(args):
import site
if args.directory == "on":
# Save PEP 3122!
if "." in __name__:
from .saxo import path as saxo_path
else:
from saxo import path as saxo_path
if not os.path.isdir(site.USER_SITE):
os.makedirs(site.USER_SITE)
debug("Warning: Created", site.USER_SITE)
saxo_pth = os.path.join(site.USER_SITE, "saxo.pth")
if not os.path.exists(saxo_pth):
with open(saxo_pth, "w") as f:
f.write(saxo_path)
debug("Created saxo.pth in", site.USER_SITE)
else:
debug("Error: %s already exists" % saxo_pth, file=sys.stderr)
sys.exit(1)
elif args.directory == "off":
saxo_pth = os.path.join(site.USER_SITE, "saxo.pth")
if os.path.isfile(saxo_pth):
os.remove(saxo_pth)
else:
debug("Error: %s not found" % saxo_pth, file=sys.stderr)
sys.exit(1)
debug("Warning: May also need to remove %s" % site.USER_SITE)
else:
debug("Error: Was expecting 'on' or 'off'", file=sys.stderr)
sys.exit(1)
@action
def info(args):
# Save PEP 3122!
if "." in __name__:
from .saxo import path as saxo_path
else:
from saxo import path as saxo_path
directory = args.directory
args.directory = None
base = base_option(args)
args.directory = directory
def check(path):
if os.path.isdir(path):
print(path)
else:
debug("-", file=sys.stderr)
sys.exit(1)
if args.directory == "base":
check(base)
elif args.directory == "path":
check(saxo_path)
elif args.directory == "base.commands":
check(os.path.join(base, "commands"))
elif args.directory == "path.commands":
check(os.path.join(saxo_path, "commands"))
else:
debug("?", file=sys.stderr)
sys.exit(1)
def main(argv, v):
# NOTE: No default for argv, because what script name would we use?
description = "Control saxo irc bot instances"
parser = argparse.ArgumentParser(description=description, add_help=False)
parser.add_argument("-f", "--foreground", action="store_true",
help="run in the foreground instead of as a daemon")
parser.add_argument("-h", "--help", action="store_true",
help="show a short help message")
parser.add_argument("-o", "--output", metavar="filename",
help="removed command: use -l / --log with no arg instead")
parser.add_argument("-l", "--log", action="store_true",
help="log to the standard log file")
parser.add_argument("-v", "--version", action="store_true",
help="show the current saxo version")
parser.add_argument("action", nargs="?",
help="use --help to show the available actions")
parser.add_argument("directory", nargs="?",
help="the path to the saxo configuration directory")
args = parser.parse_args(argv[1:])
args.original = argv[1:]
if args.output:
print("Sorry, -o / --output has been removed!")
print("Use -l / --log instead, with no arguments")
sys.exit(2)
if args.help:
help(args, v)
elif args.version:
version(args, v)
elif args.action:
if args.action in action.names:
code = action.names[args.action](args)
if isinstance(code, int):
sys.exit(code)
elif args.action.startswith("."):
# Save PEP 3122!
if "." in __name__:
from . import saxo
else:
import saxo
def do(cmd, arg):
import subprocess
cmd = os.path.join(saxo.path, "commands", cmd)
try: octets = subprocess.check_output([cmd, arg])
except Exception as err:
raise err # return "Error: %s" % err
return octets.decode("utf-8").rstrip("\r\n")
print(do(args.action[1:], args.directory or ""))
else:
common.error("unrecognised action: %s" % args.action, code=2)
else:
usage(args, v)