summaryrefslogtreecommitdiff
path: root/start_mx.py
blob: fe0b23b99980a2aa7245625ea2f6d29185ffeadb (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
                ____
               | MX |_________________________
            ___|____| An encrypting remailer  |________
           |       |__________________________|        |
           | is designed for use on a mail exchange    |
           | with OpenPGP implementations and Postfix, |
           | and is part of the Leap Encryption Access |
           | Project platform.                         |
           |___________________________________________|
"""
    # authors:   Isis Agora Lovecruft, <isis@leap.se> 0x2cdb8b35
    # license:   AGPLv3, see included LICENCE file.
    # copyright: copyright (c) 2013 Isis Agora Lovecruft


from __future__ import print_function
from os         import getcwd
from os         import path as ospath

import sys


application_name = "leap_mx"

def __get_dirs__():
    """Get the absolute path of the top-level repository directory."""
    here = getcwd()
    base = here.rsplit(application_name, 1)[0]
    repo = ospath.join(base, application_name)
    leap = ospath.join(repo, 'src')
    ours = ospath.join(leap, application_name.replace('_', '/'))
    return repo, leap, ours

## py3k check, snagged from python-gnupg-0.3.2 by Vinay Sajip
try:
    unicode
    _py3k = False
except NameError:
    _py3k = True

## Set the $PYTHONPATH:
repo, leap, ours = __get_dirs__()
sys.path[:] = map(ospath.abspath, sys.path)
sys.path.insert(0, leap)

## Now we should be able to import ourselves without installation:
try:
    from leap.mx      import runner
    from leap.mx.util import config, log, version
except ImportError, ie:
    print("%s \nExiting... \n" % ie.message)
    sys.exit(1)

try:
    from twisted.python      import usage, runtime, failure
    from twisted.python.util import spewer
except ImportError, ie:
    print("This software requires Twisted>=12.0.2, please see the README for")
    print("help on using virtualenv and pip to obtain requirements.")


class MXOptions(usage.Options):
    """
    Command line options for leap_mx.
    """
    #longdesc = str("  ")
    optParameters = [
        ['config', 'c', 'mx.conf', 'Config file to use'],]
    optFlags = [
        ['all-tests', 'a', 'Run all unittests'],
        ['verbose', 'v', 'Increase logging verbosity'],]

    def opt_version(self):
        """
        Print leap_mx version and exit
        """
        print("Version: %s" % version.getVersion())
        sys.exit(0)

    def opt_help(self):
        """
        Print this cruft
        """
        import __main__
        print("%s" % __main__.__doc__)
        self.getUsage()

    def opt_spewer(self):
        """
        Print *all of the things*. Useful for debugging.
        """
        sys.settrace(spewer)

    def parseArgs(self):
        """
        Called with the remaining commandline options which were unrecognised.
        """
        log.warn("Couldn't recognise option: %s" % self)


if __name__ == "__main__":
    dependency_check = runner.CheckRequirements(version.getPackageName(),
                                                version.getPipfile())
    mx_options = MXOptions()
    if len(sys.argv) <= 1:
        mx_options.opt_help()
        sys.exit(0)

    try:
        mx_options.parseOptions()
    except UsageError, ue:
        print("%s" % ue.message)
        sys.exit(1)

    options = mx_options.opts

    ## Get the config settings:
    config.filename = options['config']
    config.loadConfig()

    if config.basic.enable_logfile:
        ## Log to file:
        logfilename = config.basic.logfile
        logfilepath = ospath.join(repo, 'logs')
        log.start(logfilename, logfilepath)
    else:
        ## Otherwise just log to stdout:
        log.start()

    log.msg("Testing logging functionality")
    if runtime.platform.supportsThreads():
        thread_support = "with thread support."
    else:
        thread_support = "without thread support."
    log.debug("Running %s, with Python %s on %s platform %s"
              % (application_name, runtime.shortPythonVersion(),
                 runtime.platform.getType(), thread_support))

    if options['verbose']:
        config.basic.debug = True
        failure.traceupLength = 7
        failure.startDebugMode()

    if options['test']:
        from leap.mx import tests ## xxx this needs an __init__.py
        tests.run()
    else:
        mx_options.getUsage()
        sys.exit(1)