1
1
# -*- encoding: utf-8 -*-
2
2
3
+ import contextlib
3
4
import datetime
4
5
import fcntl
5
6
import glob
8
9
import logging
9
10
import operator
10
11
import os
12
+ import psycopg2
11
13
import re
12
14
import resource
13
15
import shutil
@@ -152,6 +154,18 @@ def uniq_list(l):
152
154
def fqdn ():
153
155
return socket .getfqdn ()
154
156
157
+ @contextlib .contextmanager
158
+ def local_pgadmin_cursor ():
159
+ cnx , cr = None , None
160
+ try :
161
+ cnx = psycopg2 .connect ("dbname=postgres" )
162
+ cr = cnx .cursor ()
163
+ yield cr
164
+ cnx .commit ()
165
+ finally :
166
+ if cr : cr .close ()
167
+ if cnx : cnx .close ()
168
+
155
169
#----------------------------------------------------------
156
170
# RunBot Models
157
171
#----------------------------------------------------------
@@ -797,17 +811,19 @@ def checkout(self, cr, uid, ids, context=None):
797
811
build .write ({'server_match' : server_match ,
798
812
'modules' : ',' .join (modules_to_test )})
799
813
800
- def pg_dropdb (self , cr , uid , dbname ):
801
- run (['dropdb' , dbname ])
814
+ def _local_pg_dropdb (self , cr , uid , dbname ):
815
+ with local_pgadmin_cursor () as local_cr :
816
+ local_cr .execute ('DROP DATABASE "%s" IF EXISTS' % dbname )
802
817
# cleanup filestore
803
818
datadir = appdirs .user_data_dir ()
804
819
paths = [os .path .join (datadir , pn , 'filestore' , dbname ) for pn in 'OpenERP Odoo' .split ()]
805
820
run (['rm' , '-rf' ] + paths )
806
821
807
- def pg_createdb (self , cr , uid , dbname ):
808
- self .pg_dropdb (cr , uid , dbname )
822
+ def _local_pg_createdb (self , cr , uid , dbname ):
823
+ self ._local_pg_dropdb (cr , uid , dbname )
809
824
_logger .debug ("createdb %s" , dbname )
810
- run (['createdb' , '--encoding=unicode' , '--lc-collate=C' , '--template=template0' , dbname ])
825
+ with local_pgadmin_cursor () as local_cr :
826
+ local_cr .execute ("""CREATE DATABASE "%s" TEMPLATE template0 LC_COLLATE 'C' ENCODING 'unicode'""" % dbname )
811
827
812
828
def cmd (self , cr , uid , ids , context = None ):
813
829
"""Return a list describing the command to start the build"""
@@ -906,7 +922,7 @@ def job_00_init(self, cr, uid, build, lock_path, log_path):
906
922
def job_10_test_base (self , cr , uid , build , lock_path , log_path ):
907
923
build ._log ('test_base' , 'Start test base module' )
908
924
# run base test
909
- self .pg_createdb (cr , uid , "%s-base" % build .dest )
925
+ self ._local_pg_createdb (cr , uid , "%s-base" % build .dest )
910
926
cmd , mods = build .cmd ()
911
927
if grep (build .server ("tools/config.py" ), "test-enable" ):
912
928
cmd .append ("--test-enable" )
@@ -915,7 +931,7 @@ def job_10_test_base(self, cr, uid, build, lock_path, log_path):
915
931
916
932
def job_20_test_all (self , cr , uid , build , lock_path , log_path ):
917
933
build ._log ('test_all' , 'Start test all modules' )
918
- self .pg_createdb (cr , uid , "%s-all" % build .dest )
934
+ self ._local_pg_createdb (cr , uid , "%s-all" % build .dest )
919
935
cmd , mods = build .cmd ()
920
936
if grep (build .server ("tools/config.py" ), "test-enable" ):
921
937
cmd .append ("--test-enable" )
@@ -1075,24 +1091,27 @@ def schedule(self, cr, uid, ids, context=None):
1075
1091
1076
1092
# cleanup only needed if it was not killed
1077
1093
if build .state == 'done' :
1078
- build .cleanup ()
1094
+ build ._local_cleanup ()
1079
1095
1080
1096
def skip (self , cr , uid , ids , context = None ):
1081
1097
self .write (cr , uid , ids , {'state' : 'done' , 'result' : 'skipped' }, context = context )
1082
1098
to_unduplicate = self .search (cr , uid , [('id' , 'in' , ids ), ('duplicate_id' , '!=' , False )])
1083
1099
if len (to_unduplicate ):
1084
1100
self .force (cr , uid , to_unduplicate , context = context )
1085
1101
1086
- def cleanup (self , cr , uid , ids , context = None ):
1102
+ def _local_cleanup (self , cr , uid , ids , context = None ):
1087
1103
for build in self .browse (cr , uid , ids , context = context ):
1088
- cr .execute ("""
1089
- SELECT datname
1090
- FROM pg_database
1091
- WHERE pg_get_userbyid(datdba) = current_user
1092
- AND datname LIKE %s
1093
- """ , [build .dest + '%' ])
1094
- for db , in cr .fetchall ():
1095
- self .pg_dropdb (cr , uid , db )
1104
+ # Cleanup the *local* cluster
1105
+ with local_pgadmin_cursor () as local_cr :
1106
+ local_cr .execute ("""
1107
+ SELECT datname
1108
+ FROM pg_database
1109
+ WHERE pg_get_userbyid(datdba) = current_user
1110
+ AND datname LIKE %s
1111
+ """ , [build .dest + '%' ])
1112
+ to_delete = local_cr .fetchall ()
1113
+ for db , in to_delete :
1114
+ self ._local_pg_dropdb (cr , uid , db )
1096
1115
1097
1116
if os .path .isdir (build .path ()) and build .result != 'killed' :
1098
1117
shutil .rmtree (build .path ())
@@ -1111,7 +1130,7 @@ def kill(self, cr, uid, ids, result=None, context=None):
1111
1130
build .write (v )
1112
1131
cr .commit ()
1113
1132
build .github_status ()
1114
- build .cleanup ()
1133
+ build ._local_cleanup ()
1115
1134
1116
1135
def reap (self , cr , uid , ids ):
1117
1136
while True :
0 commit comments