115 |
self.socket_dir = socket_dir |
self.socket_dir = socket_dir |
116 |
|
|
117 |
# For the client side the socket directory can be used as the |
# For the client side the socket directory can be used as the |
118 |
# host the name starts with a slash. |
# host if the name starts with a slash. |
119 |
self.host = os.path.abspath(socket_dir) |
self.host = os.path.abspath(socket_dir) |
120 |
|
|
121 |
|
# name and password for the admin and an unprivileged user |
122 |
|
self.admin_name = "postgres" |
123 |
|
self.admin_password = "postgres" |
124 |
|
self.user_name = "observer" |
125 |
|
self.user_password = "telescope" |
126 |
|
|
127 |
# Map db names to db objects |
# Map db names to db objects |
128 |
self.known_dbs = {} |
self.known_dbs = {} |
129 |
|
|
141 |
shutil.rmtree(self.dbdir) |
shutil.rmtree(self.dbdir) |
142 |
os.mkdir(self.dbdir) |
os.mkdir(self.dbdir) |
143 |
|
|
144 |
run_command(["initdb", self.dbdir], |
run_command(["initdb", "-D", self.dbdir, "-U", self.admin_name], |
145 |
os.path.join(self.dbdir, "initdb.log")) |
os.path.join(self.dbdir, "initdb.log")) |
146 |
|
|
147 |
extra_opts = "-p %d" % self.port |
extra_opts = "-p %d" % self.port |
156 |
# server ourselves |
# server ourselves |
157 |
self.wait_for_postmaster() |
self.wait_for_postmaster() |
158 |
|
|
159 |
|
self.alter_user(self.admin_name, self.admin_password) |
160 |
|
self.create_user(self.user_name, self.user_password) |
161 |
|
|
162 |
def wait_for_postmaster(self): |
def wait_for_postmaster(self): |
163 |
"""Return when the database server is running |
"""Return when the database server is running |
164 |
|
|
170 |
while count < max_count: |
while count < max_count: |
171 |
try: |
try: |
172 |
run_command(["psql", "-l", "-p", str(self.port), |
run_command(["psql", "-l", "-p", str(self.port), |
173 |
"-h", self.host], |
"-h", self.host, "-U", self.admin_name], |
174 |
os.path.join(self.dbdir, "psql-%d.log" % count)) |
os.path.join(self.dbdir, "psql-%d.log" % count)) |
175 |
except: |
except RuntimeError: |
176 |
pass |
pass |
177 |
|
except: |
178 |
|
traceback.print_exc() |
179 |
else: |
else: |
180 |
break |
break |
181 |
time.sleep(0.5) |
time.sleep(0.5) |
237 |
"roads-line.shp"))] |
"roads-line.shp"))] |
238 |
return self.get_static_data_db(dbname, tables) |
return self.get_static_data_db(dbname, tables) |
239 |
|
|
240 |
|
def connection_params(self, user): |
241 |
|
"""Return the connection parameters for the given user |
242 |
|
|
243 |
|
The return value is a dictionary suitable as keyword argument |
244 |
|
list to PostGISConnection. The user parameter may be either |
245 |
|
'admin' to connect as admin or 'user' to connect as an |
246 |
|
unprivileged user. |
247 |
|
""" |
248 |
|
return {"host": self.host, "port": self.port, |
249 |
|
"user": getattr(self, user + "_name"), |
250 |
|
"password": getattr(self, user + "_password")} |
251 |
|
|
252 |
|
def connection_string(self, user): |
253 |
|
"""Return (part of) the connection string to pass to psycopg.connect |
254 |
|
|
255 |
|
The string contains host, port, user and password. The user |
256 |
|
parameter must be either 'admin' or 'user', as for |
257 |
|
connection_params. |
258 |
|
""" |
259 |
|
params = [] |
260 |
|
for key, value in self.connection_params(user).items(): |
261 |
|
# FIXME: this doesn't do quiting correctly but that |
262 |
|
# shouldn't be much of a problem (people shouldn't be using |
263 |
|
# single quotes in filenames anyway :) ) |
264 |
|
params.append("%s='%s'" % (key, value)) |
265 |
|
return " ".join(params) |
266 |
|
|
267 |
|
def execute_sql(self, dbname, user, sql): |
268 |
|
"""Execute the sql statament |
269 |
|
|
270 |
|
The user parameter us used as in connection_params. The dbname |
271 |
|
parameter must be the name of a database in the cluster. |
272 |
|
""" |
273 |
|
conn = psycopg.connect("dbname=%s " % dbname |
274 |
|
+ self.connection_string(user)) |
275 |
|
cursor = conn.cursor() |
276 |
|
cursor.execute(sql) |
277 |
|
conn.commit() |
278 |
|
conn.close() |
279 |
|
|
280 |
|
def require_authentication(self, required): |
281 |
|
"""Switch authentication requirements on or off |
282 |
|
|
283 |
|
When started for the first time no passwords are required. Some |
284 |
|
tests want to explicitly test whether Thuban's password |
285 |
|
infrastructure works and switch password authentication on |
286 |
|
explicitly. When switching it on, there should be a |
287 |
|
corresponding call to switch it off again in the test case' |
288 |
|
tearDown method or in a finally: block. |
289 |
|
""" |
290 |
|
if required: |
291 |
|
contents = "local all password\n" |
292 |
|
else: |
293 |
|
contents = "local all trust\n" |
294 |
|
f = open(os.path.join(self.dbdir, "pg_hba.conf"), "w") |
295 |
|
f.write(contents) |
296 |
|
f.close() |
297 |
|
run_command(["pg_ctl", "-D", self.dbdir, "reload"], |
298 |
|
os.path.join(self.dbdir, "pg_ctl-reload.log")) |
299 |
|
|
300 |
|
|
301 |
|
def create_user(self, username, password): |
302 |
|
"""Create user username with password in the database""" |
303 |
|
self.execute_sql("template1", "admin", |
304 |
|
"CREATE USER %s PASSWORD '%s';" % (username,password)) |
305 |
|
|
306 |
|
def alter_user(self, username, password): |
307 |
|
"""Change the user username's password in the database""" |
308 |
|
self.execute_sql("template1", "admin", |
309 |
|
"ALTER USER %s PASSWORD '%s';" % (username,password)) |
310 |
|
|
311 |
|
|
312 |
class PostGISDatabase: |
class PostGISDatabase: |
323 |
"""Remove the old db directory and create and initialize a new database |
"""Remove the old db directory and create and initialize a new database |
324 |
""" |
""" |
325 |
run_command(["createdb", "-p", str(self.server.port), |
run_command(["createdb", "-p", str(self.server.port), |
326 |
"-h", self.server.host, self.dbname], |
"-h", self.server.host, "-U", self.server.admin_name, |
327 |
|
self.dbname], |
328 |
os.path.join(self.server.dbdir, "createdb.log")) |
os.path.join(self.server.dbdir, "createdb.log")) |
329 |
run_command(["createlang", "-p", str(self.server.port), |
run_command(["createlang", "-p", str(self.server.port), |
330 |
"-h", self.server.host, "plpgsql", self.dbname], |
"-h", self.server.host, "-U", self.server.admin_name, |
331 |
|
"plpgsql", self.dbname], |
332 |
os.path.join(self.server.dbdir, "createlang.log")) |
os.path.join(self.server.dbdir, "createlang.log")) |
333 |
# for some reason psql doesn't exit with an error code if the |
# for some reason psql doesn't exit with an error code if the |
334 |
# file given as -f doesn't exist, so we check manually by trying |
# file given as -f doesn't exist, so we check manually by trying |
337 |
f.close() |
f.close() |
338 |
del f |
del f |
339 |
run_command(["psql", "-f", self.postgis_sql, "-d", self.dbname, |
run_command(["psql", "-f", self.postgis_sql, "-d", self.dbname, |
340 |
"-p", str(self.server.port), "-h", self.server.host], |
"-p", str(self.server.port), "-h", self.server.host, |
341 |
|
"-U", self.server.admin_name], |
342 |
os.path.join(self.server.dbdir, "psql.log")) |
os.path.join(self.server.dbdir, "psql.log")) |
343 |
|
|
344 |
|
self.server.execute_sql(self.dbname, "admin", |
345 |
|
"GRANT SELECT ON geometry_columns TO PUBLIC;") |
346 |
|
|
347 |
if self.tables is not None: |
if self.tables is not None: |
348 |
for tablename, shapefile in self.tables: |
for tablename, shapefile in self.tables: |
349 |
upload_shapefile(shapefile, self, tablename) |
upload_shapefile(shapefile, self, tablename) |
464 |
|
|
465 |
server = db.server |
server = db.server |
466 |
dbname = db.dbname |
dbname = db.dbname |
467 |
conn = psycopg.connect("host=%s port=%s dbname=%s" |
conn = psycopg.connect("dbname=%s " % dbname |
468 |
% (server.host, server.port, dbname)) |
+ db.server.connection_string("admin")) |
469 |
cursor = conn.cursor() |
cursor = conn.cursor() |
470 |
|
|
471 |
shp = shapelib.ShapeFile(filename) |
shp = shapelib.ShapeFile(filename) |
515 |
#print insert % data |
#print insert % data |
516 |
cursor.execute(insert, data) |
cursor.execute(insert, data) |
517 |
|
|
518 |
|
cursor.execute("GRANT SELECT ON %s TO PUBLIC;" % tablename) |
519 |
|
|
520 |
conn.commit() |
conn.commit() |