-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
195 lines (146 loc) · 4.47 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
# Copyright 2013, Sean B. Palmer
# Source: http://inamidst.com/saxo/
import argparse
import os.path
import signal
import sys
import time
# Save PEP 3122!
if "." in __name__:
from . import generic
else:
import generic
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/
"""
if sys.version_info < (3, 3):
generic.error("requires python 3.3 or later", E_PYTHON_VERSION)
def action(function):
action.names[function.__name__] = function
return function
action.names = {}
# Options
def version(args, v):
print("saxo %s" % v)
USAGE = """
Usage:
saxo -v
saxo create [ directory ]
saxo [ -f ] [ -o filename ] 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
-o / --output filename
Redirect stdout and stderr to filename
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())
# Actions
@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 start(args):
if args.directory is None:
directory = os.path.expanduser("~/.saxo")
else:
directory = args.directory
if not args.foreground:
# Save PEP 3122!
if "." in __name__:
from . import daemon
else:
import daemon
if args.output is None:
output = open(os.devnull, "w")
elif args.output in {"-", "/dev/stdout"}:
output = sys.stdout
else:
output = open(args.output, "w")
pidfile = os.path.join(directory, "pid")
daemon.start(pidfile, output)
# Save PEP 3122!
if "." in __name__:
from . import client
else:
import client
client.start(directory)
return 0
@action
def stop(args):
if args.directory is None:
directory = os.path.expanduser("~/.saxo")
else:
directory = args.directory
pidfile = os.path.join(directory, "pid")
if not os.path.exists(pidfile):
generic.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
os.kill(pid, signal.SIGTERM)
# os.kill(pid, signal.SIGKILL)
return 0
def main(argv, v):
# NOTE: No default for argv, because what script name would we use?
parser = argparse.ArgumentParser(description="Control saxo irc bot instances",
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="redirect daemon stdout and stderr to this filename")
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:])
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)
else:
generic.error("unrecognised action: %s" % args.action, code=2)
else:
usage(args, v)