|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Receives one parameter - PGDATA directory |
| 4 | +Creates test base using first initdb found in the PATH |
| 5 | +Searches contrib directory for additional directives to |
| 6 | +put into postgresql conf. |
| 7 | +
|
| 8 | +On success, starts database |
| 9 | +""" |
| 10 | +importos,sys,subprocess,glob,re,os.path,time |
| 11 | + |
| 12 | +iflen(sys.argv)!=2: |
| 13 | +print>>sys.stderr,"Usage %s data-directory"%sys.argv[0] |
| 14 | +sys.exit(1) |
| 15 | + |
| 16 | +datadir=sys.argv[1] |
| 17 | + |
| 18 | +ifos.access(datadir,os.R_OK): |
| 19 | +importshutil |
| 20 | +shutil.rmtree(datadir) |
| 21 | +os.mkdir(datadir) |
| 22 | +env=os.environ.copy() |
| 23 | +env["LANG"]="C" |
| 24 | +withopen("initdb.log","w")asf: |
| 25 | +exitcode=subprocess.call(["initdb","-E","UTF8",datadir],env=env,stdout=f,stderr=subprocess.STDOUT) |
| 26 | +ifexitcode: |
| 27 | +sys.exit(exitcode) |
| 28 | +# Collect extra config option |
| 29 | +addopts={} |
| 30 | +formoduleinglob.glob("contrib/*"): |
| 31 | +ifnotos.path.isdir(module): |
| 32 | +continue |
| 33 | +ifnotos.access(module+"/Makefile",os.R_OK): |
| 34 | +continue |
| 35 | +withopen(module+"/Makefile","r")asmakefile: |
| 36 | +var={"top_srcdir":os.getcwd()} |
| 37 | +forlineinmakefile: |
| 38 | +m=re.match("\s*(\w+)\s*=\s*(.*)",line) |
| 39 | +ifm: |
| 40 | +var[m.group(1)]=m.group(2) |
| 41 | +if"EXTRA_REGRESS_OPTS"invar: |
| 42 | +m=re.search("--temp-config=(\S+)",var["EXTRA_REGRESS_OPTS"]) |
| 43 | +ifm: |
| 44 | +filename=re.sub("\\$[{(](\w+)[})]",lambdam:var[m.group(1)],m.group(1)) |
| 45 | +withopen(filename,"r")asconfig: |
| 46 | +forlineinconfig: |
| 47 | +m=re.match("(\w\S*\w)\s*=\s*(\S.*)\s*",line) |
| 48 | +ifm: |
| 49 | +opt=m.group(1) |
| 50 | +value=m.group(2) |
| 51 | +ifoptinaddopts: |
| 52 | +ifvalue[0]=="'": |
| 53 | +addopts[opt]=addopts[opt][:-1]+", "+value[1:] |
| 54 | +else: |
| 55 | +addopts[opt]+=", ".value |
| 56 | +else: |
| 57 | +addopts[opt]=value |
| 58 | + |
| 59 | +ifaddopts: |
| 60 | +withopen(datadir+"/postgresql.conf","a")asf: |
| 61 | +foropt,valueinaddopts.items(): |
| 62 | +print>>f,"%s=%s"%(opt,value) |
| 63 | +withopen("initdb.log","a")asf: |
| 64 | +exitcode=subprocess.call(["pg_ctl","start","-D",datadir,"-l",datadir+"/postmaster.log"],env=env,stdout=f,stderr=subprocess.STDOUT) |
| 65 | +ifexitcode: |
| 66 | +sys.exit(exitcode) |
| 67 | + |
| 68 | +failtime=time.time()+60 |
| 69 | +print"Waiting for database to start" |
| 70 | +whiletime.time()<failtime: |
| 71 | +exitcode=subprocess.call(["psql","postgres","-c","select version()"],stderr=subprocess.STDOUT) |
| 72 | +ifexitcode==0: |
| 73 | +sys.exit(0) |
| 74 | +print>>sys.stderr,"Database havent't started in 60 seconds" |
| 75 | +sys.exit(1) |
| 76 | + |
| 77 | + |