Skip to content

Commit

Permalink
Merge pull request #2 from fizyk/import_code
Browse files Browse the repository at this point in the history
import code from pytest-postgresql
  • Loading branch information
fizyk committed Sep 24, 2016
2 parents 29e82de + 7837b8e commit 672b15d
Show file tree
Hide file tree
Showing 13 changed files with 631 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ develop-eggs
.installed.cfg
lib
lib64
.cache

# Installer logs
pip-log.txt
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:
- "$INSTALLATION"
- pip install pytest-postgresql[tests] coveralls wheel
script:
- py.test -n $XDIST --cov src/pytest_postgresql tests
- py.test -n $XDIST --cov src/pytest_postgresql
- pylama
- pyroma .
after_success:
Expand Down
10 changes: 9 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ Authors
This file contains the list of people involved in the development
of pytest-postgresql along its history.

* Clearcode - The A Room
* Grzegorz Śliwiński
* Jonas Brunsgaard
* Tomasz Karbownicki
* Domen Kožar
* Przemysław Spodymek
* Michał Masłowski
* Karolina Blümke
* Paweł Wilczyński
* Georg Walther
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pytest-postgresql
====
=================

.. image:: https://img.shields.io/pypi/v/pytest-postgresql.svg
:target: https://pypi.python.org/pypi/pytest-postgresql/
Expand Down
4 changes: 4 additions & 0 deletions pylama.ini
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/*
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def read(fname):
setup(
name='pytest-postgresql',
version=package_version,
description='',
description='Postgresql fixtures and fixture factories for Pytest.',
long_description=(
read('README.rst') + '\n\n' + read('CHANGES.rst')
),
Expand All @@ -88,6 +88,10 @@ def read(fname):
install_requires=requirements,
tests_require=test_requires,
test_suite='tests',
entry_points={
'pytest11': [
'pytest_postgresql = pytest_postgresql.plugin'
]},
include_package_data=True,
zip_safe=False,
extras_require=extras_require,
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_postgresql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# You should have received a copy of the GNU Lesser General Public License
# along with pytest-postgresql. If not, see <http://www.gnu.org/licenses/>.

"""Main module for pytest-postgresql."""

import logging

Expand Down
111 changes: 111 additions & 0 deletions src/pytest_postgresql/executor.py
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)
Loading

0 comments on commit 672b15d

Please sign in to comment.