This file is indexed.

/usr/lib/python2.7/dist-packages/trytond/server.py is in tryton-server 3.0.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
"""
%prog [options]
"""
import logging
import logging.handlers
import sys
import os
import signal
import time
from getpass import getpass
import threading

from trytond.config import CONFIG
from trytond import backend
from trytond.pool import Pool
from trytond.monitor import monitor
from .transaction import Transaction


class TrytonServer(object):

    def __init__(self, options):
        format = '[%(asctime)s] %(levelname)s:%(name)s:%(message)s'
        datefmt = '%a %b %d %H:%M:%S %Y'
        logging.basicConfig(level=logging.INFO, format=format,
                datefmt=datefmt)

        CONFIG.update_etc(options['configfile'])
        CONFIG.update_cmdline(options)
        CONFIG.set_timezone()

        if CONFIG['logfile']:
            logf = CONFIG['logfile']
            # test if the directories exist, else create them
            try:
                diff = 0
                if os.path.isfile(logf):
                    diff = int(time.time()) - int(os.stat(logf)[-1])
                handler = logging.handlers.TimedRotatingFileHandler(
                    logf, 'D', 1, 30)
                handler.rolloverAt -= diff
            except Exception, exception:
                sys.stderr.write(
                    "ERROR: couldn't create the logfile directory:"
                    + str(exception))
            else:
                formatter = logging.Formatter(format, datefmt)
                # tell the handler to use this format
                handler.setFormatter(formatter)

                # add the handler to the root logger
                logging.getLogger().addHandler(handler)
                logging.getLogger().setLevel(logging.INFO)
        elif os.name != 'nt':
            reverse = '\x1b[7m'
            reset = '\x1b[0m'
            # reverse color for error and critical messages
            for level in logging.ERROR, logging.CRITICAL:
                msg = reverse + logging.getLevelName(level) + reset
                logging.addLevelName(level, msg)

        self.logger = logging.getLogger("server")

        if CONFIG.configfile:
            self.logger.info('using %s as configuration file'
                % CONFIG.configfile)
        else:
            self.logger.info('using default configuration')
        self.logger.info('initialising distributed objects services')
        self.xmlrpcd = []
        self.jsonrpcd = []
        self.webdavd = []

    def run(self):
        "Run the server and never return"
        update = bool(CONFIG['init'] or CONFIG['update'])
        init = {}

        signal.signal(signal.SIGINT, lambda *a: self.stop())
        signal.signal(signal.SIGTERM, lambda *a: self.stop())
        if hasattr(signal, 'SIGQUIT'):
            signal.signal(signal.SIGQUIT, lambda *a: self.stop())
        if hasattr(signal, 'SIGUSR1'):
            signal.signal(signal.SIGUSR1, lambda *a: self.restart())

        if CONFIG['pidfile']:
            with open(CONFIG['pidfile'], 'w') as fd_pid:
                fd_pid.write("%d" % (os.getpid()))

        if not CONFIG["db_name"] \
                and bool(CONFIG['init'] or CONFIG['update']):
            raise Exception('Missing database option!')

        if not update:
            self.start_servers()

        for db_name in CONFIG["db_name"]:
            init[db_name] = False
            with Transaction().start(db_name, 0) as transaction:
                cursor = transaction.cursor
                if CONFIG['init']:
                    if not cursor.test():
                        self.logger.info("init db")
                        backend.get('Database').init(cursor)
                        init[db_name] = True
                    cursor.commit()
                elif not cursor.test():
                    raise Exception("'%s' is not a Tryton database!" % db_name)

        for db_name in CONFIG["db_name"]:
            if update:
                with Transaction().start(db_name, 0) as transaction:
                    cursor = transaction.cursor
                    if not cursor.test():
                        raise Exception("'%s' is not a Tryton database!"
                            % db_name)
                    cursor.execute('SELECT code FROM ir_lang '
                        'WHERE translatable')
                    lang = [x[0] for x in cursor.fetchall()]
            else:
                lang = None
            Pool(db_name).init(update=update, lang=lang)

        for kind in ('init', 'update'):
            CONFIG[kind] = {}

        for db_name in CONFIG['db_name']:
            if init[db_name]:
                # try to read password from environment variable
                # TRYTONPASSFILE, empty TRYTONPASSFILE ignored
                passpath = os.getenv('TRYTONPASSFILE')
                password = ''
                if passpath:
                    try:
                        with open(passpath) as passfile:
                            password = passfile.readline()[:-1]
                    except Exception, err:
                        sys.stderr.write('Can not read password '
                            'from "%s": "%s"\n' % (passpath, err))

                if not password:
                    while True:
                        password = getpass('Admin Password for %s: ' % db_name)
                        password2 = getpass('Admin Password Confirmation: ')
                        if password != password2:
                            sys.stderr.write('Admin Password Confirmation '
                                'doesn\'t match Admin Password!\n')
                            continue
                        if not password:
                            sys.stderr.write('Admin Password is required!\n')
                            continue
                        break

                with Transaction().start(db_name, 0) as transaction:
                    pool = Pool()
                    User = pool.get('res.user')
                    admin, = User.search([('login', '=', 'admin')])
                    User.write([admin], {
                            'password': password,
                            })
                    transaction.cursor.commit()

        if update:
            self.logger.info('Update/Init succeed!')
            logging.shutdown()
            sys.exit(0)

        threads = {}
        while True:
            if CONFIG['cron']:
                for dbname in Pool.database_list():
                    thread = threads.get(dbname)
                    if thread and thread.is_alive():
                        continue
                    pool = Pool(dbname)
                    if not pool.lock.acquire(0):
                        continue
                    try:
                        if 'ir.cron' not in pool.object_name_list():
                            continue
                        Cron = pool.get('ir.cron')
                    finally:
                        pool.lock.release()
                    thread = threading.Thread(
                            target=Cron.run,
                            args=(dbname,), kwargs={})
                    thread.start()
                    threads[dbname] = thread
            if CONFIG['auto_reload']:
                for _ in range(60):
                    if monitor():
                        self.restart()
                    time.sleep(1)
            else:
                time.sleep(60)

    def start_servers(self):
        # Launch Server
        if CONFIG['jsonrpc']:
            from trytond.protocols.jsonrpc import JSONRPCDaemon
            for hostname, port in CONFIG['jsonrpc']:
                self.jsonrpcd.append(JSONRPCDaemon(hostname, port,
                    CONFIG['ssl_jsonrpc']))
                self.logger.info("starting JSON-RPC%s protocol on %s:%d" %
                    (CONFIG['ssl_jsonrpc'] and ' SSL' or '', hostname or '*',
                        port))

        if CONFIG['xmlrpc']:
            from trytond.protocols.xmlrpc import XMLRPCDaemon
            for hostname, port in CONFIG['xmlrpc']:
                self.xmlrpcd.append(XMLRPCDaemon(hostname, port,
                    CONFIG['ssl_xmlrpc']))
                self.logger.info("starting XML-RPC%s protocol on %s:%d" %
                    (CONFIG['ssl_xmlrpc'] and ' SSL' or '', hostname or '*',
                        port))

        if CONFIG['webdav']:
            from trytond.protocols.webdav import WebDAVServerThread
            for hostname, port in CONFIG['webdav']:
                self.webdavd.append(WebDAVServerThread(hostname, port,
                    CONFIG['ssl_webdav']))
                self.logger.info("starting WebDAV%s protocol on %s:%d" %
                    (CONFIG['ssl_webdav'] and ' SSL' or '', hostname or '*',
                        port))

        for servers in (self.xmlrpcd, self.jsonrpcd, self.webdavd):
            for server in servers:
                server.start()

    def stop(self, exit=True):
        for servers in (self.xmlrpcd, self.jsonrpcd, self.webdavd):
            for server in servers:
                server.stop()
                server.join()
        if exit:
            if CONFIG['pidfile']:
                os.unlink(CONFIG['pidfile'])
            logging.getLogger('server').info('stopped')
            logging.shutdown()
            sys.exit(0)

    def restart(self):
        self.stop(False)
        args = ([sys.executable] + ['-W%s' % o for o in sys.warnoptions]
            + sys.argv)
        if sys.platform == "win32":
            args = ['"%s"' % arg for arg in args]
        os.execv(sys.executable, args)