aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py2/twisted/application/runner/_exit.py
blob: ffccc417bd4e393a290a1f2bf7a462c1255bc68c (plain) (blame)
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
# -*- test-case-name: twisted.application.runner.test.test_exit -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
System exit support.
"""

from sys import stdout, stderr, exit as sysexit

from constantly import Values, ValueConstant



def exit(status, message=None):
    """
    Exit the python interpreter with the given status and an optional message.

    @param status: An exit status.
    @type status: L{int} or L{ValueConstant} from L{ExitStatus}.

    @param message: An options message to print.
    @type status: L{str}
    """
    if isinstance(status, ValueConstant):
        code = status.value
    else:
        code = int(status)

    if message:
        if code == 0:
            out = stdout
        else:
            out = stderr
        out.write(message)
        out.write("\n")

    sysexit(code)



try:
    import posix as Status
except ImportError:
    class Status(object):
        """
        Object to hang C{EX_*} values off of as a substitute for L{posix}.
        """
        EX__BASE = 64

        EX_OK          = 0
        EX_USAGE       = EX__BASE
        EX_DATAERR     = EX__BASE + 1
        EX_NOINPUT     = EX__BASE + 2
        EX_NOUSER      = EX__BASE + 3
        EX_NOHOST      = EX__BASE + 4
        EX_UNAVAILABLE = EX__BASE + 5
        EX_SOFTWARE    = EX__BASE + 6
        EX_OSERR       = EX__BASE + 7
        EX_OSFILE      = EX__BASE + 8
        EX_CANTCREAT   = EX__BASE + 9
        EX_IOERR       = EX__BASE + 10
        EX_TEMPFAIL    = EX__BASE + 11
        EX_PROTOCOL    = EX__BASE + 12
        EX_NOPERM      = EX__BASE + 13
        EX_CONFIG      = EX__BASE + 14



class ExitStatus(Values):
    """
    Standard exit status codes for system programs.

    @cvar EX_OK: Successful termination.
    @type EX_OK: L{ValueConstant}

    @cvar EX_USAGE: Command line usage error.
    @type EX_USAGE: L{ValueConstant}

    @cvar EX_DATAERR: Data format error.
    @type EX_DATAERR: L{ValueConstant}

    @cvar EX_NOINPUT: Cannot open input.
    @type EX_NOINPUT: L{ValueConstant}

    @cvar EX_NOUSER: Addressee unknown.
    @type EX_NOUSER: L{ValueConstant}

    @cvar EX_NOHOST: Host name unknown.
    @type EX_NOHOST: L{ValueConstant}

    @cvar EX_UNAVAILABLE: Service unavailable.
    @type EX_UNAVAILABLE: L{ValueConstant}

    @cvar EX_SOFTWARE: Internal software error.
    @type EX_SOFTWARE: L{ValueConstant}

    @cvar EX_OSERR: System error (e.g., can't fork).
    @type EX_OSERR: L{ValueConstant}

    @cvar EX_OSFILE: Critical OS file missing.
    @type EX_OSFILE: L{ValueConstant}

    @cvar EX_CANTCREAT: Can't create (user) output file.
    @type EX_CANTCREAT: L{ValueConstant}

    @cvar EX_IOERR: Input/output error.
    @type EX_IOERR: L{ValueConstant}

    @cvar EX_TEMPFAIL: Temporary failure; the user is invited to retry.
    @type EX_TEMPFAIL: L{ValueConstant}

    @cvar EX_PROTOCOL: Remote error in protocol.
    @type EX_PROTOCOL: L{ValueConstant}

    @cvar EX_NOPERM: Permission denied.
    @type EX_NOPERM: L{ValueConstant}

    @cvar EX_CONFIG: Configuration error.
    @type EX_CONFIG: L{ValueConstant}
    """

    EX_OK          = ValueConstant(Status.EX_OK)
    EX_USAGE       = ValueConstant(Status.EX_USAGE)
    EX_DATAERR     = ValueConstant(Status.EX_DATAERR)
    EX_NOINPUT     = ValueConstant(Status.EX_NOINPUT)
    EX_NOUSER      = ValueConstant(Status.EX_NOUSER)
    EX_NOHOST      = ValueConstant(Status.EX_NOHOST)
    EX_UNAVAILABLE = ValueConstant(Status.EX_UNAVAILABLE)
    EX_SOFTWARE    = ValueConstant(Status.EX_SOFTWARE)
    EX_OSERR       = ValueConstant(Status.EX_OSERR)
    EX_OSFILE      = ValueConstant(Status.EX_OSFILE)
    EX_CANTCREAT   = ValueConstant(Status.EX_CANTCREAT)
    EX_IOERR       = ValueConstant(Status.EX_IOERR)
    EX_TEMPFAIL    = ValueConstant(Status.EX_TEMPFAIL)
    EX_PROTOCOL    = ValueConstant(Status.EX_PROTOCOL)
    EX_NOPERM      = ValueConstant(Status.EX_NOPERM)
    EX_CONFIG      = ValueConstant(Status.EX_CONFIG)