Skip to content

Commit 9936bd5

Browse files
author
James William Pye
committed
Relocate command interface code into the bin package.
1 parent e311a26 commit 9936bd5

10 files changed

Lines changed: 210 additions & 175 deletions

File tree

postgresql/bin/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Console-script collection package.
3+
4+
Contents:
5+
6+
pg_python
7+
Python console with a PostgreSQL connection bound to `db`.
8+
9+
pg_withcluster
10+
Execute an arbitrary command in the context of a new cluster configured
11+
specifically for small tests and low resources.
12+
13+
pg_dotconf
14+
Modify a PostgreSQL configuration file.
15+
16+
pg_tin
17+
Manage and use a set of test clusters.
18+
"""

postgresql/bin/pg_dotconf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
import sys
3-
from postgresql.configfile import pg_dotconf
4-
sys.exit(pg_dotconf(sys.argv))
3+
import postgresql.bin.pg_dotconf as cmd
4+
sys.exit(cmd.pg_dotconf(sys.argv))

postgresql/bin/pg_dotconf.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
import sys
3+
from optparse import OptionParser
4+
from postgresql.configfile import pg_cf
5+
6+
__all__ = ['pg_dotconf']
7+
8+
def command(args):
9+
"""
10+
pg_dotconf script entry point.
11+
"""
12+
op = OptionParser(
13+
"%prog [--stdout] [-f settings] config_file_src [param=val]+",
14+
version = '0'
15+
)
16+
op.add_option(
17+
'-f', '--file',
18+
dest = 'settings',
19+
help = 'The file of settings to push; parameter arguments may override',
20+
default = [],
21+
action = 'append',
22+
)
23+
op.add_option(
24+
'--stdout',
25+
dest = 'stdout',
26+
help = 'Redirect the changed file to standard output',
27+
action = 'store_true',
28+
default = False
29+
)
30+
co, ca = op.parse_args(args[1:])
31+
if not ca:
32+
return 0
33+
34+
settings = {}
35+
for sfp in co.settings:
36+
with open(sfp) as sf:
37+
for line in sf:
38+
pl = pg_cf.parse_line(line)
39+
if pl is not None:
40+
if comment not in line[pl[0].start]:
41+
settings[line[pl[0]]] = unquote(line[pl[1]])
42+
43+
for p in ca[1:]:
44+
if p.startswith('#'):
45+
k = p[1:]
46+
v = None
47+
elif '=' not in p:
48+
sys.stderr.write(
49+
"ERROR: setting parameter, %r, does not have '=' " \
50+
"to separate setting name from setting value"
51+
)
52+
return 1
53+
else:
54+
k, v = p.split('=', 1)
55+
settings[k] = v
56+
57+
fp = ca[0]
58+
with open(fp, 'r') as fr:
59+
lines = pg_cf.alter_config(settings, fr)
60+
61+
if co.stdout or fp == '/dev/stdin':
62+
for l in lines:
63+
sys.stdout.write(l)
64+
else:
65+
with open(fp, 'w') as fw:
66+
for l in lines:
67+
fw.write(l)
68+
return 0
69+
70+
if __name__ == '__main__':
71+
sys.exit(command(sys.argv))

postgresql/bin/pg_python

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/usr/bin/env python
2-
from postgresql.driver.pythoncommand import command
3-
command()
2+
import sys
3+
import postgresql.bin.pg_python as cmd
4+
sys.exit(cmd.command(sys.argv))
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# http://python.projects.postgresql.org
44
##
55
"""
6-
Python command with a postgresql.driver.pgapi connection.
6+
Python command with a postgresql.driver.pq3 connection.
77
"""
88
import os
99
import sys
@@ -14,7 +14,7 @@
1414
from .. import clientparameters
1515
from ..resolved import pythoncommand as pycmd
1616

17-
from . import default as pg_driver
17+
from ..driver import default as pg_driver
1818

1919
pq_trace = optparse.make_option(
2020
'--pq-trace',
@@ -110,6 +110,6 @@ def command(args = sys.argv):
110110
return rv
111111

112112
if __name__ == '__main__':
113-
sys.exit(command())
113+
sys.exit(command(sys.argv))
114114
##
115115
# vim: ts=3:sw=3:noet:

postgresql/bin/pg_tin

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
import sys
3-
from postgresql.tin import command
4-
sys.exit(command(sys.argv))
3+
import postgresql.bin.pg_tin as cmd
4+
sys.exit(cmd.command(sys.argv))
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
# http://python.projects.postgresql.org
44
##
55
"""
6-
tin provides a means to manage a set of test clusters.
6+
pg_tin provides a means to manage a set of test clusters.
7+
8+
This script is used to manage the set of test clusters normally managed in
9+
"~/.pg_tin". It's purpose is to provide simple and quick access to a PostgreSQL
10+
database of a given version or configuration.
711
"""
812
import sys
913
import os
@@ -13,9 +17,8 @@
1317
import warnings
1418
from random import random
1519

16-
from . import versionstring as pg_version
17-
from . import pg_config
18-
from .cluster import Cluster
20+
from .. import versionstring as pg_version
21+
from ..cluster import Cluster
1922

2023
from gettext import gettext as _
2124

@@ -216,7 +219,7 @@ def exit_message(c, r):
216219
def exit_message(c, r):
217220
pass
218221

219-
def tin(args):
222+
def command(args):
220223
if os.environ.has_key('PGTINCUP'):
221224
DEFAULT_TINCUP = os.environ['PGTINCUP']
222225
elif os.environ.has_key('HOME'):
@@ -459,7 +462,7 @@ def tin(args):
459462
for k in p:
460463
sys.stdout.write('%s=%s\n' %(k, p[k]))
461464
ca = ()
462-
elif com in (_('readlogs'),):
465+
elif com in ('readlogs',):
463466
pager = os.environ.get('PAGER', 'less')
464467
if pager is None:
465468
sys.stderr.write(
@@ -489,4 +492,4 @@ def tin(args):
489492
sys.exit(1)
490493

491494
if __name__ == '__main__':
492-
tin(sys.argv)
495+
command(sys.argv)

postgresql/bin/pg_withcluster

Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,4 @@
11
#!/usr/bin/env python
2-
"""
3-
Entry point for pg_withcluster.
4-
"""
5-
import tempfile
6-
import subprocess as sp
7-
import getpass
8-
from optparse import OptionParser
9-
from postgresql.cluster import Cluster
10-
from postgresql.installation import Installation
11-
12-
def main(args = sys.argv):
13-
op = OptionParser(
14-
"%prog [-p] [--version] {[setting=value], ...} [pg_config] command",
15-
version = '1.0'
16-
)
17-
op.allow_interspersed_args = False
18-
op.add_option(
19-
'-l',
20-
dest = 'logfile',
21-
help = 'logfile for the PostgreSQL daemon',
22-
default = '/dev/stderr',
23-
)
24-
op.add_option(
25-
'-p',
26-
dest = 'look_in_path',
27-
help = 'Look for "pg_config" in the PATH environment',
28-
action = 'store_true',
29-
default = False
30-
)
31-
op.add_option(
32-
'-N',
33-
dest = 'max_connections',
34-
help = 'Number',
35-
type = 'int',
36-
default = 4,
37-
)
38-
co, ca = op.parse_args(args[1:])
39-
if not ca:
40-
sys.stderr.write("no command given to run in cluster context")
41-
sys.exit(1)
42-
43-
max_con = co.max_connections
44-
if max_con <= 0:
45-
max_con = 4
46-
i = 0
47-
for x in ca:
48-
if '=' not in x:
49-
break
50-
i += 1
51-
settings = ca[0:i]
52-
command = ca[i:]
53-
d = tempfile.mkdtemp()
54-
try:
55-
c = Cluster(d, inn)
56-
c.init(
57-
user = getpass.getuser(),
58-
)
59-
c.settings.update([
60-
x.split('=', 1) for x in settings
61-
])
62-
c.settings.update({
63-
'port' : '14327',
64-
'listen_addresses' : 'localhost',
65-
'max_connections' : str(co.max_connections),
66-
'shared_buffers' : str((co.max_connections * 2) + 50),
67-
})
68-
c.start(logfile = sys.stderr)
69-
c.wait_until_started()
70-
with c.connect() as con:
71-
pass
72-
try:
73-
p = sp.Popen(
74-
command,
75-
env = {
76-
'PGUSER' : getpass.getuser(),
77-
'PGHOST' : 'localhost',
78-
'PGPORT' : c.settings['port'],
79-
'PGDATABASE' : getpass.getuser(),
80-
'PGINSTALLATION' : c.installation.pg_config_path,
81-
'PGDATA' : d,
82-
'PG_WITHCLUSTER' : '1',
83-
},
84-
)
85-
while True:
86-
try:
87-
return p.wait()
88-
except OSError as e:
89-
if e.errno != errno.ESRCH:
90-
raise
91-
# sigh.
92-
finally:
93-
c.stop()
94-
finally:
95-
c.drop()
96-
97-
if __name__ == '__main__':
98-
sys.exit(main())
2+
import sys
3+
import postgresql.bin.pg_withcluster as cmd
4+
sys.exit(cmd.command(sys.argv))

postgresql/bin/pg_withcluster.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
"""
3+
Entry point for pg_withcluster.
4+
"""
5+
import tempfile
6+
import subprocess as sp
7+
import getpass
8+
from optparse import OptionParser
9+
from postgresql.cluster import Cluster
10+
from postgresql.installation import Installation
11+
12+
def main(args = sys.argv):
13+
op = OptionParser(
14+
"%prog [-p] [--version] {[setting=value], ...} [pg_config] command",
15+
version = '1.0'
16+
)
17+
op.allow_interspersed_args = False
18+
op.add_option(
19+
'-l',
20+
dest = 'logfile',
21+
help = 'logfile for the PostgreSQL daemon',
22+
default = '/dev/stderr',
23+
)
24+
op.add_option(
25+
'-p',
26+
dest = 'look_in_path',
27+
help = 'Look for "pg_config" in the PATH environment',
28+
action = 'store_true',
29+
default = False
30+
)
31+
op.add_option(
32+
'-N',
33+
dest = 'max_connections',
34+
help = 'Number',
35+
type = 'int',
36+
default = 4,
37+
)
38+
co, ca = op.parse_args(args[1:])
39+
if not ca:
40+
sys.stderr.write("no command given to run in cluster context")
41+
sys.exit(1)
42+
43+
max_con = co.max_connections
44+
if max_con <= 0:
45+
max_con = 4
46+
i = 0
47+
for x in ca:
48+
if '=' not in x:
49+
break
50+
i += 1
51+
settings = ca[0:i]
52+
command = ca[i:]
53+
d = tempfile.mkdtemp()
54+
try:
55+
c = Cluster(d, inn)
56+
c.init(
57+
user = getpass.getuser(),
58+
)
59+
c.settings.update([
60+
x.split('=', 1) for x in settings
61+
])
62+
c.settings.update({
63+
'port' : '14327',
64+
'listen_addresses' : 'localhost',
65+
'max_connections' : str(co.max_connections),
66+
'shared_buffers' : str((co.max_connections * 2) + 50),
67+
})
68+
c.start(logfile = sys.stderr)
69+
c.wait_until_started()
70+
with c.connect() as con:
71+
pass
72+
try:
73+
p = sp.Popen(
74+
command,
75+
env = {
76+
'PGUSER' : getpass.getuser(),
77+
'PGHOST' : 'localhost',
78+
'PGPORT' : c.settings['port'],
79+
'PGDATABASE' : getpass.getuser(),
80+
'PGINSTALLATION' : c.installation.pg_config_path,
81+
'PGDATA' : d,
82+
'PG_WITHCLUSTER' : '1',
83+
},
84+
)
85+
while True:
86+
try:
87+
return p.wait()
88+
except OSError as e:
89+
if e.errno != errno.ESRCH:
90+
raise
91+
# sigh.
92+
finally:
93+
c.stop()
94+
finally:
95+
c.drop()
96+
97+
if __name__ == '__main__':
98+
sys.exit(main())

0 commit comments

Comments
 (0)