17 |
import popen2 |
import popen2 |
18 |
import shutil |
import shutil |
19 |
import traceback |
import traceback |
20 |
|
import re |
21 |
|
|
22 |
import support |
import support |
23 |
|
|
185 |
raise RuntimeError("postmaster didn't start") |
raise RuntimeError("postmaster didn't start") |
186 |
|
|
187 |
def is_running(self): |
def is_running(self): |
188 |
"""Return true a postmaster process is running on self.dbdir |
"""Return whether a postmaster process is running on self.dbdir |
189 |
|
|
190 |
This method runs pg_ctl status on the dbdir so even if the |
This method runs pg_ctl status on the dbdir and returns True if |
191 |
object has just been created it is possible that this method |
that command succeeds and False otherwise. |
192 |
returns true if there's still a postmaster process running for |
|
193 |
self.dbdir. |
Note that it is possible that this method returns true even if |
194 |
|
the PostgreSQLServer instance has just been created and |
195 |
|
createdb() has not been called yet. This can happen, for |
196 |
|
instance, if the server has been started manually for debugging |
197 |
|
purposes after a test suite run. |
198 |
""" |
""" |
199 |
return run_boolean_command(["pg_ctl", "-D", self.dbdir, "status"]) |
return run_boolean_command(["pg_ctl", "-D", self.dbdir, "status"]) |
200 |
|
|
304 |
return " ".join(params) |
return " ".join(params) |
305 |
|
|
306 |
def execute_sql(self, dbname, user, sql): |
def execute_sql(self, dbname, user, sql): |
307 |
"""Execute the sql statament |
"""Execute the sql statament and return a result for SELECT statements |
308 |
|
|
309 |
The user parameter us used as in connection_params. The dbname |
The user parameter us used as in connection_params. The dbname |
310 |
parameter must be the name of a database in the cluster. |
parameter must be the name of a database in the cluster. The |
311 |
|
sql parameter is the SQL statement to execute as a string. If |
312 |
|
the string starts with 'select' (matched case insensitively) the |
313 |
|
first row of the result will be returned. Otherwise the return |
314 |
|
value is None. |
315 |
""" |
""" |
316 |
conn = psycopg.connect("dbname=%s " % dbname |
conn = psycopg.connect("dbname=%s " % dbname |
317 |
+ self.connection_string(user)) |
+ self.connection_string(user)) |
318 |
cursor = conn.cursor() |
cursor = conn.cursor() |
319 |
cursor.execute(sql) |
cursor.execute(sql) |
320 |
|
if sql.lower().startswith("select"): |
321 |
|
row = cursor.fetchone() |
322 |
|
else: |
323 |
|
row = None |
324 |
conn.commit() |
conn.commit() |
325 |
conn.close() |
conn.close() |
326 |
|
return row |
327 |
|
|
328 |
|
def server_version(self): |
329 |
|
"""Return the server version as a tuple (major, minor, patch) |
330 |
|
|
331 |
|
Each item in the tuple is an int. |
332 |
|
""" |
333 |
|
result = self.execute_sql("template1", "admin", "SELECT version();")[0] |
334 |
|
match = re.match(r"PostgreSQL (\d+\.\d+\.\d+)", result) |
335 |
|
if match: |
336 |
|
return tuple(map(int, match.group(1).split("."))) |
337 |
|
else: |
338 |
|
raise RutimeError("Cannot determine PostgreSQL server version" |
339 |
|
" from %r" % result) |
340 |
|
|
341 |
def require_authentication(self, required): |
def require_authentication(self, required): |
342 |
"""Switch authentication requirements on or off |
"""Switch authentication requirements on or off |
348 |
corresponding call to switch it off again in the test case' |
corresponding call to switch it off again in the test case' |
349 |
tearDown method or in a finally: block. |
tearDown method or in a finally: block. |
350 |
""" |
""" |
351 |
|
# Starting with PostgreSQL 7.3 the pg_hba.conf file has an |
352 |
|
# additional column with a username. Query the server version |
353 |
|
# and generate a file in the correct format. |
354 |
|
if self.server_version() >= (7, 3): |
355 |
|
user = "all" |
356 |
|
else: |
357 |
|
user = "" |
358 |
if required: |
if required: |
359 |
contents = "local all password\n" |
contents = "local all %s password\n" % user |
360 |
else: |
else: |
361 |
contents = "local all trust\n" |
contents = "local all %s trust\n" % user |
362 |
f = open(os.path.join(self.dbdir, "pg_hba.conf"), "w") |
f = open(os.path.join(self.dbdir, "pg_hba.conf"), "w") |
363 |
f.write(contents) |
f.write(contents) |
364 |
f.close() |
f.close() |
495 |
"""Return the name of the postgis_sql file |
"""Return the name of the postgis_sql file |
496 |
|
|
497 |
A postgis installation usually has the postgis_sql file in |
A postgis installation usually has the postgis_sql file in |
498 |
PostgreSQL's datadir (i.e. the directory where PostgreSQL keeps |
PostgreSQL's $datadir (i.e. the directory where PostgreSQL keeps |
499 |
static files, not the directory containing the databases). |
static files, not the directory containing the databases). |
500 |
Unfortunately there's no way to determine the name of this directory |
Unfortunately there's no way to determine the name of this directory |
501 |
with pg_config so we assume here that it's |
with pg_config so we assume here that it's |
502 |
$bindir/../share/postgresql/. |
$bindir/../share/postgresql/. |
503 |
|
|
504 |
|
Furthermore, different versions of postgis place the file in |
505 |
|
slightly different locations. For instance: |
506 |
|
|
507 |
|
postgis 0.7.5 $datadir/contrib/postgis.sql |
508 |
|
postgis 0.8.1 $datadir/postgis.sql |
509 |
|
|
510 |
|
To support both versions, we look in both places and return the |
511 |
|
first one found (looking under contrib first). If the file is not |
512 |
|
found the return value is None. |
513 |
""" |
""" |
514 |
bindir = run_config_script("pg_config --bindir").strip() |
bindir = run_config_script("pg_config --bindir").strip() |
515 |
return os.path.join(bindir, "..", "share", "postgresql", |
datadir = os.path.join(bindir, "..", "share", "postgresql") |
516 |
"contrib", "postgis.sql") |
for filename in [os.path.join(datadir, "contrib", "postgis.sql"), |
517 |
|
os.path.join(datadir, "postgis.sql")]: |
518 |
|
if os.path.exists(filename): |
519 |
|
return filename |
520 |
|
|
521 |
|
|
522 |
_postgres_server = None |
_postgres_server = None |
523 |
def get_test_server(): |
def get_test_server(): |