<commit_before>#!/usr/bin/env python
# Start various clients, server or tests

import sys
import getopt
from subprocess import Popen
import os
from time import sleep

import lib.lib as lib
import client.desktop_client as desktop_client_mod
import client.cli_client as cli_client_mod
import client.sub_client as sub_client_mod
import planner.fsm_planner as pfsm_mod


def main(argv):
    """Get arguments and handle them."""
    try:
        opts, args = getopt.getopt(argv, "hsdtp8xcTfu",
                     ["help", "server", "desktop", "tests", "fsm-planner",
                      "pep8", "exit-server", "cli", "test-mode", "pub-server",
                      "sub"])
    except getopt.GetoptError:
        print_help()
        sys.exit(2)

    opt_list = list(zip(*opts)[0])

    if len(opt_list) == 0 or "-h" in opt_list or "--help" in opt_list:
        print_help()
        sys.exit(0)

    if "-T" in opt_list or "--test-mode" in opt_list:
        test_mode = "True"
    else:
        test_mode = "False"
    lib.set_testing(bool(test_mode))

    if "-8" in opt_list or "--pep8" in opt_list:
        os.system("./scripts/check_pep8.sh")

    if "-t" in opt_list or "--tests" in opt_list:
        print "Running unit tests"
        os.system("python -m unittest discover")

    if "-f" in opt_list or "--planner" in opt_list:
        print "Starting planner"
        planner = pfsm_mod.Robot()

    if "-s" in opt_list or "--server" in opt_list:
        print "Starting server"
        server = Popen(["./server/server.py", test_mode])
        # Give server a chance to get up and running
        sleep(.2)

    if "-p" in opt_list or "--pub-server" in opt_list:
        print "Starting PubServer"
        pub_server = Popen(["./server/pub_server.py", test_mode])
        # Give server a chance to get up and running
        sleep(.2)

    if "-d" in opt_list or "--desktop" in opt_list:
        print "Starting desktop client."
        desktop_client_mod.DesktopClient().run()

    if "-c" in opt_list or "--cli" in opt_list:
        print "Starting CLI client."
        cli_client_mod.CLIClient().cmdloop()

    if "-u" in opt_list or "--sub" in opt_list:
        print "Starting subscriber client"
        sub_client_mod.SubClient().print_msgs()

    if "-x" in opt_list or "--exit-server" in opt_list:
        try:
            server.kill()
            print "Closed server"
        except NameError:
            print "I don't own the server. Use pkill/pgrep or similar."
    elif "-s" in opt_list or "--server" in opt_list:
        print "Server is still running, give -x to close next time."


def print_help():
    """Print usage of script."""
    print "start.py [-h|--help] [-s|--server] [-d|--desktop] [-t|--tests]\n" +\
          "         [-p|--planner] [-8|--pep8] [-x|--exit-server]"

if __name__ == "__main__":
    main(sys.argv[1:])<commit_msg>Updated start.py to use argparse, issue #49<commit_after>#!/usr/bin/env python
# Start various clients, server or tests

import sys
import argparse
from subprocess import Popen
import os
from time import sleep

import lib.lib as lib
import client.desktop_client as desktop_client_mod
import client.cli_client as cli_client_mod
import client.sub_client as sub_client_mod
import planner.fsm_planner as pfsm_mod

# Build parser and argument groups
parser = argparse.ArgumentParser(description="start bot clients, servers and tests")
root_group = parser.add_mutually_exclusive_group()
client_group = root_group.add_mutually_exclusive_group()
test_group = root_group.add_mutually_exclusive_group()

# Add arguments
client_group.add_argument("-a", "--auto-client", action="store_true",
                          help="autonomously solve the course")
client_group.add_argument("-c", "--cli-client", action="store_true",
                          help="CLI interface for controlling the bot")
client_group.add_argument("-d", "--desktop-client", action="store_true",
                          help="desktop GUI interface for controlling the bot")
client_group.add_argument("-u", "--sub-client", action="store_true",
                          help="subscribe to bot's published information")
test_group.add_argument("-t", "--tests", action="store_true",
                        help="run all unit tests")
test_group.add_argument("-8", "--pep8", action="store_true",
                        help="run script to check PEP8 style conformance")
parser.add_argument("-T", "--test-mode", action="store_true",
                    help="use simulated hardware, to allow running off the bone")
parser.add_argument("-s", "--server", action="store_true",
                    help="start server to provide interface for controlling the bot")
parser.add_argument("-p", "--pub-server", action="store_true",
                    help="start server to publish bot's information")

# Parse the given args
args = parser.parse_args()

# Run on simulated hardware or not
lib.set_testing(args.test_mode)

if args.test_mode:
    print "Using simulated hardware"
else:
    print "Using real hardware. Hopefully you're on a bone."

if args.pep8:
    print "Running PEP8 style checks"
    os.system("./scripts/check_pep8.sh")

if args.tests:
    print "Running unit tests"
    os.system("python -m unittest discover")

if args.auto_client:
    print "Starting autonomous client"
    planner = pfsm_mod.Robot()

if args.server:
    print "Starting server"
    server = Popen(["./server/server.py", str(args.test_mode)])
    # Give server a chance to get up and running
    sleep(.2)

if args.pub_server:
    print "Starting PubServer"
    pub_server = Popen(["./server/pub_server.py", str(args.test_mode)])
    # Give server a chance to get up and running
    sleep(.2)

if args.cli_client:
    print "Starting CLI client"
    cli_client_mod.CLIClient().cmdloop()

if args.desktop_client:
    print "Starting desktop client"
    desktop_client_mod.DesktopClient().run()

if args.sub_client:
    print "Starting subscriber client"
    sub_client_mod.SubClient().print_msgs()
{'fraction_non_alphanumeric': 0.12156536219816819, 'fraction_numerical': 0.003996669442131557, 'mean_word_length': 2.8549422336328627, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7067939', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1827, 'n_words': 549}