38
38
import tempfile
39
39
import threading
40
40
import time
41
+ import traceback
41
42
42
43
import port_for
43
44
@@ -349,7 +350,7 @@ def _prepare_dir(self, destroy):
349
350
# Copy backup to new data dir
350
351
shutil .copytree (data1 ,data2 )
351
352
except Exception as e :
352
- raise BackupException (str (e ))
353
+ raise BackupException (_explain_exception (e ))
353
354
else :
354
355
base_dir = self .base_dir
355
356
@@ -691,13 +692,13 @@ def print_node_file(node_file):
691
692
return "### file not found ###\n "
692
693
693
694
error_text = (
694
- "Cannot start node\n "
695
- "{}\n " # pg_ctl log
696
- "{}:\n ----\n {}\n " # postgresql.log
697
- "{}:\n ----\n {}\n " # postgresql.conf
698
- "{}:\n ----\n {}\n " # pg_hba.conf
699
- "{}:\n ----\n {}\n " # recovery.conf
700
- ).format (str (e ),
695
+ u "Cannot start node\n "
696
+ u "{}\n "# pg_ctl log
697
+ u "{}:\n ----\n {}\n "# postgresql.log
698
+ u "{}:\n ----\n {}\n "# postgresql.conf
699
+ u "{}:\n ----\n {}\n "# pg_hba.conf
700
+ u "{}:\n ----\n {}\n "# recovery.conf
701
+ ).format (_explain_exception (e ),
701
702
log_filename ,print_node_file (log_filename ),
702
703
conf_filename ,print_node_file (conf_filename ),
703
704
hba_filename ,print_node_file (hba_filename ),
@@ -867,7 +868,9 @@ def safe_psql(self, dbname, query, username=None):
867
868
868
869
ret ,out ,err = self .psql (dbname ,query ,username = username )
869
870
if ret :
870
- raise QueryException (six .text_type (err ))
871
+ if err :
872
+ err = err .decode ('utf-8' )
873
+ raise QueryException (err )
871
874
return out
872
875
873
876
def dump (self ,dbname ,filename = None ):
@@ -1041,7 +1044,7 @@ def catchup(self):
1041
1044
lsn = master .execute ('postgres' ,poll_lsn )[0 ][0 ]
1042
1045
self .poll_query_until ('postgres' ,wait_lsn .format (lsn ))
1043
1046
except Exception as e :
1044
- raise CatchUpException (str (e ))
1047
+ raise CatchUpException (_explain_exception (e ))
1045
1048
1046
1049
def pgbench_init (self ,dbname = 'postgres' ,scale = 1 ,options = []):
1047
1050
"""
@@ -1103,6 +1106,15 @@ def connect(self, dbname='postgres', username=None):
1103
1106
username = username )
1104
1107
1105
1108
1109
+ def _explain_exception (e ):
1110
+ """
1111
+ Use this function instead of str(e).
1112
+ """
1113
+
1114
+ lines = traceback .format_exception_only (type (e ),e )
1115
+ return '' .join (lines )
1116
+
1117
+
1106
1118
def _cached_initdb (data_dir ,initdb_logfile ,initdb_params = []):
1107
1119
"""
1108
1120
Perform initdb or use cached node files.
@@ -1113,7 +1125,7 @@ def call_initdb(_data_dir):
1113
1125
_params = [_data_dir ,"-N" ]+ initdb_params
1114
1126
_execute_utility ("initdb" ,_params ,initdb_logfile )
1115
1127
except Exception as e :
1116
- raise InitNodeException (str (e ))
1128
+ raise InitNodeException (_explain_exception (e ))
1117
1129
1118
1130
# Call initdb if we have custom params
1119
1131
if initdb_params or not TestgresConfig .cache_initdb :
@@ -1144,7 +1156,7 @@ def rm_cached_data_dir(rm_dir):
1144
1156
shutil .copytree (cached_data_dir ,data_dir )
1145
1157
1146
1158
except Exception as e :
1147
- raise InitNodeException (str (e ))
1159
+ raise InitNodeException (_explain_exception (e ))
1148
1160
1149
1161
1150
1162
def _execute_utility (util ,args ,logfile ,write_to_pipe = True ):
@@ -1174,23 +1186,29 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
1174
1186
1175
1187
# get result
1176
1188
out ,_ = process .communicate ()
1177
- out = '' if not out else six .text_type (out )
1178
1189
1179
1190
# write new log entry if possible
1180
1191
try :
1181
1192
with open (logfile ,"a" )as file_out :
1182
- # write util name + args
1183
- file_out .write ('' .join (map (lambda x :str (x )+ ' ' ,
1184
- [util ]+ args )))
1185
- file_out .write ('\n ' )
1186
- file_out .write (out )
1193
+ # write util name
1194
+ file_out .write (util )
1195
+ # write args
1196
+ for arg in args :
1197
+ file_out .write (arg )
1198
+ with open (logfile ,"ab" )as file_out :
1199
+ # write output
1200
+ if out :
1201
+ file_out .write (out )
1187
1202
except IOError :
1188
1203
pass
1189
1204
1205
+ # decode output
1206
+ out = '' if not out else out .decode ('utf-8' )
1207
+
1190
1208
if process .returncode :
1191
1209
error_text = (
1192
- "{} failed\n "
1193
- "log:\n ----\n {}\n "
1210
+ u "{} failed\n "
1211
+ u "log:\n ----\n {}\n "
1194
1212
).format (util ,out )
1195
1213
1196
1214
raise ExecUtilException (error_text ,process .returncode )