| 25 | | PIDFILE = '%s/postmaster.pid' % PGDATA |
| 26 | | if os.path.exists(wrapper_script) and os.path.exists(PIDFILE): |
| 27 | | print 'Shutting down PostgreSQL server...' |
| 28 | | system('%s stop' % wrapper_script) |
| 29 | | while os.path.exists(PIDFILE): |
| 30 | | time.sleep(1) |
| | 35 | def initpg(port): |
| | 36 | """Initializes a test PostgreSQL server.""" |
| | 37 | # Shut down the server if it is running |
| | 38 | PIDFILE = '%s/postmaster.pid' % PGDATA |
| | 39 | if os.path.exists(wrapper_script) and os.path.exists(PIDFILE): |
| | 40 | print 'Shutting down PostgreSQL server...' |
| | 41 | system('%s stop' % wrapper_script) |
| | 42 | while os.path.exists(PIDFILE): |
| | 43 | time.sleep(1) |
| 51 | | system('%s start' % wrapper_script) |
| 52 | | time.sleep(2) |
| | 61 | # Update the port setting and start up the server |
| | 62 | conffile = '%s/postgresql.conf' % PGDATA |
| | 63 | f = open(conffile) |
| | 64 | conf = ('port = %s' % port).join(f.read().split('#port = 5432')) |
| | 65 | f.close() |
| | 66 | open(conffile, 'w').write(conf) |
| | 67 | system('%s start' % wrapper_script) |
| | 68 | time.sleep(2) |
| | 69 | |
| | 70 | # Convert the shapefile data to SQL insert statements |
| | 71 | system('%s/bin/shp2pgsql -I -s 4269 %s postgis.world_borders > /tmp/wb.sql' % (POSTGIS, DATASET)) |
| | 72 | |
| | 73 | # Create the test database |
| | 74 | system('%s/bin/createdb --port=%s %s' % (PGROOT, port, PGDBNAME)) |
| | 75 | system('%s/bin/createlang --port=%s plpgsql %s' % (PGROOT, port, PGDBNAME)) |
| | 76 | system('%s/bin/psql -p %s -d %s -c "CREATE SCHEMA postgis"' % (PGROOT, port, PGDBNAME)) |
| | 77 | system('%s/bin/psql -p %s -d %s -f %s/share/lwpostgis.sql' % (PGROOT, port, PGDBNAME, POSTGIS)) |
| | 78 | system('%s/bin/psql -p %s -d %s -f %s/share/spatial_ref_sys.sql' % (PGROOT, port, PGDBNAME, POSTGIS)) |
| | 79 | |
| | 80 | # Import the data |
| | 81 | system('%s/bin/psql -p %s -d %s -f /tmp/wb.sql' % (PGROOT, port, PGDBNAME)) |
| | 82 | system('%s/bin/psql -p %s -d %s -c "CREATE VIEW postgis.uk_borders AS SELECT * FROM postgis.world_borders WHERE fips_cntry = \'UK\'"' % (PGROOT, port, PGDBNAME)) |
| | 83 | system('%s/bin/psql -p %s -d %s -c "INSERT INTO geometry_columns (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type) VALUES (\'\', \'postgis\', \'uk_borders\', \'the_geom\', 2, 4269, \'MULTIPOLYGON\')"' % (PGROOT, port, PGDBNAME)) |
| | 84 | system('%s/bin/psql -p %s -d %s -c "CREATE TABLE postgis.us_borders AS SELECT * FROM postgis.world_borders WHERE fips_cntry = \'US\'"' % (PGROOT, port, PGDBNAME)) |
| | 85 | system('%s/bin/psql -p %s -d %s -c "INSERT INTO geometry_columns (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type) VALUES (\'\', \'postgis\', \'us_borders\', \'the_geom\', 2, 4269, \'MULTIPOLYGON\')"' % (PGROOT, port, PGDBNAME)) |
| | 86 | |
| | 87 | os.unlink('/tmp/wb.sql') |
| | 88 | |
| | 89 | if port != '5432': |
| | 90 | # If we're not using the default port, we need to update the |
| | 91 | # test configuration and run the buildout again so that the |
| | 92 | # test runner gets updated. |
| | 93 | open('test-environment.cfg', 'w').write(test_env_tmpl % dict(port=port)) |
| | 94 | system('./bin/buildout') |
| | 95 | |
| | 96 | |
| | 97 | |
| | 98 | def main(): |
| | 99 | parser = OptionParser() |
| | 100 | parser.add_option('--port', dest='port', default='5432', |
| | 101 | help='PostgreSQL server port number') |
| | 102 | |
| | 103 | options, args = parser.parse_args() |
| | 104 | |
| | 105 | initpg(port=options.port) |
| | 106 | |
| | 107 | print '\n\n' |
| | 108 | print 'PostgreSQL test server initialized' |
| | 109 | print '----------------------------------' |
| | 110 | print 'The server is listening on localhost:%s' % options.port |
| | 111 | print 'You can control the server process by using the ./bin/pg_ctl script' |
| 55 | | # Create a database and import the world borders data set into the db |
| 56 | | # for testing |
| 57 | | system('%s/bin/shp2pgsql -I -s 4269 %s postgis.world_borders > /tmp/wb.sql' % (POSTGIS, DATASET)) |
| 58 | | system('%s/bin/createdb --port=%s %s' % (PGROOT, PGPORT, PGDBNAME)) |
| 59 | | system('%s/bin/createlang --port=%s plpgsql %s' % (PGROOT, PGPORT, PGDBNAME)) |
| 60 | | system('%s/bin/psql -p %s -d %s -c "CREATE SCHEMA postgis"' % (PGROOT, PGPORT, PGDBNAME)) |
| 61 | | system('%s/bin/psql -p %s -d %s -f %s/share/lwpostgis.sql' % (PGROOT, PGPORT, PGDBNAME, POSTGIS)) |
| 62 | | system('%s/bin/psql -p %s -d %s -f %s/share/spatial_ref_sys.sql' % (PGROOT, PGPORT, PGDBNAME, POSTGIS)) |
| 63 | | system('%s/bin/psql -p %s -d %s -f /tmp/wb.sql' % (PGROOT, PGPORT, PGDBNAME)) |
| 64 | | |
| 65 | | system('%s/bin/psql -p %s -d %s -c "CREATE VIEW postgis.uk_borders AS SELECT * FROM postgis.world_borders WHERE fips_cntry = \'UK\'"' % (PGROOT, PGPORT, PGDBNAME)) |
| 66 | | system('%s/bin/psql -p %s -d %s -c "INSERT INTO geometry_columns (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type) VALUES (\'\', \'postgis\', \'uk_borders\', \'the_geom\', 2, 4269, \'MULTIPOLYGON\')"' % (PGROOT, PGPORT, PGDBNAME)) |
| 67 | | system('%s/bin/psql -p %s -d %s -c "CREATE TABLE postgis.us_borders AS SELECT * FROM postgis.world_borders WHERE fips_cntry = \'US\'"' % (PGROOT, PGPORT, PGDBNAME)) |
| 68 | | system('%s/bin/psql -p %s -d %s -c "INSERT INTO geometry_columns (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type) VALUES (\'\', \'postgis\', \'us_borders\', \'the_geom\', 2, 4269, \'MULTIPOLYGON\')"' % (PGROOT, PGPORT, PGDBNAME)) |
| | 114 | if __name__ == '__main__': |
| | 115 | main() |