-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from fizyk/import_code
import code from pytest-postgresql
- Loading branch information
Showing
13 changed files
with
631 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ develop-eggs | |
.installed.cfg | ||
lib | ||
lib64 | ||
.cache | ||
|
||
# Installer logs | ||
pip-log.txt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# Ignored linter errors and explanations | ||
# D203 - 1 blank line required before class docstring (found 0) [pydocstyle] - Opposite of D211 No blank lines allowed before class docstring (found 1) | ||
|
||
[pylama] | ||
linters = pep8,pyflakes,pydocstyle | ||
ignore = D203 | ||
skip = docs/*,\ | ||
build/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright (C) 2013 by Clearcode <http://clearcode.cc> | ||
# and associates (see AUTHORS). | ||
|
||
# This file is part of pytest-dbfixtures. | ||
|
||
# pytest-dbfixtures is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
|
||
# pytest-dbfixtures is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser General Public License for more details. | ||
|
||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with pytest-dbfixtures. If not, see <http://www.gnu.org/licenses/>. | ||
"""PostgreSQL executor crafter around pg_ctl.""" | ||
|
||
import re | ||
import subprocess | ||
|
||
from path import path | ||
|
||
from mirakuru import TCPExecutor | ||
|
||
|
||
class PostgreSQLExecutor(TCPExecutor): | ||
""" | ||
PostgreSQL executor running on pg_ctl. | ||
Based over an `pg_ctl program | ||
<http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_ | ||
""" | ||
|
||
BASE_PROC_START_COMMAND = """{pg_ctl} start -D {datadir} | ||
-o "-F -p {port} -c log_destination='stderr' -c %s='{unixsocketdir}'" | ||
-l {logfile} {startparams}""" | ||
|
||
def __init__(self, pg_ctl, host, port, | ||
datadir, unixsocketdir, logfile, startparams, | ||
shell=False, timeout=60, sleep=0.1): | ||
""" | ||
Initialize PostgreSQLExecutor executor. | ||
:param str pg_ctl: pg_ctl location | ||
:param str host: host under which process is accessible | ||
:param int port: port under which process is accessible | ||
:param str datadir: path to postgresql datadir | ||
:param str unixsocketdir: path to socket directory | ||
:param str logfile: path to logfile for postgresql | ||
:param str startparams: additional start parameters | ||
:param bool shell: see `subprocess.Popen` | ||
:param int timeout: time to wait for process to start or stop. | ||
if None, wait indefinitely. | ||
:param float sleep: how often to check for start/stop condition | ||
""" | ||
self.pg_ctl = pg_ctl | ||
self.version = self.version() | ||
self.datadir = path(datadir) | ||
self.unixsocketdir = unixsocketdir | ||
command = self.proc_start_command().format( | ||
pg_ctl=self.pg_ctl, | ||
datadir=self.datadir, | ||
port=port, | ||
unixsocketdir=self.unixsocketdir, | ||
logfile=logfile, | ||
startparams=startparams, | ||
) | ||
super(PostgreSQLExecutor, self).__init__( | ||
command, host, port, shell=shell, timeout=timeout, sleep=sleep) | ||
|
||
def proc_start_command(self): | ||
"""Based on postgres version return proper start command.""" | ||
if float(self.version) > 9.2: | ||
unix_socket_dir_arg_name = 'unix_socket_directories' | ||
else: | ||
unix_socket_dir_arg_name = 'unix_socket_directory' | ||
return self.BASE_PROC_START_COMMAND % unix_socket_dir_arg_name | ||
|
||
def version(self): | ||
"""Detect postgresql version.""" | ||
version_string = subprocess.check_output( | ||
[self.pg_ctl, '--version']).decode('utf-8') | ||
matches = re.search('.* (?P<version>\d\.\d)', version_string) | ||
return matches.groupdict()['version'] | ||
|
||
def running(self): | ||
"""Check if server is still running.""" | ||
if not self.datadir.exists(): | ||
return False | ||
|
||
output = subprocess.check_output( | ||
'{pg_ctl} status -D {datadir}'.format( | ||
pg_ctl=self.pg_ctl, | ||
datadir=self.datadir | ||
), | ||
shell=True | ||
).decode('utf-8') | ||
return "pg_ctl: server is running" in output | ||
|
||
def stop(self): | ||
"""Issue a stop request to pg_ctl.""" | ||
subprocess.check_output( | ||
'{pg_ctl} stop -D {datadir} -m f'.format( | ||
pg_ctl=self.pg_ctl, | ||
datadir=self.datadir, | ||
port=self.port, | ||
unixsocketdir=self.unixsocketdir | ||
), | ||
shell=True) |
Oops, something went wrong.