|
| 1 | +## |
| 2 | +# copyright 2008, pg/python project. |
| 3 | +# http://python.projects.postgresql.org |
| 4 | +## |
| 5 | +""" |
| 6 | +Create and interface with PostgreSQL clusters. |
| 7 | +""" |
| 8 | +import sys |
| 9 | +import os |
| 10 | +import subprocess as sp |
| 11 | +import warnings |
| 12 | +from . import api as pg_api |
| 13 | +from . import configfile |
| 14 | + |
| 15 | +DEFAULT_CONFIG_FILENAME = 'postgresql.conf' |
| 16 | +DEFAULT_HBA_FILENAME = 'pg_hba.conf' |
| 17 | + |
| 18 | +initdb_option_map = { |
| 19 | + 'encoding' : '-E', |
| 20 | + 'locale' : '--locale', |
| 21 | + 'collate' : '--lc-collate', |
| 22 | + 'ctype' : '--lc-ctype', |
| 23 | + 'monetary' : '--lc-monetary', |
| 24 | + 'numeric' : '--lc-numeric', |
| 25 | + 'time' : '--lc-time', |
| 26 | + 'authentication' : '-A', |
| 27 | + 'superusername' : '-U', |
| 28 | + 'superuserpass' : '--pwfile', |
| 29 | + 'verbose' : '-d', |
| 30 | + 'sources' : '-L', |
| 31 | +} |
| 32 | + |
| 33 | +class Cluster(pg_api.Cluster): |
| 34 | + """ |
| 35 | + """ |
| 36 | + @property |
| 37 | + def settings(self): |
| 38 | + if not hasattr(self, '_settings'): |
| 39 | + self._settings = configfile.ConfigFile(self.pgsql_dot_conf) |
| 40 | + return self._settings |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def create(cls, |
| 44 | + path : "path to give to initdb", |
| 45 | + initdb = 'initdb', |
| 46 | + **kw |
| 47 | + ): |
| 48 | + """ |
| 49 | + Create the cluster at the given `datadir` using the |
| 50 | + provided keyword parameters as options to the command. |
| 51 | +
|
| 52 | + `command_option_map` provides the mapping of keyword arguments |
| 53 | + to command options. |
| 54 | + """ |
| 55 | + # Transform keyword options into command options for the executable. |
| 56 | + opts = [] |
| 57 | + for x in kw: |
| 58 | + if x in ('nowait', 'logfile', 'extra_arguments'): |
| 59 | + continue |
| 60 | + if x not in self.command_option_map: |
| 61 | + raise TypeError("got an unexpected keyword argument %r" %(x,)) |
| 62 | + opts.append(self.command_option_map[x]) |
| 63 | + opts.append(kw[x]) |
| 64 | + nowait = kw.get('nowait', False) |
| 65 | + logfile = kw.get('logfile', sp.PIPE) |
| 66 | + extra_args = kw.get('extra_arguments', ()) |
| 67 | + |
| 68 | + cmd = (self._path, '-D', datadir) + tuple(opts) + extra_args |
| 69 | + p = sp.Popen( |
| 70 | + cmd, |
| 71 | + close_fds = True, |
| 72 | + stdin = sp.PIPE, |
| 73 | + stdout = logfile, |
| 74 | + stderr = sp.PIPE |
| 75 | + ) |
| 76 | + p.stdin.close() |
| 77 | + |
| 78 | + if nowait: |
| 79 | + return p |
| 80 | + |
| 81 | + try: |
| 82 | + rc = p.wait() |
| 83 | + except KeyboardInterrupt: |
| 84 | + os.kill(p.pid, signal.SIGINT) |
| 85 | + raise |
| 86 | + |
| 87 | + if rc != 0: |
| 88 | + raise InitDBError(cmd, rc, p.stderr.read()) |
| 89 | + |
| 90 | + def __init__(self, path, postgres_path = 'postgres'): |
| 91 | + """ |
| 92 | + """ |
| 93 | + if not os.path.isdir(path): |
| 94 | + raise ValueError("cluster at %s does not exist" %(path,)) |
| 95 | + self.path = path |
| 96 | + |
| 97 | + def __repr__(self): |
| 98 | + return "%s.%s(%r, %r)" %( |
| 99 | + type(self).__module__, |
| 100 | + type(self).__name__, |
| 101 | + self.path, |
| 102 | + self.postgres_path, |
| 103 | + ) |
| 104 | + |
| 105 | + def drop(self): |
| 106 | + 'Stop the cluster and delete it from the filesystem' |
| 107 | + if self.running(): |
| 108 | + self.stop() |
| 109 | + # Really, using rm -rf would be the best, but use this for portability. |
| 110 | + for root, dirs, files in os.walk(self.control.data, topdown = False): |
| 111 | + for name in files: |
| 112 | + os.remove(os.path.join(root, name)) |
| 113 | + for name in dirs: |
| 114 | + os.rmdir(os.path.join(root, name)) |
| 115 | + os.rmdir(self.control.data) |
| 116 | + |
| 117 | + def start(self): |
| 118 | + """ |
| 119 | + Start the cluster |
| 120 | + """ |
| 121 | +## |
| 122 | +# vim: ts=3:sw=3:noet: |
0 commit comments