def drop_all(): from trytond.backend import Database database = Database().connect() cursor = database.cursor() erase_cursor = database.cursor(autocommit=True) for db in database.list(cursor): print "Dropping database %s" % db database.drop(erase_cursor, db) erase_cursor.close() cursor.close()
def get_childs(self, uri, filter=None): res = [] dbname, dburi = self._get_dburi(uri) if not dbname: database = Database().connect() cursor = database.cursor() try: lists = database.list(cursor) except Exception: lists = [] finally: cursor.close() for dbname in lists: res.append(urlparse.urljoin(uri, dbname)) return res pool = Pool(Transaction().cursor.database_name) try: Collection = pool.get('webdav.collection') scheme, netloc, path, params, query, fragment = \ urlparse.urlparse(uri) if path[-1:] != '/': path += '/' for child in Collection.get_childs(dburi, filter=filter, cache=CACHE): res.append(urlparse.urlunparse((scheme, netloc, path + child.encode('utf-8'), params, query, fragment))) except KeyError: return res except (DAV_Error, DAV_NotFound, DAV_Secret, DAV_Forbidden), exception: self._log_exception(exception) raise
def get_userinfo(self, user, password, command=''): path = urlparse.urlparse(self.path).path dbname = urllib.unquote_plus(path.split('/', 2)[1]) database = Database().connect() cursor = database.cursor() databases = database.list(cursor) cursor.close() if not dbname or dbname not in databases: return True if user: user = int(login(dbname, user, password, cache=False)) if not user: return None else: url = urlparse.urlparse(self.path) query = urlparse.parse_qs(url.query) path = url.path[len(dbname) + 2:] if 'key' in query: key, = query['key'] with Transaction().start(dbname, 0) as transaction: database_list = Pool.database_list() pool = Pool(dbname) if not dbname in database_list: pool.init() Share = pool.get('webdav.share') user = Share.get_login(key, command, path) transaction.cursor.commit() if not user: return None Transaction().start(dbname, user) Cache.clean(dbname) return user
def init(self): from trytond.config import CONFIG CONFIG['database_type'] = 'sqlite' self.db_name = ':memory:' from trytond.backend import Database database = Database() cursor = database.cursor() databases = database.list(cursor) cursor.close() if self.db_name not in databases: from trytond.protocols.dispatcher import create create(self.db_name, 'admin', 'en_US', 'admin') self.user = 1 self.context = None from trytond.pool import Pool from trytond.transaction import Transaction with Transaction().start(self.db_name, self.user, self.context) as txn: self.pool = Pool(self.db_name) self.pool.init() self.initialised = True
def restore(database_name, password, data, update=False): logger = logging.getLogger('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: cursor = Database(database_name).connect().cursor() cursor.execute('SELECT code FROM ir_lang ' \ 'WHERE translatable') lang = [x[0] for x in cursor.fetchall()] cursor.execute('UPDATE ir_module_module SET ' \ "state = 'to upgrade' " \ "WHERE state = 'installed'") cursor.commit() cursor.close() Pool(database_name).init(update=update, lang=lang) logger.info('Update/Init succeed!') return True
def install_module(name): ''' Install module for the tested database ''' database = Database().connect() cursor = database.cursor() databases = database.list(cursor) cursor.close() if DB_NAME not in databases: create(DB_NAME, CONFIG['admin_passwd'], 'en_US', USER_PASSWORD) with Transaction().start(DB_NAME, USER, context=CONTEXT) as transaction: module_obj = POOL.get('ir.module.module') module_ids = module_obj.search([ ('name', '=', name), ]) assert module_ids module_ids = module_obj.search([ ('name', '=', name), ('state', '!=', 'installed'), ]) if not module_ids: return module_obj.install(module_ids) transaction.cursor.commit() install_upgrade_obj = POOL.get('ir.module.module.install_upgrade', type='wizard') install_upgrade_obj.transition_upgrade(None) transaction.cursor.commit()
def database_list(): """Fetch all databases in current instance""" from trytond.backend import Database database = Database().connect() cursor = database.cursor() databases = database.list(cursor) cursor.close() return databases
def start(self, database_name, user, readonly=False, context=None): ''' Start transaction ''' assert self.user is None assert self.cursor is None assert self.context is None database = Database(database_name).connect() self.cursor = database.cursor(readonly=readonly) self.user = user self.context = context or {} self.create_records = {} self.delete_records = {} self.delete = {} self.timestamp = {} return _TransactionManager()
def clean(dbname): if not CONFIG["multi_server"]: return database = Database(dbname).connect() cursor = database.cursor() try: cursor.execute('SELECT "timestamp", "name" FROM ir_cache') timestamps = {} for timestamp, name in cursor.fetchall(): timestamps[name] = timestamp finally: cursor.commit() cursor.close() for inst in Cache._cache_instance: if inst._name in timestamps: with inst._lock: if not inst._timestamp or timestamps[inst._name] > inst._timestamp: inst._timestamp = timestamps[inst._name] inst._cache[dbname] = LRUDict(inst.size_limit)
def clean(dbname): if not CONFIG['multi_server']: return database = Database(dbname).connect() cursor = database.cursor() try: cursor.execute('SELECT "timestamp", "name" FROM ir_cache') timestamps = {} for timestamp, name in cursor.fetchall(): timestamps[name] = timestamp finally: cursor.commit() cursor.close() for obj in Cache._cache_instance: if obj._name in timestamps: if (not obj._timestamp or timestamps[obj._name] > obj._timestamp): obj._timestamp = timestamps[obj._name] obj._lock.acquire() try: obj._cache[dbname] = {} finally: obj._lock.release()
def resets(dbname): if not CONFIG['multi_server']: return database = Database(dbname).connect() cursor = database.cursor() Cache._resets_lock.acquire() Cache._resets.setdefault(dbname, set()) try: for name in Cache._resets[dbname]: cursor.execute('SELECT name FROM ir_cache WHERE name = %s', (name,)) if cursor.fetchone(): cursor.execute('UPDATE ir_cache SET "timestamp" = %s '\ 'WHERE name = %s', (datetime.datetime.now(), name)) else: cursor.execute('INSERT INTO ir_cache ' \ '("timestamp", "name") ' \ 'VALUES (%s, %s)', (datetime.datetime.now(), name)) Cache._resets[dbname].clear() finally: cursor.commit() cursor.close() Cache._resets_lock.release()
def drop(database_name, password): security.check_super(password) Database(database_name).close() # Sleep to let connections close time.sleep(1) logger = logging.getLogger('database') database = Database().connect() cursor = database.cursor(autocommit=True) try: try: database.drop(cursor, database_name) cursor.commit() except Exception: logger.error('DROP DB: %s failed' % (database_name,)) tb_s = ''.join(traceback.format_exception(*sys.exc_info())) logger.error('Exception in call: \n' + tb_s) raise else: logger.info('DROP DB: %s' % (database_name)) Pool.stop(database_name) finally: cursor.close(close=True) return True
def resets(dbname): if not CONFIG["multi_server"]: return database = Database(dbname).connect() cursor = database.cursor() try: with Cache._resets_lock: Cache._resets.setdefault(dbname, set()) for name in Cache._resets[dbname]: cursor.execute("SELECT name FROM ir_cache WHERE name = %s", (name,)) if cursor.fetchone(): # It would be better to insert only cursor.execute( 'UPDATE ir_cache SET "timestamp" = %s ' "WHERE name = %s", (datetime.datetime.now(), name) ) else: cursor.execute( "INSERT INTO ir_cache " '("timestamp", "name") ' "VALUES (%s, %s)", (datetime.datetime.now(), name), ) Cache._resets[dbname].clear() finally: cursor.commit() cursor.close()
def new_cursor(self): manager = _CursorManager(self.cursor) database = Database(self.cursor.database_name).connect() self.cursor = database.cursor() return manager
def run(self): "Run the server and never return" from trytond.backend import Database from trytond.pool import Pool from trytond.monitor import monitor 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 CONFIG['psyco']: import psyco psyco.full() 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 database = Database(db_name).connect() cursor = database.cursor() try: if CONFIG['init']: if not cursor.test(): self.logger.info("init db") Database.init(cursor) init[db_name] = True cursor.commit() elif not cursor.test(): raise Exception("'%s' is not a Tryton database!" % db_name) finally: cursor.close() for db_name in CONFIG["db_name"]: if update: cursor = Database(db_name).connect().cursor() try: 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()] finally: cursor.close() 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]: 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 database = Database(db_name).connect() cursor = database.cursor() try: salt = ''.join(random.sample( string.letters + string.digits, 8)) password += salt if hashlib: password = hashlib.sha1(password).hexdigest() else: password = sha.new(password).hexdigest() cursor.execute('UPDATE res_user ' \ 'SET password = %s, salt = %s ' \ 'WHERE login = \'admin\'', (password, salt)) cursor.commit() finally: cursor.close() 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_obj = pool.get('ir.cron') finally: pool.lock.release() thread = threading.Thread( target=cron_obj.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)
from trytond.modules import register_classes from tryton.pool import Pool from trytond.backend import Database from trytond.tools import Cache #register class populates the pool of moduls: register_classes() #Instantiate the dataase and the pool DB = Database(settings,TRYTON_db).connect() POOL = Pool(setting.TRYTON_DB) POOL.init() user_obj = POOL.get('res_user') cursur = DB.cursor() Cache.clean(settings.TRYTON_DB) try: #user 0 is aroot user .we ue it to get user id USER = user.obj.search(cursor,0, [ ('login','=',settings.TRYTON_UN), ], limit=1)[0] finally: cursor.close() Cache.reset(settings.TRYTON_DB)
warnings.filterwarnings("ignore", message="Old style callback, usecb_func(ok, store) instead") TRYTOND_PATH = settings.TRYTOND_PATH DIR = os.path.abspath(os.path.normpath(os.path.join(TRYTOND_PATH,'trytond'))) if os.path.isdir(DIR): sys.path.insert(0, os.path.dirname(DIR)) from trytond.modules import register_classes from trytond.pool import Pool from trytond.backend import Database from trytond.tools import Cache # Register classes populates the pool of models: register_classes() # Instantiate the database and the pool DB = Database(settings.TRYTON_DB).connect() POOL = Pool(settings.TRYTON_DB) POOL.init() user_obj = POOL.get('res.user') cursor = DB.cursor() Cache.clean(settings.TRYTON_DB) try: # User 0 is root user. We use it to get the user id: USER = user_obj.search(cursor, 0, [ ('login', '=', settings.TRYTON_UN), ], limit=1)[0] finally: cursor.close() Cache.resets(settings.TRYTON_DB)
def run(self): "Run the server and never return" from trytond.backend import Database from trytond.pool import Pool from trytond.monitor import monitor 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 database = Database(db_name).connect() cursor = database.cursor() try: if CONFIG['init']: if not cursor.test(): self.logger.info("init db") Database.init(cursor) init[db_name] = True cursor.commit() elif not cursor.test(): raise Exception("'%s' is not a Tryton database!" % db_name) finally: cursor.close() for db_name in CONFIG["db_name"]: if update: cursor = Database(db_name).connect().cursor() try: 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()] finally: cursor.close() 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 database = Database(db_name).connect() cursor = database.cursor() try: salt = ''.join(random.sample( string.letters + string.digits, 8)) password += salt password = hashlib.sha1(password).hexdigest() cursor.execute('UPDATE res_user ' 'SET password = %s, salt = %s ' 'WHERE login = \'admin\'', (password, salt)) cursor.commit() finally: cursor.close()
def dispatch(host, port, protocol, database_name, user, session, object_type, object_name, method, *args, **kargs): 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) Cache.clean(database_name) logger = logging.getLogger('dispatcher') 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)) Cache.resets(database_name) return res or False elif method == 'logout': name = security.logout(database_name, user, session) logger = logging.getLogger('dispatcher') 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 == 'timezone_get': return CONFIG['timezone'] 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_ES', 'Español (España)'), ('es_CO', 'Español (Colombia)'), ('fr_FR', 'Français'), ('nl_NL', 'Nederlands'), ('ru_RU', 'Russian'), ] elif method == 'db_exist': try: database = Database(*args, **kargs).connect() cursor = database.cursor() cursor.close(close=True) return True except Exception: return False elif method == 'list': if CONFIG['prevent_dblist']: raise Exception('AccessDenied') database = Database().connect() try: cursor = database.cursor() try: res = database.list(cursor) finally: cursor.close(close=True) except Exception: res = [] return res elif method == 'create': return create(*args, **kargs) elif method == 'restore': return restore(*args, **kargs) elif method == 'drop': return drop(*args, **kargs) elif method == 'dump': return dump(*args, **kargs) 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) 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)) user = security.check(database_name, user, session) Cache.clean(database_name) 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: readonly = not obj._rpc[method] elif method in getattr(obj, '_buttons', {}): readonly = False else: raise UserError('Calling method %s on %s %s is not allowed!' % (method, object_type, object_name)) for count in range(int(CONFIG['retry']), -1, -1): with Transaction().start(database_name, user, readonly=readonly) as transaction: try: args_without_context = list(args) if 'context' in kargs: context = kargs.pop('context') else: context = args_without_context.pop() if '_timestamp' in context: transaction.timestamp = context['_timestamp'] del context['_timestamp'] transaction.context = context res = getattr(obj, method)(*args_without_context, **kargs) if not readonly: transaction.cursor.commit() except DatabaseOperationalError, exception: transaction.cursor.rollback() if count and not readonly: continue raise except Exception, exception: if CONFIG['verbose'] and not isinstance(exception, ( NotLogged, ConcurrencyException, UserError, UserWarning)): tb_s = ''.join(traceback.format_exception(*sys.exc_info())) logger = logging.getLogger('dispatcher') logger.error('Exception calling method %s on ' '%s %s from %s@%s:%d/%s:\n' % (method, object_type, object_name, user, host, port, database_name) + tb_s) transaction.cursor.rollback() raise
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 ''' security.check_super(password) res = False logger = logging.getLogger('database') try: database = Database().connect() cursor = database.cursor(autocommit=True) try: database.create(cursor, database_name) cursor.commit() cursor.close(close=True) except Exception: cursor.close() raise with Transaction().start(database_name, 0) as transaction: database.init(transaction.cursor) transaction.cursor.commit() pool = Pool(database_name) pool.init(update=True, lang=[lang]) with Transaction().start(database_name, 0) as transaction: cursor = transaction.cursor #XXX replace with model write cursor.execute('UPDATE ir_lang ' \ 'SET translatable = %s ' \ 'WHERE code = %s', (True, lang)) cursor.execute('UPDATE res_user ' \ 'SET language = (' + \ cursor.limit_clause('SELECT id FROM ir_lang ' \ 'WHERE code = %s', 1) + ')' \ 'WHERE login <> \'root\'', (lang,)) if hashlib: admin_password = hashlib.sha1(admin_password).hexdigest() else: admin_password = sha.new(admin_password).hexdigest() cursor.execute('UPDATE res_user ' \ 'SET password = %s ' \ 'WHERE login = \'admin\'', (admin_password,)) module_obj = pool.get('ir.module.module') if module_obj: module_obj.update_list() cursor.commit() res = True except Exception: logger.error('CREATE DB: %s failed' % (database_name,)) tb_s = ''.join(traceback.format_exception(*sys.exc_info())) logger.error('Exception in call: \n' + tb_s) raise else: logger.info('CREATE DB: %s' % (database_name,)) return res