diff options
author | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2010-02-04 15:54:44 +0100 |
---|---|---|
committer | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2010-02-04 15:54:44 +0100 |
commit | 103fb1904920a672f74417d7866b842c82c83c95 (patch) | |
tree | 879b590afd96bc3a0d49100926fe96b4d2ab8b81 | |
parent | d06ff54a47260ccd355f77bb8190dd1395de5101 (diff) | |
parent | 25bcd2a5ab8ba5667cfed3260db88fb674f98b40 (diff) | |
download | logilab-common-103fb1904920a672f74417d7866b842c82c83c95.tar.gz |
merge with 0.46.0
-rw-r--r-- | adbh.py | 69 | ||||
-rw-r--r-- | configuration.py | 4 | ||||
-rw-r--r-- | optik_ext.py | 2 | ||||
-rw-r--r-- | textutils.py | 6 |
4 files changed, 48 insertions, 33 deletions
@@ -3,7 +3,7 @@ Helpers are provided for postgresql, mysql and sqlite. :copyright: - 2000-2009 `LOGILAB S.A. <http://www.logilab.fr>`_ (Paris, FRANCE), + 2000-2010 `LOGILAB S.A. <http://www.logilab.fr>`_ (Paris, FRANCE), all rights reserved. :contact: @@ -16,6 +16,8 @@ Helpers are provided for postgresql, mysql and sqlite. """ __docformat__ = "restructuredtext en" +import os +import sys class BadQuery(Exception): pass class UnsupportedFunction(BadQuery): pass @@ -165,16 +167,28 @@ class _GenericAdvFuncHelper: """return the system database for the given driver""" raise NotImplementedError('not supported by this DBMS') - def backup_command(self, dbname, dbhost, dbuser, dbpassword, backupfile, + def backup_command(self, dbname, dbhost, dbuser, backupfile, keepownership=True): - """return a command to backup the given database""" + """return a list of commands to backup the given database. + + Each command may be given as a list or as a string. In the latter case, + expected to be used with a subshell (for instance using `os.system(cmd)` + or `subprocess.call(cmd, shell=True)` + """ raise NotImplementedError('not supported by this DBMS') def restore_commands(self, dbname, dbhost, dbuser, backupfile, encoding='utf-8', keepownership=True, drop=True): - """return a list of commands to restore a backup the given database""" + """return a list of commands to restore a backup of the given database + + + Each command may be given as a list or as a string. In the latter case, + expected to be used with a subshell (for instance using `os.system(cmd)` + or `subprocess.call(cmd, shell=True)` + """ raise NotImplementedError('not supported by this DBMS') + # helpers to standardize SQL according to the database def sql_current_date(self): @@ -291,8 +305,9 @@ INSERT INTO %s VALUES (0);''' % (seq_name, seq_name) -def pgdbcmd(cmd, dbhost, dbuser): +def pgdbcmd(cmd, dbhost, dbuser, *args): cmd = [cmd] + cmd += args if dbhost: cmd.append('--host=%s' % dbhost) if dbuser: @@ -313,35 +328,35 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper): def backup_command(self, dbname, dbhost, dbuser, backupfile, keepownership=True): - """return a command to backup the given database""" - cmd = ['pg_dump -Fc'] + cmd = ['pg_dump', '-Fc'] if dbhost: cmd.append('--host=%s' % dbhost) if dbuser: cmd.append('--username=%s' % dbuser) if not keepownership: cmd.append('--no-owner') - cmd.append('--file="%s"' % backupfile) + cmd.append('--file') + cmd.append(backupfile) cmd.append(dbname) - return ' '.join(cmd) + return cmd def restore_commands(self, dbname, dbhost, dbuser, backupfile, encoding='utf-8', keepownership=True, drop=True): - """return a list of commands to restore a backup the given database""" cmds = [] if drop: cmd = pgdbcmd('dropdb', dbhost, dbuser) cmd.append(dbname) - cmds.append(' '.join(cmd)) - cmd = pgdbcmd('createdb -T template0 -E %s' % encoding, dbhost, dbuser) + cmds.append(cmd) + cmd = pgdbcmd('createdb', dbhost, dbuser, '-T', 'template0', '-E', encoding) + cmd.append(dbname) + cmds.append(cmd) + cmd = pgdbcmd('pg_restore', dbhost, dbuser, '-Fc') + cmd.append('--dbname') cmd.append(dbname) - cmds.append(' '.join(cmd)) - cmd = pgdbcmd('pg_restore -Fc', dbhost, dbuser) - cmd.append('--dbname %s' % dbname) if not keepownership: cmd.append('--no-owner') - cmd.append('"%s"' % backupfile) - cmds.append(' '.join(cmd)) + cmd.append(backupfile) + cmds.append(cmd) return cmds def sql_create_sequence(self, seq_name): @@ -423,13 +438,13 @@ class _SqliteAdvFuncHelper(_GenericAdvFuncHelper): def backup_command(self, dbname, dbhost, dbuser, backupfile, keepownership=True): - """return a command to backup the given database""" - return 'gzip --stdout %s > %s' % (dbname, backupfile) + return ['gzip', dbname], ['mv', dbname + '.gz', backupfile] def restore_commands(self, dbname, dbhost, dbuser, backupfile, encoding='utf-8', keepownership=True, drop=True): - """return a list of commands to restore a backup the given database""" - return ['gunzip --stdout %s > %s' % (backupfile, dbname)] + gunziped, ext = os.pathsplitext(backupfile) + assert ext.lower() in ('.gz', '.z') # else gunzip will fail anyway + return [['gunzip', backupfile], ['mv', gunziped, dbname]] def sql_create_index(self, table, column, unique=False): idx = self._index_name(table, column, unique) @@ -479,18 +494,15 @@ class _MyAdvFuncHelper(_GenericAdvFuncHelper): def backup_command(self, dbname, dbhost, dbuser, backupfile, keepownership=True): - """return a command to backup the given database""" + cmd = ['mysqldump'] # XXX compress if dbhost is not None: - host_option = '-h %s' % dbhost - else: - host_option = '' - return 'mysqldump %s -u %s -p -r %s %s' % (host_option, dbuser, - backupfile, dbname) + cmd += ('-h', dbhost) + cmd += ['-u', dbuser, '-p', '-r', backupfile, dbname] + return cmd def restore_commands(self, dbname, dbhost, dbuser, backupfile, encoding='utf-8', keepownership=True, drop=True): - """return a list of commands to restore a backup the given database""" cmds = [] host_option = '' if dbhost is not None: @@ -594,7 +606,6 @@ class _SqlServer2005FuncHelper(_GenericAdvFuncHelper): def binary_value(self, value): return StringIO.StringIO(value) - ADV_FUNC_HELPER_DIRECTORY = {'postgres': _PGAdvFuncHelper(), 'sqlite': _SqliteAdvFuncHelper(), 'mysql': _MyAdvFuncHelper(), diff --git a/configuration.py b/configuration.py index 6172c5c..6d03af1 100644 --- a/configuration.py +++ b/configuration.py @@ -311,8 +311,8 @@ def format_option_value(optdict, value): value = "'%s'" % value elif optdict.get('type') == 'time' and isinstance(value, (float, int, long)): value = "%ss" % value - elif optdict.get('type') == 'bytes' and isinstance(value, (int, long)): - value = "%sB" % value + elif optdict.get('type') == 'bytes' and hasattr(value, '__int__'): + value = "%iB" % value return value def ini_format_section(stream, section, options, encoding=None, doc=None): diff --git a/optik_ext.py b/optik_ext.py index 34dbfe9..86c2ffd 100644 --- a/optik_ext.py +++ b/optik_ext.py @@ -176,7 +176,7 @@ def check_time(option, opt, value): def check_bytes(option, opt, value): from logilab.common.textutils import BYTE_UNITS, apply_units - if instance(value, (int, long)): + if hasattr(value, '__int__'): return value return apply_units(value, BYTE_UNITS) diff --git a/textutils.py b/textutils.py index 38f59c7..37d4f48 100644 --- a/textutils.py +++ b/textutils.py @@ -289,7 +289,11 @@ def apply_units( string, units, inter=None, final=float, blank_reg=_BLANK_RE, lit, unit = dic["value"], dic.get("unit") value = inter(lit) if unit is not None: - value *= units[unit] + try: + value *= units[unit] + except KeyError: + raise KeyError('invalid unit %s. valid units are %s' % + unit, units.keys()) values.append(value) return final(sum(values)) |