Skip to content

pep8 fixes and windows compatibility #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from distutils.core import setup
setup(
name = 'testgres',
packages = ['testgres'],
version = '0.1.11',
description = 'Testing utility for postgresql and it''s extensions',
author = 'Ildar Musin',
author_email = '[email protected]',
url = 'https://github.com/postgrespro/testgres',
keywords = ['testing', 'postgresql'],
classifiers = [],
install_requires = ["pg8000", "six"]
name='testgres',
packages=['testgres'],
version='0.1.11',
description='Testing utility for postgresql and it''s extensions',
author='Ildar Musin',
author_email='[email protected]',
url='https://github.com/postgrespro/testgres',
keywords=['testing', 'postgresql'],
classifiers=[],
install_requires=["pg8000", "six"]
)
48 changes: 27 additions & 21 deletions testgres/testgres.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#coding: utf-8
# coding: utf-8
"""
testgres.py
Postgres testing utility
Expand Down Expand Up @@ -49,23 +49,27 @@


class ClusterException(Exception):

"""
Predefined exceptions
"""
pass


class QueryException(Exception):

"""
Predefined exceptions
"""
pass


class NodeConnection(object):

"""
Transaction wrapper returned by Node
"""

def __init__(self, parent_node, dbname):
self.parent_node = parent_node

Expand Down Expand Up @@ -93,7 +97,7 @@ def begin(self, isolation_level=0):
print(type(isolation_level))
# Check if level is int [0..3]
if (isinstance(isolation_level, int) and
isolation_level in range(0, 4)):
isolation_level in range(0, 4)):

# Replace index with isolation level type
isolation_level = levels[isolation_level]
Expand Down Expand Up @@ -130,6 +134,7 @@ def close(self):


class PostgresNode(object):

def __init__(self, name, port):
self.name = name
self.host = '127.0.0.1'
Expand All @@ -140,19 +145,19 @@ def __init__(self, name, port):

@property
def data_dir(self):
return "%s/data" % self.base_dir
return os.path.join(self.base_dir, "data")

@property
def logs_dir(self):
return "%s/logs" % self.base_dir
return os.path.join(self.base_dir, "logs")

@property
def output_filename(self):
return "%s/stdout.log" % self.logs_dir
return os.path.join(self.logs_dir, "stdout.log")

@property
def error_filename(self):
return "%s/stderr.log" % self.logs_dir
return os.path.join(self.logs_dir, "stderr.log")

@property
def connstr(self):
Expand All @@ -165,7 +170,7 @@ def get_bin_path(self, filename):
if "BINDIR" not in pg_config:
return filename
else:
return "%s/%s" % (pg_config.get("BINDIR"), filename)
return os.path.join(pg_config.get("BINDIR"), filename)

def init(self, allows_streaming=False):
""" Performs initdb """
Expand All @@ -174,7 +179,7 @@ def init(self, allows_streaming=False):
os.makedirs(self.data_dir)
initdb = self.get_bin_path("initdb")
with open(self.output_filename, "a") as file_out, \
open(self.error_filename, "a") as file_err:
open(self.error_filename, "a") as file_err:
ret = subprocess.call(
[initdb, self.data_dir, "-N"],
stdout=file_out,
Expand All @@ -184,8 +189,8 @@ def init(self, allows_streaming=False):
raise ClusterException("Cluster initialization failed")

# add parameters to config file
config_name = "%s/postgresql.conf" % self.data_dir
with open(config_name, "a") as conf:
postgres_conf = os.path.join(self.data_dir, "postgresql.conf")
with open(postgres_conf, "a") as conf:
conf.write(
"fsync = off\n"
"log_statement = all\n"
Expand Down Expand Up @@ -218,7 +223,7 @@ def init_from_backup(self, root_node, backup_name, has_streaming=False, hba_perm
"""Initializes cluster from backup, made by another node"""

# Copy data from backup
backup_path = "%s/%s" % (root_node.base_dir, backup_name)
backup_path = os.path.join(root_node.base_dir, backup_name)
shutil.copytree(backup_path, self.data_dir)
os.chmod(self.data_dir, 0o0700)

Expand All @@ -234,14 +239,14 @@ def init_from_backup(self, root_node, backup_name, has_streaming=False, hba_perm
self.enable_streaming(root_node)

def set_replication_conf(self):
hba_conf = "%s/pg_hba.conf" % self.data_dir
hba_conf = os.path.join(self.data_dir, "pg_hba.conf")
with open(hba_conf, "a") as conf:
conf.write("local replication all trust\n")
# conf.write("host replication all 127.0.0.1/32 trust\n")

def enable_streaming(self, root_node):
config_name = "%s/recovery.conf" % self.data_dir
with open(config_name, "a") as conf:
recovery_conf = os.path.join(self.data_dir, "recovery.conf")
with open(recovery_conf, "a") as conf:
conf.write(
"primary_conninfo='%s application_name=%s'\n"
"standby_mode=on\n"
Expand Down Expand Up @@ -273,7 +278,7 @@ def pg_ctl(self, command, params):
arguments.append(value)

with open(self.output_filename, "a") as file_out, \
open(self.error_filename, "a") as file_err:
open(self.error_filename, "a") as file_err:

res = subprocess.call(
arguments + [command],
Expand All @@ -287,7 +292,7 @@ def pg_ctl(self, command, params):

def start(self):
""" Starts cluster """
logfile = self.logs_dir + "/postgresql.log"
logfile = os.path.join(self.logs_dir, "postgresql.log")
params = {
"-D": self.data_dir,
"-w": None,
Expand Down Expand Up @@ -381,7 +386,7 @@ def safe_psql(self, dbname, query):

def dump(self, dbname, filename):
"""Invoke pg_dump and exports database to a file as an sql script"""
path = self.base_dir + "/" + filename
path = os.path.join(self.base_dir, filename)
params = [
self.get_bin_path("pg_dump"),
"-p %s" % self.port,
Expand All @@ -405,7 +410,7 @@ def restore(self, dbname, filename, node=None):
if not node:
node = self

path = node.base_dir + "/" + filename
path = os.path.join(node.base_dir, filename)
self.psql(dbname, filename=path)

def poll_query_until(self, dbname, query):
Expand All @@ -431,11 +436,11 @@ def execute(self, dbname, query):
def backup(self, name):
"""Performs pg_basebackup"""
pg_basebackup = self.get_bin_path("pg_basebackup")
backup_path = self.base_dir + "/" + name
backup_path = os.path.join(self.base_dir, name)
os.makedirs(backup_path)
params = [pg_basebackup, "-D", backup_path, "-p %s" % self.port, "-x"]
with open(self.output_filename, "a") as file_out, \
open(self.error_filename, "a") as file_err:
open(self.error_filename, "a") as file_err:
ret = subprocess.call(
params,
stdout=file_out,
Expand All @@ -462,7 +467,8 @@ def get_config():
pg_config_cmd = os.environ.get("PG_CONFIG") \
if "PG_CONFIG" in os.environ else "pg_config"

out = six.StringIO(subprocess.check_output([pg_config_cmd], universal_newlines=True))
out = six.StringIO(
subprocess.check_output([pg_config_cmd], universal_newlines=True))
for line in out:
if line and "=" in line:
key, value = line.split("=", 1)
Expand Down