@@ -82,10 +82,10 @@ def __str__(self):
8282
8383
8484class LogWriter (threading .Thread ):
85- '''
85+ """
8686 Helper class to implement reading from postgresql.log
8787 and redirect it python logging
88- '''
88+ """
8989
9090 def __init__ (self , node_name , fd ):
9191 assert callable (fd .readline )
@@ -117,9 +117,10 @@ def stopped(self):
117117
118118
119119def log_watch (node_name , pg_logname ):
120- ''' Starts thread for node that redirects postgresql logs
121- to python logging system
122- '''
120+ """
121+ Starts thread for node that redirects postgresql
122+ logs to python logging system
123+ """
123124
124125 reader = LogWriter (node_name , open (pg_logname , 'r' ))
125126 reader .start ()
@@ -348,7 +349,7 @@ def init_from_backup(self,
348349 backup_name ,
349350 has_streaming = False ,
350351 hba_permit_replication = True ):
351- """Initializes cluster from backup, made by another node"""
352+ """ Initializes cluster from backup, made by another node """
352353
353354 # Copy data from backup
354355 backup_path = os .path .join (root_node .base_dir , backup_name )
@@ -377,8 +378,8 @@ def enable_streaming(self, root_node):
377378 self .name ))
378379
379380 def append_conf (self , filename , string ):
380- """Appends line to a config file like "postgresql.conf"
381- or " pg_hba.conf"
381+ """
382+ Appends line to a config file like postgresql.conf or pg_hba.conf
382383
383384 A new line is not automatically appended to the string
384385 """
@@ -389,10 +390,13 @@ def append_conf(self, filename, string):
389390 return self
390391
391392 def pg_ctl (self , command , params = {}, command_options = []):
392- """Runs pg_ctl with specified params
393+ """
394+ Runs pg_ctl with specified params
393395
394396 This function is a workhorse for start(), stop(), reload() and status()
395- functions"""
397+ function
398+ """
399+
396400 pg_ctl = self .get_bin_path ("pg_ctl" )
397401
398402 arguments = [pg_ctl ]
@@ -458,16 +462,18 @@ def print_log_file(log_file):
458462 print ("\n {}:\n ----" .format (conf_filename ))
459463 print_log_file (conf_filename )
460464
461- raise ClusterException ("Couldn't start the new node" )
465+ raise ClusterException ("Cannot start node {}" , self . node )
462466
463467 self .working = True
464468 return self
465469
466470 def status (self ):
467- """ Check cluster status
468- If Running - return True
469- If not Running - return False
470- If there is no data in data_dir or data_dir do not exists - return None
471+ """
472+ Check cluster status
473+
474+ If Running - return True
475+ If not Running - return False
476+ If there is no data in data_dir or data_dir do not exists - return None
471477 """
472478 try :
473479 # TODO: use result?
@@ -487,8 +493,9 @@ def status(self):
487493 return None
488494
489495 def get_pid (self ):
490- """ Check that node is running and return postmaster pid
491- Otherwise return None
496+ """
497+ Check that node is running and return postmaster pid
498+ Otherwise return None
492499 """
493500 if self .status ():
494501 file = open (os .path .join (self .data_dir , 'postmaster.pid' ))
@@ -535,15 +542,15 @@ def restart(self, params={}):
535542 return self
536543
537544 def reload (self , params = {}):
538- """Reloads config files"""
545+ """ Reloads config files """
539546 _params = {"-D" : self .data_dir }
540547 _params .update (params )
541548 self .pg_ctl ("reload" , _params )
542549
543550 return self
544551
545552 def cleanup (self ):
546- """Stops cluster if needed and removes the data directory"""
553+ """ Stops cluster if needed and removes the data directory """
547554
548555 # stop server if it still working
549556 if self .working :
@@ -557,7 +564,8 @@ def cleanup(self):
557564 return self
558565
559566 def psql (self , dbname , query = None , filename = None , username = None ):
560- """Executes a query by the psql
567+ """
568+ Executes a query by the psql
561569
562570 Returns a tuple (code, stdout, stderr) in which:
563571 * code is a return code of psql (0 if alright)
@@ -590,9 +598,10 @@ def psql(self, dbname, query=None, filename=None, username=None):
590598 return process .returncode , out , err
591599
592600 def safe_psql (self , dbname , query , username = None ):
593- """Executes a query by the psql
601+ """
602+ Executes a query by the psql
594603
595- Returns the stdout if succeed. Otherwise throws the
604+ Returns the stdout if succeed. Otherwise throws
596605 ClusterException with stderr output
597606 """
598607 ret , out , err = self .psql (dbname , query , username = username )
@@ -601,7 +610,7 @@ def safe_psql(self, dbname, query, username=None):
601610 return out
602611
603612 def dump (self , dbname , filename ):
604- """Invoke pg_dump and exports database to a file as an sql script"""
613+ """ Invoke pg_dump and exports database to a file as an sql script """
605614 path = os .path .join (self .base_dir , filename )
606615 params = [
607616 self .get_bin_path ("pg_dump" ), "-p {}" .format (self .port ), "-f" ,
@@ -614,7 +623,8 @@ def dump(self, dbname, filename):
614623 raise ClusterException ("Dump creation failed" )
615624
616625 def restore (self , dbname , filename , node = None ):
617- """ Restore database from dump file
626+ """
627+ Restore database from dump file
618628
619629 dbname name of database we're restoring data to
620630 filename path to the dump file
@@ -628,7 +638,7 @@ def restore(self, dbname, filename, node=None):
628638 self .psql (dbname , filename = path )
629639
630640 def poll_query_until (self , dbname , query ):
631- """Runs a query once a second until it returs True"""
641+ """ Runs a query once a second until it returs True """
632642 max_attemps = 60
633643 attemps = 0
634644
@@ -643,15 +653,15 @@ def poll_query_until(self, dbname, query):
643653 raise QueryException ("Timeout while waiting for query to return True" )
644654
645655 def execute (self , dbname , query , username = None , commit = False ):
646- """Executes the query and returns all rows"""
656+ """ Executes the query and returns all rows """
647657 with self .connect (dbname , username ) as node_con :
648658 res = node_con .execute (query )
649659 if commit :
650660 node_con .commit ()
651661 return res
652662
653663 def backup (self , name ):
654- """Performs pg_basebackup"""
664+ """ Performs pg_basebackup """
655665 pg_basebackup = self .get_bin_path ("pg_basebackup" )
656666 backup_path = os .path .join (self .base_dir , name )
657667 os .makedirs (backup_path )
@@ -668,7 +678,7 @@ def backup(self, name):
668678 return backup_path
669679
670680 def pgbench_init (self , dbname = 'postgres' , scale = 1 , options = []):
671- """Prepare pgbench database"""
681+ """ Prepare pgbench database """
672682 pgbench = self .get_bin_path ("pgbench" )
673683 params = [pgbench , "-i" , "-s" ,
674684 "%i" % scale , "-p" ,
@@ -682,7 +692,7 @@ def pgbench_init(self, dbname='postgres', scale=1, options=[]):
682692 return True
683693
684694 def pgbench (self , dbname = 'postgres' , stdout = None , stderr = None , options = []):
685- """Make pgbench process"""
695+ """ Make pgbench process """
686696 pgbench = self .get_bin_path ("pgbench" )
687697 params = [pgbench , "-p" , "%i" % self .port ] + options + [dbname ]
688698 proc = subprocess .Popen (params , stdout = stdout , stderr = stderr )
@@ -724,7 +734,7 @@ def get_config():
724734
725735
726736def version_to_num (version ):
727- """Converts PostgreSQL version to number for easier comparison"""
737+ """ Converts PostgreSQL version to number for easier comparison """
728738 import re
729739
730740 if not version :
@@ -774,7 +784,7 @@ def stop_all():
774784 global util_threads
775785
776786 for node in registered_nodes :
777- # stop server if it still working
787+ # stop server if it's still working
778788 if node .working :
779789 node .stop ()
780790
0 commit comments