1
1
# coding: utf-8
2
2
3
3
import atexit
4
+ import io
4
5
import os
5
6
import shutil
6
7
import subprocess
@@ -155,21 +156,22 @@ def _format_verbose_error(self):
155
156
def print_node_file (node_file ):
156
157
if os .path .exists (node_file ):
157
158
try :
158
- with open (node_file , 'r' ) as f :
159
- return f .read ()
159
+ with io . open (node_file , "r" ) as f :
160
+ return f .read (). decode ( 'utf-8' )
160
161
except Exception as e :
161
162
pass
162
163
return "### file not found ###\n "
163
164
165
+ # yapf: disable
164
166
error_text = (
165
167
u"{}:\n ----\n {}\n " # log file, e.g. postgresql.log
166
168
u"{}:\n ----\n {}\n " # postgresql.conf
167
169
u"{}:\n ----\n {}\n " # pg_hba.conf
168
170
u"{}:\n ----\n {}\n " # recovery.conf
169
- ).format (log_filename , print_node_file (log_filename ), conf_filename ,
170
- print_node_file (conf_filename ), hba_filename ,
171
- print_node_file (hba_filename ), recovery_filename ,
172
- print_node_file (recovery_filename ))
171
+ ).format (log_filename , print_node_file (log_filename ),
172
+ conf_filename , print_node_file (conf_filename ),
173
+ hba_filename , print_node_file (hba_filename ),
174
+ recovery_filename , print_node_file (recovery_filename ))
173
175
174
176
return error_text
175
177
@@ -219,7 +221,7 @@ def default_conf(self,
219
221
hba_conf = os .path .join (self .data_dir , "pg_hba.conf" )
220
222
221
223
# filter lines in hba file
222
- with open (hba_conf , "r+" ) as conf :
224
+ with io . open (hba_conf , "r+" ) as conf :
223
225
# get rid of comments and blank lines
224
226
lines = [
225
227
s for s in conf .readlines ()
@@ -242,11 +244,11 @@ def get_auth_method(t):
242
244
auth_local = get_auth_method ('local' )
243
245
auth_host = get_auth_method ('host' )
244
246
247
+ # yapf: disable
245
248
new_lines = [
246
- "local\t replication\t all\t \t \t {}\n " .format (auth_local ),
247
- "host\t replication\t all\t 127.0.0.1/32\t {}\n " .format (
248
- auth_host ),
249
- "host\t replication\t all\t ::1/128\t \t {}\n " .format (auth_host )
249
+ u"local\t replication\t all\t \t \t {}\n " .format (auth_local ),
250
+ u"host\t replication\t all\t 127.0.0.1/32\t {}\n " .format (auth_host ),
251
+ u"host\t replication\t all\t ::1/128\t \t {}\n " .format (auth_host )
250
252
]
251
253
252
254
# write missing lines
@@ -255,14 +257,16 @@ def get_auth_method(t):
255
257
conf .write (line )
256
258
257
259
# overwrite postgresql.conf file
258
- with open (postgres_conf , "w" ) as conf :
260
+ with io . open (postgres_conf , "w" ) as conf :
259
261
if not fsync :
260
- conf .write ("fsync = off\n " )
262
+ conf .write (u "fsync = off\n " )
261
263
262
- conf .write ("log_statement = {}\n "
263
- "listen_addresses = '{}'\n "
264
- "port = {}\n " .format (log_statement , self .host ,
265
- self .port ))
264
+ # yapf: disable
265
+ conf .write (u"log_statement = {}\n "
266
+ u"listen_addresses = '{}'\n "
267
+ u"port = {}\n " .format (log_statement ,
268
+ self .host ,
269
+ self .port ))
266
270
267
271
# replication-related settings
268
272
if allow_streaming :
@@ -273,13 +277,15 @@ def get_auth_method(t):
273
277
else :
274
278
wal_level = "hot_standby"
275
279
280
+ # yapf: disable
276
281
max_wal_senders = 5
277
282
wal_keep_segments = 20
278
- conf .write ("hot_standby = on\n "
279
- "max_wal_senders = {}\n "
280
- "wal_keep_segments = {}\n "
281
- "wal_level = {}\n " .format (
282
- max_wal_senders , wal_keep_segments , wal_level ))
283
+ conf .write (u"hot_standby = on\n "
284
+ u"max_wal_senders = {}\n "
285
+ u"wal_keep_segments = {}\n "
286
+ u"wal_level = {}\n " .format (max_wal_senders ,
287
+ wal_keep_segments ,
288
+ wal_level ))
283
289
284
290
return self
285
291
@@ -296,8 +302,8 @@ def append_conf(self, filename, string):
296
302
"""
297
303
298
304
config_name = os .path .join (self .data_dir , filename )
299
- with open (config_name , "a" ) as conf :
300
- conf .write ('' .join ([string , '\n ' ]))
305
+ with io . open (config_name , "a" ) as conf :
306
+ conf .write (u"" .join ([string , '\n ' ]))
301
307
302
308
return self
303
309
@@ -329,7 +335,7 @@ def get_pid(self):
329
335
"""
330
336
331
337
if self .status ():
332
- with open (os .path .join (self .data_dir , 'postmaster.pid' )) as f :
338
+ with io . open (os .path .join (self .data_dir , 'postmaster.pid' )) as f :
333
339
return int (f .readline ())
334
340
335
341
# for clarity
0 commit comments