184 |
raise RuntimeError("postmaster didn't start") |
raise RuntimeError("postmaster didn't start") |
185 |
|
|
186 |
def is_running(self): |
def is_running(self): |
187 |
"""Return true a postmaster process is running on self.dbdir |
"""Return whether a postmaster process is running on self.dbdir |
188 |
|
|
189 |
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 |
190 |
object has just been created it is possible that this method |
that command succeeds and False otherwise. |
191 |
returns true if there's still a postmaster process running for |
|
192 |
self.dbdir. |
Note that it is possible that this method returns true even if |
193 |
|
the PostgreSQLServer instance has just been created and |
194 |
|
createdb() has not been called yet. This can happen, for |
195 |
|
instance, if the server has been started manually for debugging |
196 |
|
purposes after a test suite run. |
197 |
""" |
""" |
198 |
return run_boolean_command(["pg_ctl", "-D", self.dbdir, "status"]) |
return run_boolean_command(["pg_ctl", "-D", self.dbdir, "status"]) |
199 |
|
|
202 |
run_command(["pg_ctl", "-m", "fast", "-D", self.dbdir, "stop"], |
run_command(["pg_ctl", "-m", "fast", "-D", self.dbdir, "stop"], |
203 |
os.path.join(self.dbdir, "pg_ctl-stop.log")) |
os.path.join(self.dbdir, "pg_ctl-stop.log")) |
204 |
|
|
205 |
def new_postgis_db(self, dbname, tables = None, reference_systems = None): |
def new_postgis_db(self, dbname, tables = None, reference_systems = None, |
206 |
|
views = None): |
207 |
"""Create and return a new PostGISDatabase object using self as server |
"""Create and return a new PostGISDatabase object using self as server |
208 |
""" |
""" |
209 |
db = PostGISDatabase(self, self.postgis_sql, dbname, tables = tables, |
db = PostGISDatabase(self, self.postgis_sql, dbname, tables = tables, |
210 |
reference_systems = reference_systems) |
reference_systems = reference_systems, |
211 |
|
views = views) |
212 |
db.initdb() |
db.initdb() |
213 |
self.known_dbs[dbname] = db |
self.known_dbs[dbname] = db |
214 |
return db |
return db |
215 |
|
|
216 |
def get_static_data_db(self, dbname, tables, reference_systems): |
def get_static_data_db(self, dbname, tables, reference_systems, views): |
217 |
"""Return a PostGISDatabase for a database with the given static data |
"""Return a PostGISDatabase for a database with the given static data |
218 |
|
|
219 |
If no databasse of the name dbname exists, create a new one via |
If no databasse of the name dbname exists, create a new one via |
231 |
""" |
""" |
232 |
db = self.known_dbs.get(dbname) |
db = self.known_dbs.get(dbname) |
233 |
if db is not None: |
if db is not None: |
234 |
if db.has_data(tables, reference_systems): |
if db.has_data(tables, reference_systems, views): |
235 |
return db |
return db |
236 |
raise ValueError("PostGISDatabase named %r doesn't have tables %r" |
raise ValueError("PostGISDatabase named %r doesn't have tables %r" |
237 |
% (dbname, tables)) |
% (dbname, tables)) |
238 |
return self.new_postgis_db(dbname, tables, reference_systems) |
return self.new_postgis_db(dbname, tables, reference_systems, views) |
239 |
|
|
240 |
def get_default_static_data_db(self): |
def get_default_static_data_db(self): |
241 |
dbname = "PostGISStaticTests" |
dbname = "PostGISStaticTests" |
272 |
("srid", 1), |
("srid", 1), |
273 |
("gid_column", "point_id")]), |
("gid_column", "point_id")]), |
274 |
] |
] |
275 |
return self.get_static_data_db(dbname, tables, srids) |
views = [("v_landmarks", "SELECT * FROM landmarks_point_id")] |
276 |
|
return self.get_static_data_db(dbname, tables, srids, views) |
277 |
|
|
278 |
def connection_params(self, user): |
def connection_params(self, user): |
279 |
"""Return the connection parameters for the given user |
"""Return the connection parameters for the given user |
352 |
"""A PostGIS database in a PostgreSQLServer""" |
"""A PostGIS database in a PostgreSQLServer""" |
353 |
|
|
354 |
def __init__(self, server, postgis_sql, dbname, tables = None, |
def __init__(self, server, postgis_sql, dbname, tables = None, |
355 |
reference_systems = ()): |
reference_systems = (), views = None): |
356 |
"""Initialize the PostGISDatabase |
"""Initialize the PostGISDatabase |
357 |
|
|
358 |
Parameters: |
Parameters: |
378 |
(srid, params) pairs where srid is the srid defined by |
(srid, params) pairs where srid is the srid defined by |
379 |
the proj4 paramter string params. The srid can be given |
the proj4 paramter string params. The srid can be given |
380 |
as an extra parameter in the tables list. |
as an extra parameter in the tables list. |
381 |
|
|
382 |
|
views -- Optional description of views. If given it should |
383 |
|
be a list of (viewname, select_stmt) pairs where |
384 |
|
viewname is the name of the view to be created and |
385 |
|
select_stmt is the select statement to use as the basis. |
386 |
|
The views will be created after the tables and may refer |
387 |
|
to them in the select_stmt. |
388 |
""" |
""" |
389 |
self.server = server |
self.server = server |
390 |
self.postgis_sql = postgis_sql |
self.postgis_sql = postgis_sql |
391 |
self.dbname = dbname |
self.dbname = dbname |
392 |
self.tables = tables |
self.tables = tables |
393 |
|
self.views = views |
394 |
if reference_systems: |
if reference_systems: |
395 |
self.reference_systems = reference_systems |
self.reference_systems = reference_systems |
396 |
else: |
else: |
446 |
tablename, shapefile, kw = unpack(info) |
tablename, shapefile, kw = unpack(info) |
447 |
upload_shapefile(shapefile, self, tablename, **kw) |
upload_shapefile(shapefile, self, tablename, **kw) |
448 |
|
|
449 |
def has_data(self, tables, reference_systems): |
if self.views is not None: |
450 |
|
for viewname, select_stmt in self.views: |
451 |
|
self.server.execute_sql(self.dbname, "admin", |
452 |
|
"CREATE VIEW %s AS %s" % (viewname, |
453 |
|
select_stmt)) |
454 |
|
self.server.execute_sql(self.dbname, "admin", |
455 |
|
"GRANT SELECT ON %s TO PUBLIC;" |
456 |
|
% viewname) |
457 |
|
|
458 |
|
def has_data(self, tables, reference_systems, views): |
459 |
return (self.tables == tables |
return (self.tables == tables |
460 |
and self.reference_systems == reference_systems) |
and self.reference_systems == reference_systems |
461 |
|
and self.views == views) |
462 |
|
|
463 |
|
|
464 |
def find_postgis_sql(): |
def find_postgis_sql(): |