Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runner.py « jcl « src - github.com/dax/jcl.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 10007770a96c1f171ba024a8e6f256a32dadf0f7 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
##
## runner.py
## Login : David Rousselie <dax@happycoders.org>
## Started on  Thu May 17 22:02:08 2007 David Rousselie
## $Id$
##
## Copyright (C) 2007 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##

import logging
import logging.handlers

import os
import sys
from ConfigParser import ConfigParser
from getopt import gnu_getopt
import threading

from jcl.lang import Lang
from jcl.jabber.component import JCLComponent
import jcl.model as model
from jcl.model.account import Account, PresenceAccount, User, LegacyJID

LOG_FORMATTER = logging.Formatter(fmt="[%(levelname)s] %(asctime)s (%(pathname)s:%(lineno)d): %(message)s")

class JCLRunner(object):

    def __init__(self, component_name, component_version):
        """
        options: list of tuples:
        - short_opt: same as getopt
        - long_opt: same as getopt
        """
        self.component_short_name = "JCL"
        self.component_name = component_name
        self.component_version = component_version
        self.config_file = "jcl.conf"
        self.server = "localhost"
        self.port = 5347
        self.secret = "secret"
        self.service_jid = "jcl.localhost"
        self.language = "en"
        self.db_url = "sqlite:///var/spool/jabber/jcl.db"
        self.pid_file = "/var/run/jabber/jcl.pid"
        self.log_stdout = False
        self.log_file = None
        self.console = False
        self.options = [("c:", "config-file=", None,
                         " FILE\t\t\t\tConfiguration file to use",
                         lambda arg: self.set_attr("config_file", arg)),
                        ("S:", "server=", "jabber",
                         " SERVER_ADDRESS\t\t\tAddress of the Jabber server",
                         lambda arg: self.set_attr("server", arg)),
                        ("P:", "port=", "jabber",
                         " PORT\t\t\t\t\tPort of the Jabber server to connect the component",
                         lambda arg: self.set_attr("port", int(arg))),
                        ("s:", "secret=", "jabber",
                         " SECRET\t\t\t\tComponent password to connect to the Jabber server",
                         lambda arg: self.set_attr("secret", arg)),
                        ("j:", "service-jid=", "jabber",
                         " JID\t\t\t\tJID of the component",
                         lambda arg: self.set_attr("service_jid", arg)),
                        ("l:", "language=", "jabber",
                         " LANG\t\t\t\tDefault Language of the component",
                         lambda arg: self.set_attr("language", arg)),
                        ("u:", "db-url=", "db",
                         " URL\t\t\t\tDatabase URL",
                         lambda arg: self.set_attr("db_url", arg)),
                        ("p:", "pid-file=", "component",
                         " FILE\t\t\t\tPath of the PID file",
                         lambda arg: self.set_attr("pid_file", arg)),
                        ("d", "debug", None,
                         "\t\t\t\t\tEnable debug traces",
                         lambda arg: self.set_attr("debug", True)),
                        ("C", "console", None,
                         "\t\t\t\t\tRun in console mode",
                         lambda arg: self.set_attr("console", True)),
                        ("o", "log-stdout", None,
                         "\t\t\t\t\tLog on stdout",
                         lambda arg: self.set_attr("log_stdout", True)),
                        ("f:", "log-file=", "component",
                         "\t\t\t\t\tLog in file",
                         lambda arg: self.set_attr("log_file", arg)),
                        ("h", "help", None,
                         "\t\t\t\t\tThis help",
                         lambda arg: self.print_help())]
        self.logger = logging.getLogger()
        self.__debug = False
        self.wait_event = threading.Event()

    def set_attr(self, attr, value):
        setattr(self, attr, value)

    def __configure_commandline_args(self, shortopts, longopts, cleanopts):
        (opts, args) = gnu_getopt(sys.argv[1:], shortopts, longopts)
        commandline_args = {}
        for (arg, value) in opts:
            clean_arg = arg.lstrip('-')
            if cleanopts.has_key(clean_arg):
                commandline_args[clean_arg] = value
        return commandline_args

    def __apply_commandline_args(self, commandline_args, cleanopts):
        for arg in commandline_args:
            value = commandline_args[arg]
            self.logger.debug("Applying argument " + arg + " = " +
                              value)
            cleanopts[arg][1](value)

    def __apply_configfile(self, commandline_args, cleanopts):
        if commandline_args.has_key("config-file"):
            self.config_file = commandline_args["config-file"]
        elif commandline_args.has_key("c"):
            self.config_file = commandline_args["c"]
        self.config = ConfigParser()
        self.logger.debug("Loading config file " + self.config_file)
        read_file = self.config.read(self.config_file)
        if read_file == []:
            self.logger.info("Creating empty config file " + self.config_file)
        else:
            for opt in cleanopts:
                if len(opt) > 1:
                    (section, set_func) = cleanopts[opt]
                    if section is not None:
                        attr = opt.replace("-", "_")
                        if self.config.has_section(section) \
                                and self.config.has_option(section, attr):
                            config_property = self.config.get(section, attr)
                            self.logger.debug("Setting " + attr + " = " +
                                              config_property +
                                              " from configuration file " +
                                              self.config_file)
                            set_func(config_property)

    def configure(self):
        """
        Apply configuration from command line and configuration file.
        command line arguments override configuration file properties.
        """
        shortopts = ""
        longopts = []
        cleanopts = {}
        for option in self.options:
            shortopts += option[0]
            longopts += [option[1]]
            cleanopts[option[0][0]] = (option[2], option[4])
            if option[1][-1:] == '=':
                cleanopts[option[1][:-1]] = (option[2], option[4])
            else:
                cleanopts[option[1]] = (option[2], option[4])

        commandline_args = self.__configure_commandline_args(shortopts,
                                                             longopts,
                                                             cleanopts)
        if commandline_args.has_key("debug") or commandline_args.has_key("d"):
            self.debug = True
            self.logger.debug("Debug activated")
        else:
            self.debug = False
        self.__apply_configfile(commandline_args, cleanopts)
        self.__apply_commandline_args(commandline_args, cleanopts)
        if self.log_stdout:
            handler = logging.StreamHandler()
            handler.setFormatter(LOG_FORMATTER)
            logging.Logger.root.addHandler(handler)
        if self.log_file is not None:
            handler = logging.handlers.WatchedFileHandler(self.log_file)
            handler.setFormatter(LOG_FORMATTER)
            logging.Logger.root.addHandler(handler)

    def _get_help(self):
        help = self.component_name + " v" + self.component_version + " help:\n"
        for option in self.options:
            if option[1][-1:] == '=':
                long_option = option[1][:-1]
            else:
                long_option = option[1]
            help += "\t-" + option[0][0] + ", --" + long_option + option[3] + "\n"
        return help

    def print_help(self):
        print self._get_help()
        sys.exit(0)

    def get_debug(self):
        return self.__debug

    def set_debug(self, value):
        self.__debug = value
        if self.__debug:
            self.logger.setLevel(logging.DEBUG)
        else:
            self.logger.setLevel(logging.CRITICAL)

    debug = property(get_debug, set_debug)

    def setup_db(self):
        Account.createTable(ifNotExists=True)
        PresenceAccount.createTable(ifNotExists=True)
        User.createTable(ifNotExists=True)
        LegacyJID.createTable(ifNotExists=True)

    def setup_pidfile(self):
        pidfile = open(self.pid_file, "w")
        pidfile.write(str(os.getpid()))
        pidfile.close()

    def _run(self, run_func):
        if self.console:
            self.run_console()
        else:
            try:
                self.setup_pidfile()
                model.db_connection_str = self.db_url
                model.db_connect()
                self.setup_db()
                model.db_disconnect()
                self.logger.debug(self.component_name + " v" +
                                  self.component_version + " is starting ...")
                restart = True
                while restart:
                    (restart, time_to_wait) = run_func()
                    self.wait_event.wait(time_to_wait)
                self.logger.debug(self.component_name + " is exiting")
            finally:
                if os.path.exists(self.pid_file):
                    os.remove(self.pid_file)

    def run(self):
        def run_func():
            component = JCLComponent(jid=self.service_jid,
                                     secret=self.secret,
                                     server=self.server,
                                     port=self.port,
                                     lang=Lang(self.language),
                                     config=self.config,
                                     config_file=self.config_file)
            component.version = self.component_version
            return component.run()
        self._run(run_func)

    def run_console(self):
        """
        Run JCL console. Used to access SQLObject connection
        """
        from IPython.Shell import IPShellEmbed
        # pre-import jcl.model.account to be used in the shell
        import jcl.model.account as account
        model.db_connection_str = self.db_url
        model.db_connect()
        self.setup_db()
        ipshell = IPShellEmbed(["-pi1", self.component_short_name + "[\\#]: "],
                               "Starting " + self.component_name + " v" \
                                   + self.component_version + " console.",
                               "Ending " + self.component_name + " v" \
                                   + self.component_version + " console.")
        ipshell()

def main():
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    del sys.setdefaultencoding
    import jcl
    from jcl.runner import JCLRunner
    runner = JCLRunner("JCL", jcl.version)
    runner.configure()
    runner.run()

if __name__ == '__main__':
    main()