/usr/lib/python2.7/dist-packages/trytond/protocols/dispatcher.py is in tryton-server 3.4.0-3+deb8u3.
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 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | # -*- coding: utf-8 -*-
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import logging
import time
import sys
import pydoc
from sql import Table
from trytond.pool import Pool
from trytond import security
from trytond import backend
from trytond.config import config
from trytond.version import VERSION
from trytond.transaction import Transaction
from trytond.cache import Cache
from trytond.exceptions import UserError, UserWarning, NotLogged, \
ConcurrencyException
from trytond.rpc import RPC
logger = logging.getLogger(__name__)
ir_configuration = Table('ir_configuration')
ir_lang = Table('ir_lang')
ir_module = Table('ir_module_module')
res_user = Table('res_user')
def dispatch(host, port, protocol, database_name, user, session, object_type,
object_name, method, *args, **kwargs):
Database = backend.get('Database')
DatabaseOperationalError = backend.get('DatabaseOperationalError')
if object_type == 'common':
if method == 'login':
try:
database = Database(database_name).connect()
cursor = database.cursor()
cursor.close()
except Exception:
return False
res = security.login(database_name, user, session)
with Transaction().start(database_name, 0):
Cache.clean(database_name)
Cache.resets(database_name)
msg = res and 'successful login' or 'bad login or password'
logger.info('%s \'%s\' from %s:%d using %s on database \'%s\''
% (msg, user, host, port, protocol, database_name))
return res or False
elif method == 'logout':
name = security.logout(database_name, user, session)
logger.info(('logout \'%s\' from %s:%d '
'using %s on database \'%s\'')
% (name, host, port, protocol, database_name))
return True
elif method == 'version':
return VERSION
elif method == 'list_lang':
return [
('bg_BG', 'Български'),
('ca_ES', 'Català'),
('cs_CZ', 'Čeština'),
('de_DE', 'Deutsch'),
('en_US', 'English'),
('es_AR', 'Español (Argentina)'),
('es_EC', 'Español (Ecuador)'),
('es_ES', 'Español (España)'),
('es_CO', 'Español (Colombia)'),
('fr_FR', 'Français'),
('lt_LT', 'Lietuvių'),
('nl_NL', 'Nederlands'),
('ru_RU', 'Russian'),
('sl_SI', 'Slovenščina'),
]
elif method == 'db_exist':
try:
database = Database(*args, **kwargs).connect()
cursor = database.cursor()
cursor.close(close=True)
return True
except Exception:
return False
elif method == 'list':
if not config.get('database', 'list'):
raise Exception('AccessDenied')
with Transaction().start(None, 0, close=True) as transaction:
return transaction.database.list(transaction.cursor)
elif method == 'create':
return create(*args, **kwargs)
elif method == 'restore':
return restore(*args, **kwargs)
elif method == 'drop':
return drop(*args, **kwargs)
elif method == 'dump':
return dump(*args, **kwargs)
return
elif object_type == 'system':
database = Database(database_name).connect()
database_list = Pool.database_list()
pool = Pool(database_name)
if not database_name in database_list:
pool.init()
if method == 'listMethods':
res = []
for type in ('model', 'wizard', 'report'):
for object_name, obj in pool.iterobject(type=type):
for method in obj.__rpc__:
res.append(type + '.' + object_name + '.' + method)
if hasattr(obj, '_buttons'):
for button in obj._buttons:
res.append(type + '.' + object_name + '.' + button)
return res
elif method == 'methodSignature':
return 'signatures not supported'
elif method == 'methodHelp':
res = []
args_list = args[0].split('.')
object_type = args_list[0]
object_name = '.'.join(args_list[1:-1])
method = args_list[-1]
obj = pool.get(object_name, type=object_type)
return pydoc.getdoc(getattr(obj, method))
for count in range(config.getint('database', 'retry'), -1, -1):
try:
user = security.check(database_name, user, session)
except DatabaseOperationalError:
if count:
continue
raise
break
database_list = Pool.database_list()
pool = Pool(database_name)
if not database_name in database_list:
with Transaction().start(database_name, user,
readonly=True) as transaction:
pool.init()
obj = pool.get(object_name, type=object_type)
if method in obj.__rpc__:
rpc = obj.__rpc__[method]
elif method in getattr(obj, '_buttons', {}):
rpc = RPC(readonly=False, instantiate=0)
else:
raise UserError('Calling method %s on %s %s is not allowed!'
% (method, object_type, object_name))
exception_message = ('Exception calling %s.%s.%s from %s@%s:%d/%s' %
(object_type, object_name, method, user, host, port, database_name))
for count in range(config.getint('database', 'retry'), -1, -1):
with Transaction().start(database_name, user,
readonly=rpc.readonly) as transaction:
Cache.clean(database_name)
try:
c_args, c_kwargs, transaction.context, transaction.timestamp \
= rpc.convert(obj, *args, **kwargs)
meth = getattr(obj, method)
if not hasattr(meth, 'im_self') or meth.im_self:
result = rpc.result(meth(*c_args, **c_kwargs))
else:
assert rpc.instantiate == 0
inst = c_args.pop(0)
if hasattr(inst, method):
result = rpc.result(meth(inst, *c_args, **c_kwargs))
else:
result = [rpc.result(meth(i, *c_args, **c_kwargs))
for i in inst]
if not rpc.readonly:
transaction.cursor.commit()
except DatabaseOperationalError:
transaction.cursor.rollback()
if count and not rpc.readonly:
continue
raise
except (NotLogged, ConcurrencyException, UserError, UserWarning):
logger.debug(exception_message, exc_info=sys.exc_info())
transaction.cursor.rollback()
raise
except Exception:
logger.error(exception_message, exc_info=sys.exc_info())
transaction.cursor.rollback()
raise
Cache.resets(database_name)
with Transaction().start(database_name, 0) as transaction:
pool = Pool(database_name)
Session = pool.get('ir.session')
try:
Session.reset(session)
except DatabaseOperationalError:
# Silently fail when reseting session
transaction.cursor.rollback()
else:
transaction.cursor.commit()
return result
def create(database_name, password, lang, admin_password):
'''
Create a database
:param database_name: the database name
:param password: the server password
:param lang: the default language for the database
:param admin_password: the admin password
:return: True if succeed
'''
Database = backend.get('Database')
security.check_super(password)
res = False
try:
with Transaction().start(None, 0, close=True, autocommit=True) \
as transaction:
transaction.database.create(transaction.cursor, database_name)
transaction.cursor.commit()
with Transaction().start(database_name, 0) as transaction:
Database.init(transaction.cursor)
transaction.cursor.execute(*ir_configuration.insert(
[ir_configuration.language], [[lang]]))
transaction.cursor.commit()
pool = Pool(database_name)
pool.init(update=['res', 'ir'], lang=[lang])
with Transaction().start(database_name, 0) as transaction:
User = pool.get('res.user')
Lang = pool.get('ir.lang')
language, = Lang.search([('code', '=', lang)])
language.translatable = True
language.save()
users = User.search([('login', '!=', 'root')])
User.write(users, {
'language': language.id,
})
admin, = User.search([('login', '=', 'admin')])
User.write([admin], {
'password': admin_password,
})
Module = pool.get('ir.module.module')
if Module:
Module.update_list()
transaction.cursor.commit()
res = True
except Exception:
logger.error('CREATE DB: %s failed' % database_name,
exc_info=sys.exc_info())
raise
else:
logger.info('CREATE DB: %s' % (database_name,))
return res
def drop(database_name, password):
Database = backend.get('Database')
security.check_super(password)
Database(database_name).close()
# Sleep to let connections close
time.sleep(1)
with Transaction().start(None, 0, close=True, autocommit=True) \
as transaction:
cursor = transaction.cursor
try:
Database.drop(cursor, database_name)
cursor.commit()
except Exception:
logger.error('DROP DB: %s failed' % database_name,
exc_info=sys.exc_info())
raise
else:
logger.info('DROP DB: %s' % (database_name))
Pool.stop(database_name)
return True
def dump(database_name, password):
Database = backend.get('Database')
security.check_super(password)
Database(database_name).close()
# Sleep to let connections close
time.sleep(1)
data = Database.dump(database_name)
logger.info('DUMP DB: %s' % (database_name))
return buffer(data)
def restore(database_name, password, data, update=False):
Database = backend.get('Database')
security.check_super(password)
try:
database = Database().connect()
cursor = database.cursor()
cursor.close(close=True)
raise Exception("Database already exists!")
except Exception:
pass
Database.restore(database_name, data)
logger.info('RESTORE DB: %s' % (database_name))
if update:
with Transaction().start(database_name, 0) as transaction:
cursor = transaction.cursor
cursor.execute(*ir_lang.select(ir_lang.code,
where=ir_lang.translatable))
lang = [x[0] for x in cursor.fetchall()]
cursor.execute(*ir_module.select(ir_module.name,
where=(ir_module.state == 'installed')))
update = [x[0] for x in cursor.fetchall()]
Pool(database_name).init(update=update, lang=lang)
logger.info('Update/Init succeed!')
return True
|