diff options
author | Sylvain <syt@logilab.fr> | 2006-10-22 00:07:28 +0200 |
---|---|---|
committer | Sylvain <syt@logilab.fr> | 2006-10-22 00:07:28 +0200 |
commit | 8acc5f576f3c14ed93f383bcfb28e0f351733a97 (patch) | |
tree | 2c3cfac5acdcf5515cd15e0a347f4e6b74d643a1 | |
parent | bac7aea7ce9453d90e36e22ec27dcf5af9de70b9 (diff) | |
download | logilab-common-8acc5f576f3c14ed93f383bcfb28e0f351733a97.tar.gz |
restore commands
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | __init__.py | 43 | ||||
-rw-r--r-- | db.py | 26 |
3 files changed, 30 insertions, 42 deletions
@@ -4,9 +4,10 @@ ChangeLog for logilab.common -- * db: - fixed date handling - - new method on advanced helper to generate backup command + - new methods on advanced helper to generate backup commands * configuration: basic deprecated config handling support * new implementation of pytest + * backport a dot backend from yams into a new "graph" module 2006-10-03 -- 0.19.3 * fixed bug in textutils.normalise_[text|paragraph] with unsplitable diff --git a/__init__.py b/__init__.py index ce49f2b..71a1f1c 100644 --- a/__init__.py +++ b/__init__.py @@ -18,6 +18,9 @@ Logilab common libraries from __future__ import nested_scopes +# bw compat +from logilab.common.graph import get_cycles + # FIXME: move all those functions in a separated module def intersection(list1, list2): @@ -94,46 +97,6 @@ def flatten(iterable, tr_func=None, results=None): return results -def get_cycles(graph_dict, vertices=None): - '''given a dictionnary representing an ordered graph (i.e. key are vertices - and values is a list of destination vertices representing edges), return a - list of detected cycles - ''' - if not graph_dict: - return () - result = [] - if vertices is None: - vertices = graph_dict.keys() - for vertice in vertices: - _get_cycles(graph_dict, vertice, [], result) - return result - -def _get_cycles(graph_dict, vertice=None, path=None, result=None): - """recursive function doing the real work for get_cycles""" - if vertice in path: - cycle = [vertice] - for i in range(len(path)-1, 0, -1): - node = path[i] - if node == vertice: - break - cycle.insert(0, node) - # make a canonical representation - start_from = min(cycle) - index = cycle.index(start_from) - cycle = cycle[index:] + cycle[0:index] - # append it to result if not already in - if not cycle in result: - result.append(cycle) - return - path.append(vertice) - try: - for node in graph_dict[vertice]: - _get_cycles(graph_dict, node, path, result) - except KeyError: - pass - path.pop() - - def cached(callableobj, keyarg=None): """simple decorator to cache result of method call""" #print callableobj, keyarg, callableobj.func_code.co_argcount @@ -444,7 +444,31 @@ class _PGAdvFuncHelper(_GenericAdvFuncHelper): cmd.append('--file=%s' % backupfile) cmd.append(dbname) return ' '.join(cmd) - + + def restore_commands(self, dbname, dbhost, dbuser, backupfile, + encoding='UTF8', drop=True): + """return a command to restore a backup the given database""" + cmds = [] + if drop: + cmd = dbcmd('dropdb', dbhost, dbuser) + cmd.append(dbname) + cmds.append(' '.join(cmd)) + cmd = dbcmd('createdb -T template0 -E %s' % encoding, dbhost, dbuser) + cmd.append(dbname) + cmds.append(' '.join(cmd)) + cmd = dbcmd('pg_restore -Fc', dbhost, dbuser) + cmd.append('--dbname %s' % dbname) + cmd.append(backupfile) + cmds.append(' '.join(cmd)) + return cmds + +def dbcmd(cmd, dbhost, dbuser): + cmd = [cmd] + if dbhost: + cmd.append('--host=%s' % dbhost) + if dbuser: + cmd.append('--username=%s' % dbuser) + return cmd def sql_create_sequence(self, seq_name): return 'CREATE SEQUENCE %s;' % seq_name |