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 314 315 316
|
# -*- coding: utf-8 -*-
# Copyright 2014 Sébastien Alix
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl)
"""Provide the :class:`DB` class to manage the server databases."""
import base64
import io
import sys
from odoorpc import error
from odoorpc.tools import v
# Python 2
if sys.version_info[0] < 3:
def encode2bytes(data):
return data
# Python >= 3
else:
def encode2bytes(data):
return bytes(data, 'ascii')
class DB(object):
"""The `DB` class represents the database management service.
It provides functionalities such as list, create, drop, dump
and restore databases.
.. note::
This service have to be used through the :attr:`odoorpc.ODOO.db`
property.
>>> import odoorpc
>>> odoo = odoorpc.ODOO('localhost') # doctest: +SKIP
>>> odoo.db
<odoorpc.db.DB object at 0x...>
"""
def __init__(self, odoo):
self._odoo = odoo
def dump(self, password, db, format_='zip'):
"""Backup the `db` database. Returns the dump as a binary ZIP file
containing the SQL dump file alongside the filestore directory (if any).
>>> dump = odoo.db.dump('super_admin_passwd', 'prod') # doctest: +SKIP
.. doctest::
:hide:
>>> dump = odoo.db.dump(SUPER_PWD, DB)
If you get a timeout error, increase this one before performing the
request:
>>> timeout_backup = odoo.config['timeout']
>>> odoo.config['timeout'] = 600 # Timeout set to 10 minutes
>>> dump = odoo.db.dump('super_admin_passwd', 'prod') # doctest: +SKIP
>>> odoo.config['timeout'] = timeout_backup
Write it on the file system:
.. doctest::
:options: +SKIP
>>> with open('dump.zip', 'wb') as dump_zip:
... dump_zip.write(dump.read())
...
.. doctest::
:hide:
>>> with open('dump.zip', 'wb') as dump_zip:
... fileno = dump_zip.write(dump.read()) # Python 3
...
You can manipulate the file with the `zipfile` module for instance:
.. doctest::
:options: +SKIP
>>> import zipfile
>>> zipfile.ZipFile('dump.zip').namelist()
['dump.sql',
'filestore/ef/ef2c882a36dbe90fc1e7e28d816ad1ac1464cfbb',
'filestore/dc/dcf00aacce882bbfd117c0277e514f829b4c5bf0',
...]
.. doctest::
:hide:
>>> import zipfile
>>> zipfile.ZipFile('dump.zip').namelist() # doctest: +NORMALIZE_WHITESPACE
['dump.sql'...'filestore/...'...]
The super administrator password is required to perform this method.
*Python 2:*
:return: `io.BytesIO`
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database)
:raise: `urllib2.URLError` (connection error)
*Python 3:*
:return: `io.BytesIO`
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database)
:raise: `urllib.error.URLError` (connection error)
"""
args = [password, db]
if v(self._odoo.version)[0] >= 9:
args.append(format_)
data = self._odoo.json(
'/jsonrpc', {'service': 'db', 'method': 'dump', 'args': args}
)
# Encode to bytes forced to be compatible with Python 3.2
# (its 'base64.standard_b64decode()' function only accepts bytes)
result = encode2bytes(data['result'])
content = base64.standard_b64decode(result)
return io.BytesIO(content)
def change_password(self, password, new_password):
"""Change the administrator password by `new_password`.
>>> odoo.db.change_password('super_admin_passwd', 'new_admin_passwd') # doctest: +SKIP
.. doctest:
:hide:
>>> odoo.db.change_password(SUPER_PWD, 'new_admin_passwd')
>>> odoo.db.change_password('new_admin_passwd', SUPER_PWD)
The super administrator password is required to perform this method.
*Python 2:*
:raise: :class:`odoorpc.error.RPCError` (access denied)
:raise: `urllib2.URLError` (connection error)
*Python 3:*
:raise: :class:`odoorpc.error.RPCError` (access denied)
:raise: `urllib.error.URLError` (connection error)
"""
self._odoo.json(
'/jsonrpc',
{
'service': 'db',
'method': 'change_admin_password',
'args': [password, new_password],
},
)
def create(
self, password, db, demo=False, lang='en_US', admin_password='admin'
):
"""Request the server to create a new database named `db`
which will have `admin_password` as administrator password and
localized with the `lang` parameter.
You have to set the flag `demo` to `True` in order to insert
demonstration data.
>>> odoo.db.create('super_admin_passwd', 'prod', False, 'fr_FR', 'my_admin_passwd') # doctest: +SKIP
If you get a timeout error, increase this one before performing the
request:
>>> timeout_backup = odoo.config['timeout']
>>> odoo.config['timeout'] = 600 # Timeout set to 10 minutes
>>> odoo.db.create('super_admin_passwd', 'prod', False, 'fr_FR', 'my_admin_passwd') # doctest: +SKIP
>>> odoo.config['timeout'] = timeout_backup
The super administrator password is required to perform this method.
*Python 2:*
:raise: :class:`odoorpc.error.RPCError` (access denied)
:raise: `urllib2.URLError` (connection error)
*Python 3:*
:raise: :class:`odoorpc.error.RPCError` (access denied)
:raise: `urllib.error.URLError` (connection error)
"""
self._odoo.json(
'/jsonrpc',
{
'service': 'db',
'method': 'create_database',
'args': [password, db, demo, lang, admin_password],
},
)
def drop(self, password, db):
"""Drop the `db` database. Returns `True` if the database was removed,
`False` otherwise (database did not exist):
>>> odoo.db.drop('super_admin_passwd', 'test') # doctest: +SKIP
True
The super administrator password is required to perform this method.
*Python 2:*
:return: `True` or `False`
:raise: :class:`odoorpc.error.RPCError` (access denied)
:raise: `urllib2.URLError` (connection error)
*Python 3:*
:return: `True` or `False`
:raise: :class:`odoorpc.error.RPCError` (access denied)
:raise: `urllib.error.URLError` (connection error)
"""
if self._odoo._env and self._odoo._env.db == db:
# Remove the existing session to avoid HTTP session error
self._odoo.logout()
data = self._odoo.json(
'/jsonrpc',
{'service': 'db', 'method': 'drop', 'args': [password, db]},
)
return data['result']
def duplicate(self, password, db, new_db):
"""Duplicate `db' as `new_db`.
>>> odoo.db.duplicate('super_admin_passwd', 'prod', 'test') # doctest: +SKIP
The super administrator password is required to perform this method.
*Python 2:*
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database)
:raise: `urllib2.URLError` (connection error)
*Python 3:*
:raise: :class:`odoorpc.error.RPCError` (access denied / wrong database)
:raise: `urllib.error.URLError` (connection error)
"""
self._odoo.json(
'/jsonrpc',
{
'service': 'db',
'method': 'duplicate_database',
'args': [password, db, new_db],
},
)
def list(self):
"""Return the list of the databases:
>>> odoo.db.list() # doctest: +SKIP
['prod', 'test']
*Python 2:*
:return: `list` of database names
:raise: `urllib2.URLError` (connection error)
*Python 3:*
:return: `list` of database names
:raise: `urllib.error.URLError` (connection error)
"""
data = self._odoo.json(
'/jsonrpc', {'service': 'db', 'method': 'list', 'args': []}
)
return data.get('result', [])
def restore(self, password, db, dump, copy=False):
"""Restore the `dump` database into the new `db` database.
The `dump` file object can be obtained with the
:func:`dump <DB.dump>` method.
If `copy` is set to `True`, the restored database will have a new UUID.
>>> odoo.db.restore('super_admin_passwd', 'test', dump_file) # doctest: +SKIP
If you get a timeout error, increase this one before performing the
request:
>>> timeout_backup = odoo.config['timeout']
>>> odoo.config['timeout'] = 7200 # Timeout set to 2 hours
>>> odoo.db.restore('super_admin_passwd', 'test', dump_file) # doctest: +SKIP
>>> odoo.config['timeout'] = timeout_backup
The super administrator password is required to perform this method.
*Python 2:*
:raise: :class:`odoorpc.error.RPCError`
(access denied / database already exists)
:raise: :class:`odoorpc.error.InternalError` (dump file closed)
:raise: `urllib2.URLError` (connection error)
*Python 3:*
:raise: :class:`odoorpc.error.RPCError`
(access denied / database already exists)
:raise: :class:`odoorpc.error.InternalError` (dump file closed)
:raise: `urllib.error.URLError` (connection error)
"""
if dump.closed:
raise error.InternalError("Dump file closed")
b64_data = base64.standard_b64encode(dump.read()).decode()
self._odoo.json(
'/jsonrpc',
{
'service': 'db',
'method': 'restore',
'args': [password, db, b64_data, copy],
},
)
|