forked from buntine/laser-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaserdriftd
executable file
·66 lines (55 loc) · 2.47 KB
/
laserdriftd
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
#! /usr/bin/env python
import logging
import sys
import daemon
from laserdrift import LaserDrift
from optparse import OptionParser
parser = OptionParser(description="Starts a Laser Drift racing server with the given players.")
parser.add_option("-d", "--daemon", action="store_true", dest="daemon", default=False,
help="Daemonise the laserdriftd process")
parser.add_option("-s", "--socket", dest="socket", default="/var/run/lirc/lircd",
help="Location of the lircd socket [default: %default]", metavar="PATH")
parser.add_option("-H", "--host", dest="host", default="localhost",
help="Host name to run TCP server on [default: %default]" , metavar="HOST")
parser.add_option("-p", "--port", dest="port", default=8099,
help="TCP port to listen on [default: %default]", metavar="PORT")
parser.add_option("-r", "--remote", dest="remote", default="carrera",
help="The name of the remote control in the lirc config [default: %default]",
metavar="NAME")
parser.add_option("-l", "--logfile", dest="logfile", default=None,
help="The path to a file to log output to [default: STDOUT]",
metavar="PATH")
parser.add_option("--p0", action="append_const", const=0, dest="players",
help="Player #1 is active")
parser.add_option("--p1", action="append_const", const=1, dest="players",
help="Player #2 is active")
parser.add_option("--p2", action="append_const", const=2, dest="players",
help="Player #3 is active")
parser.add_option("--p3", action="append_const", const=3, dest="players",
help="Player #4 is active")
(opts, _) = parser.parse_args()
logging.basicConfig(filename=opts.logfile, level=logging.INFO)
if not opts.players:
logging.error("Atleast one player must be active.")
sys.exit(1)
if opts.daemon and not opts.logfile:
logging.error("Log file must be specified when running as a daemon.")
sys.exit(1)
opts.players.sort()
ld = LaserDrift(opts.logfile)
logfile = logging.root.handlers[0].stream.fileno()
process = daemon.DaemonContext(files_preserve=[logfile])
try:
if opts.daemon:
process.open()
ld.run(players=opts.players,
host=opts.host,
port=int(opts.port),
daemon=opts.daemon,
socket=opts.socket,
remote=opts.remote)
except KeyboardInterrupt:
ld.terminate()
finally:
if opts.daemon:
process.close()