11# coding: utf-8
22
33import atexit
4+ import io
45import os
56import shutil
67import subprocess
@@ -155,21 +156,22 @@ def _format_verbose_error(self):
155156 def print_node_file (node_file ):
156157 if os .path .exists (node_file ):
157158 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' )
160161 except Exception as e :
161162 pass
162163 return "### file not found ###\n "
163164
165+ # yapf: disable
164166 error_text = (
165167 u"{}:\n ----\n {}\n " # log file, e.g. postgresql.log
166168 u"{}:\n ----\n {}\n " # postgresql.conf
167169 u"{}:\n ----\n {}\n " # pg_hba.conf
168170 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 ))
173175
174176 return error_text
175177
@@ -219,7 +221,7 @@ def default_conf(self,
219221 hba_conf = os .path .join (self .data_dir , "pg_hba.conf" )
220222
221223 # filter lines in hba file
222- with open (hba_conf , "r+" ) as conf :
224+ with io . open (hba_conf , "r+" ) as conf :
223225 # get rid of comments and blank lines
224226 lines = [
225227 s for s in conf .readlines ()
@@ -242,11 +244,11 @@ def get_auth_method(t):
242244 auth_local = get_auth_method ('local' )
243245 auth_host = get_auth_method ('host' )
244246
247+ # yapf: disable
245248 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 )
250252 ]
251253
252254 # write missing lines
@@ -255,14 +257,16 @@ def get_auth_method(t):
255257 conf .write (line )
256258
257259 # overwrite postgresql.conf file
258- with open (postgres_conf , "w" ) as conf :
260+ with io . open (postgres_conf , "w" ) as conf :
259261 if not fsync :
260- conf .write ("fsync = off\n " )
262+ conf .write (u "fsync = off\n " )
261263
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 ))
266270
267271 # replication-related settings
268272 if allow_streaming :
@@ -273,13 +277,15 @@ def get_auth_method(t):
273277 else :
274278 wal_level = "hot_standby"
275279
280+ # yapf: disable
276281 max_wal_senders = 5
277282 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 ))
283289
284290 return self
285291
@@ -296,8 +302,8 @@ def append_conf(self, filename, string):
296302 """
297303
298304 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 ' ]))
301307
302308 return self
303309
@@ -329,7 +335,7 @@ def get_pid(self):
329335 """
330336
331337 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 :
333339 return int (f .readline ())
334340
335341 # for clarity
0 commit comments