aboutsummaryrefslogtreecommitdiff
path: root/mcwrapper/__init__.py
blob: 2ae3c2a5f508cf083cebabb1395898a4302fe303 (plain)
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
# vim: expandtab ft=python ts=4 sw=4 sts=4:
import os
import sys
import subprocess
import signal
import time
import atexit
import argparse
from threading import Thread

from . import prints
from . import wrapper
from .wrapper import MCWrapper

mcserver_wrapper = None


def __wrapper_atexit__():
    "This is called when wrapper is exiting"
    mcserver_wrapper.clean()


def __wrapper_toexit__():
    "This function is called when system signalizes that mcwrapper should exit"
    mcserver_wrapper.stop()


def __signal_term__(_signo, _stack_frame):
    __wrapper_toexit__()


__HELP_DESC__ = """
    This script is executing Minecraft server and reads its output. From output
    is extracted server status and list of online players. And standard input
    can be accessed by fifo file.
    """


def main():
    "Main function"
    global verbose_level
    parser = argparse.ArgumentParser(description=__HELP_DESC__)
    parser.add_argument('--verbose', '-v', action='count', default=0,
                        help="Increase verbose level of output")
    parser.add_argument('--quiet', '-q', action='count', default=0,
                        help="Decrease verbose level of output")
    parser.add_argument('--status-file', '-s', action='store_true',
                        help="Outputs server status to file \"status\"")
    parser.add_argument('--players-file', '-p', action='store_true',
                        help="""Outputs list of online players to file
                        \"players\" """)
    parser.add_argument('command', nargs=argparse.REMAINDER,
                        help="""Command to be executed to start Minecraft
                        server.""")
    args = parser.parse_args()

    prints.verbose_level = args.verbose - args.quiet
    command = args.command
    sfile = args.status_file
    pfile = args.players_file

    if not command:
        parser.print_help()
        return
    if 'nogui' not in command:
        command.append('nogui')

    global mcserver_wrapper
    mcserver_wrapper = MCWrapper(command, pfile, sfile)
    signal.signal(signal.SIGTERM, __signal_term__)
    signal.signal(signal.SIGINT, __signal_term__)
    atexit.register(__wrapper_atexit__)

    mcserver_wrapper.execstart()

if __name__ == '__main__':
    main()