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