Show
Ignore:
Timestamp:
08/09/07 06:49:21 (3 years ago)
Author:
dokai
Message:

Added support for setting the PostgreSQL port by running:
./bin/python init_postgresql.py --port=[port_number]

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • buildout/pcl.buildout/trunk/init_postgresql.py

    r795 r818  
    99import shutil 
    1010 
     11from optparse import OptionParser 
     12 
    1113container = os.getcwd() 
    1214PGDATA = os.path.join(container, 'var', 'postgresql') 
    1315PGROOT = os.path.join(container, 'parts', 'postgresql') 
    14 PGPORT = 5432 
    1516PGDBNAME = 'pcl_test' 
    1617POSTGIS = os.path.join(container, 'parts', 'postgis') 
     
    1819 
    1920wrapper_script = os.path.join(container, 'bin', 'pg_ctl') 
     21wrapper_script_tmpl = """\ 
     22#!/bin/sh 
     23PGDATA=%s %s/bin/pg_ctl $@ 
     24"""  
     25 
     26test_env_tmpl = """\ 
     27[test-environment] 
     28PCLCORE_DSN = host=localhost dbname=pcl_test port=%(port)s 
     29""" 
    2030 
    2131def system(cmd): 
     
    2333        raise RuntimeError('Error running command: %s' % cmd) 
    2434 
    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) 
     35def 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) 
    3144 
    32 # Clean up existing parts 
    33 if os.path.exists(PGDATA): 
    34     shutil.rmtree(PGDATA) 
     45    # Clean up existing parts 
     46    if os.path.exists(PGDATA): 
     47        shutil.rmtree(PGDATA) 
    3548 
    36 assert not os.path.exists(PGDATA) 
    37 os.makedirs(PGDATA) 
     49    assert not os.path.exists(PGDATA) 
     50    os.makedirs(PGDATA) 
    3851 
    39 # Initialize the database 
    40 system('%s/bin/initdb --auth=trust -D %s' % (PGROOT, PGDATA)) 
     52    # Initialize the database 
     53    system('%s/bin/initdb --auth=trust -D %s' % (PGROOT, PGDATA)) 
    4154 
    42 # Create a wrapper script for pg_ctl 
    43 wrapper = open(wrapper_script, 'w') 
    44 wrapper.write("""\ 
    45 #!/bin/sh 
    46 PGDATA=%s %s/bin/pg_ctl $@ 
    47 """ % (PGDATA, PGROOT)) 
    48 wrapper.close() 
    49 os.chmod(wrapper_script, stat.S_IRWXU) 
     55    # Create a wrapper script for pg_ctl 
     56    wrapper = open(wrapper_script, 'w') 
     57    wrapper.write(wrapper_script_tmpl % (PGDATA, PGROOT)) 
     58    wrapper.close() 
     59    os.chmod(wrapper_script, stat.S_IRWXU) 
    5060 
    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 
     98def 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'  
    53112 
    54113 
    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)) 
     114if __name__ == '__main__': 
     115    main() 
    69116 
    70117 
     118