2323
2424import os
2525import random
26- import socket
26+ # import socket
2727import subprocess
2828import pwd
2929import tempfile
3030import shutil
3131import time
32+ import six
3233
33- # Try to use psycopg2 by default. If psycopg2 isn"t available then use
34+ # Try to use psycopg2 by default. If psycopg2 isn"t available then use
3435# pg8000 which is slower but much more portable because uses only
3536# pure-Python code
3637try :
4344
4445
4546registered_nodes = []
46- last_assigned_port = int (random .random () * 16384 ) + 49152 ;
47+ last_assigned_port = int (random .random () * 16384 ) + 49152
4748pg_config_data = {}
4849
4950
50- """
51- Predefined exceptions
52- """
53- class ClusterException (Exception ): pass
54- class QueryException (Exception ): pass
51+ class ClusterException (Exception ):
52+ """
53+ Predefined exceptions
54+ """
55+ pass
56+
57+
58+ class QueryException (Exception ):
59+ """
60+ Predefined exceptions
61+ """
62+ pass
5563
5664
57- """
58- Transaction wrapper returned by Node
59- """
6065class NodeConnection (object ):
66+ """
67+ Transaction wrapper returned by Node
68+ """
6169 def __init__ (self , parent_node , dbname ):
6270 self .parent_node = parent_node
6371
@@ -77,21 +85,22 @@ def __exit__(self, type, value, tb):
7785 self .connection .close ()
7886
7987 def begin (self , isolation_level = 0 ):
80- levels = [ 'read uncommitted' ,
81- 'read committed' ,
82- 'repeatable read' ,
83- 'serializable' ]
88+ levels = ['read uncommitted' ,
89+ 'read committed' ,
90+ 'repeatable read' ,
91+ 'serializable' ]
8492
93+ print (type (isolation_level ))
8594 # Check if level is int [0..3]
86- if isinstance (isolation_level , int ) and \
87- isolation_level in range (0 , 4 ):
95+ if ( isinstance (isolation_level , int ) and
96+ isolation_level in range (0 , 4 )) :
8897
8998 # Replace index with isolation level type
9099 isolation_level = levels [isolation_level ]
91100
92101 # Or it might be a string
93- elif isinstance (isolation_level , str ) and \
94- str .lower (isolation_level ) in levels :
102+ elif ( isinstance (isolation_level , six . text_type ) and
103+ isolation_level .lower () in levels ) :
95104
96105 # Nothing to do here
97106 pass
@@ -120,7 +129,7 @@ def close(self):
120129 self .connection .close ()
121130
122131
123- class PostgresNode :
132+ class PostgresNode ( object ) :
124133 def __init__ (self , name , port ):
125134 self .name = name
126135 self .host = '127.0.0.1'
@@ -147,13 +156,13 @@ def error_filename(self):
147156
148157 @property
149158 def connstr (self ):
150- return "port=%s" % self .port
159+ return "port=%s" % self .port
151160 # return "port=%s host=%s" % (self.port, self.host)
152161
153162 def get_bin_path (self , filename ):
154163 """ Returns full path to an executable """
155164 pg_config = get_config ()
156- if not "BINDIR" in pg_config :
165+ if "BINDIR" not in pg_config :
157166 return filename
158167 else :
159168 return "%s/%s" % (pg_config .get ("BINDIR" ), filename )
@@ -258,7 +267,7 @@ def pg_ctl(self, command, params):
258267 pg_ctl = self .get_bin_path ("pg_ctl" )
259268
260269 arguments = [pg_ctl ]
261- for key , value in params .iteritems ():
270+ for key , value in six .iteritems (params ):
262271 arguments .append (key )
263272 if value :
264273 arguments .append (value )
@@ -406,9 +415,8 @@ def poll_query_until(self, dbname, query):
406415
407416 while attemps < max_attemps :
408417 ret = self .safe_psql (dbname , query )
409-
410418 # TODO: fix psql so that it returns result without newline
411- if ret == "t\n " :
419+ if ret == six . b ( "t\n " ) :
412420 return
413421
414422 time .sleep (1 )
@@ -446,17 +454,18 @@ def get_username():
446454 """ Returns current user name """
447455 return pwd .getpwuid (os .getuid ())[0 ]
448456
457+
449458def get_config ():
450459 global pg_config_data
451460
452461 if not pg_config_data :
453462 pg_config_cmd = os .environ .get ("PG_CONFIG" ) \
454463 if "PG_CONFIG" in os .environ else "pg_config"
455464
456- out = subprocess .check_output ([pg_config_cmd ])
457- for line in out . split ( " \n " ) :
458- if line :
459- key , value = unicode ( line ) .split ("=" , 1 )
465+ out = six . StringIO ( subprocess .check_output ([pg_config_cmd ], universal_newlines = True ) )
466+ for line in out :
467+ if line and "=" in line :
468+ key , value = line .split ("=" , 1 )
460469 pg_config_data [key .strip ()] = value .strip ()
461470
462471 # Numeric version format
@@ -465,19 +474,22 @@ def get_config():
465474
466475 return pg_config_data
467476
477+
468478def version_to_num (version ):
469479 """Converts PostgreSQL version to number for easier comparison"""
470480 import re
471481
472482 if not version :
473483 return 0
474484 parts = version .split ("." )
475- while len (parts ) < 3 : parts .append ("0" )
485+ while len (parts ) < 3 :
486+ parts .append ("0" )
476487 num = 0
477488 for part in parts :
478489 num = num * 100 + int (re .sub ("[^\d]" , "" , part ))
479490 return num
480491
492+
481493def get_new_node (name ):
482494 global registered_nodes
483495 global last_assigned_port
@@ -505,12 +517,14 @@ def get_new_node(name):
505517
506518 return node
507519
520+
508521def clean_all ():
509522 global registered_nodes
510523 for node in registered_nodes :
511524 node .cleanup ()
512525 registered_nodes = []
513526
527+
514528def stop_all ():
515529 global registered_nodes
516530 for node in registered_nodes :
0 commit comments