File: main.py

package info (click to toggle)
python-dcos 0.2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,440 kB
  • sloc: python: 8,196; sh: 194; makefile: 36
file content (115 lines) | stat: -rw-r--r-- 2,860 bytes parent folder | download | duplicates (4)
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
"""Display command line usage information

Usage:
    dcos help
    dcos help --info
    dcos help <command>

Options:
    --help     Show this screen
    --info     Show a short description of this subcommand
    --version  Show version
"""
import subprocess

import dcoscli
import docopt
from concurrent.futures import ThreadPoolExecutor
from dcos import cmds, emitting, options, subcommand, util
from dcos.errors import DCOSException
from dcoscli.main import decorate_docopt_usage

emitter = emitting.FlatEmitter()
logger = util.get_logger(__name__)


def main():
    try:
        return _main()
    except DCOSException as e:
        emitter.publish(e)
        return 1


@decorate_docopt_usage
def _main():
    util.configure_process_from_environ()

    args = docopt.docopt(
        __doc__,
        version='dcos-help version {}'.format(dcoscli.version))

    return cmds.execute(_cmds(), args)


def _cmds():
    """
    :returns: All of the supported commands
    :rtype: list of dcos.cmds.Command
    """

    return [
        cmds.Command(
            hierarchy=['help', '--info'],
            arg_keys=[],
            function=_info),

        cmds.Command(
            hierarchy=['help'],
            arg_keys=['<command>'],
            function=_help),
    ]


def _info():
    """
    :returns: process return code
    :rtype: int
    """

    emitter.publish(__doc__.split('\n')[0])
    return 0


def _help(command):
    """
    :param command: the command name for which you want to see a help
    :type command: str
    :returns: process return code
    :rtype: int
    """

    if command is not None:
        _help_command(command)
    else:
        logger.debug("DCOS bin path: {!r}".format(util.dcos_bin_path()))

        paths = subcommand.list_paths()
        with ThreadPoolExecutor(max_workers=len(paths)) as executor:
            results = executor.map(subcommand.documentation, paths)
            commands_message = options\
                .make_command_summary_string(sorted(results))

        emitter.publish(
            "Command line utility for the Mesosphere Datacenter Operating\n"
            "System (DCOS). The Mesosphere DCOS is a distributed operating\n"
            "system built around Apache Mesos. This utility provides tools\n"
            "for easy management of a DCOS installation.\n")
        emitter.publish("Available DCOS commands:")
        emitter.publish(commands_message)
        emitter.publish(
            "\nGet detailed command description with 'dcos <command> --help'.")

        return 0


def _help_command(command):
    """
    :param command: the command name for which you want to see a help
    :type command: str
    :returns: process return code
    :rtype: int
    """

    executable = subcommand.command_executables(command)
    return subprocess.call([executable, command, '--help'])