Skip to content

Commit

Permalink
Merge pull request #14 from mosquito/featire/refactoring
Browse files Browse the repository at this point in the history
Drop old python versions and reformat code
  • Loading branch information
mosquito committed Mar 24, 2023
2 parents 0f6e0bc + 40a200d commit b710226
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 60 deletions.
9 changes: 5 additions & 4 deletions pidfile/__init__.py
@@ -1,7 +1,8 @@
from .pidfile import PIDFile, AlreadyRunningError
from .version import version_info, __version__, __author__, author_info
from .pidfile import AlreadyRunningError, PIDFile
from .version import __author__, __version__, author_info, version_info


__all__ = (
'__version__', '__author__', 'author_info',
'version_info', 'PIDFile', 'AlreadyRunningError'
"__version__", "__author__", "author_info",
"version_info", "PIDFile", "AlreadyRunningError",
)
14 changes: 8 additions & 6 deletions pidfile/pidfile.py
@@ -1,5 +1,7 @@
import atexit
import os
from typing import Any

import psutil


Expand All @@ -8,12 +10,12 @@ class AlreadyRunningError(Exception):


class PIDFile(object):
def __init__(self, filename='pidfile'):
def __init__(self, filename: Any = "pidfile"):
self._process_name = psutil.Process(os.getpid()).cmdline()[0]
self._file = filename
self._file = str(filename)

@property
def is_running(self):
def is_running(self) -> bool:
if not os.path.exists(self._file):
return False

Expand All @@ -32,14 +34,14 @@ def is_running(self):
except psutil.AccessDenied:
return False

def close(self):
def close(self) -> None:
if os.path.exists(self._file):
try:
os.unlink(self._file)
except OSError:
pass

def __enter__(self):
def __enter__(self) -> "PIDFile":
if self.is_running:
raise AlreadyRunningError

Expand All @@ -50,6 +52,6 @@ def __enter__(self):

return self

def __exit__(self, *args):
def __exit__(self, *_) -> None:
self.close()
atexit.unregister(self.close)
2 changes: 1 addition & 1 deletion pidfile/version.py
Expand Up @@ -5,7 +5,7 @@
("Michele Cancilla", None),
)

version_info = (3, 1, 0)
version_info = (3, 1, 1)

__version__ = ".".join(map(str, version_info))
__author__ = ", ".join("{0} <{1}>".format(*author) for author in author_info)
26 changes: 8 additions & 18 deletions setup.py
@@ -1,21 +1,13 @@
import sys
import os
import importlib.util
from setuptools import setup, find_packages


version_file = os.path.join('pidfile', 'version.py')

if sys.version_info < (3,):
import imp
version = imp.load_source('version', version_file)
elif sys.version_info < (3, 5):
from importlib.machinery import SourceFileLoader
version = SourceFileLoader("version", version_file).load_module()
else:
import importlib.util
spec = importlib.util.spec_from_file_location("version", version_file)
version = importlib.util.module_from_spec(spec)
spec.loader.exec_module(version)
spec = importlib.util.spec_from_file_location("version", version_file)
version = importlib.util.module_from_spec(spec)
spec.loader.exec_module(version)


setup(
Expand All @@ -27,22 +19,20 @@
),
url="https://github.com/mosquito/python-pidfile",
license="MIT",
description="PIDFile context processor. Supported py2 and py3",
description="PIDFile context manager.",
long_description=open('README.rst').read(),
platforms="unix",
classifiers=[
'Development Status :: 5 - Production/Stable',
'Natural Language :: English',
'Operating System :: MacOS',
'Operating System :: POSIX',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: Implementation :: CPython',
'License :: OSI Approved :: MIT License',
],
Expand Down
60 changes: 29 additions & 31 deletions tests/test_pidfile.py
@@ -1,24 +1,18 @@
import os
from unittest import TestCase
from unittest.mock import mock_open, patch

try:
# Python 3.x
from unittest.mock import patch, mock_open
open_name = 'builtins.open'
except ImportError:
# Python 2.7
from mock import patch, mock_open
open_name = '__builtin__.open'
import psutil

import pidfile
import os
import psutil


builtins_open = open


def open_patcher(data):
def patched_open(*args, **kwargs):
if args[0] == 'pidfile':
if args[0] == "pidfile":
return mock_open(read_data=data)(*args, **kwargs)
else:
return builtins_open(*args, **kwargs)
Expand All @@ -27,7 +21,7 @@ def patched_open(*args, **kwargs):

def open_patcher_exception():
def patched_open(*args, **kwargs):
if args[0] == 'pidfile':
if args[0] == "pidfile":
mo = mock_open()
mo.return_value.read.side_effect = OSError
return mo(*args, **kwargs)
Expand All @@ -37,48 +31,52 @@ def patched_open(*args, **kwargs):


class PIDFileTestCase(TestCase):
@patch(open_name, new=open_patcher('1'))
@patch('os.path.exists')
@patch("builtins.open", new=open_patcher("1"))
@patch("os.path.exists")
def test_pidfile_not_exists(self, exists_mock):
exists_mock.return_value = False
with pidfile.PIDFile():
assert True

@patch(open_name, new=open_patcher('1'))
@patch('psutil.pid_exists')
@patch('psutil.Process')
@patch('os.path.exists')
def test_pidfile_exists_process_running(self, exists_mock, Process_mock,
pid_exists_mock):
@patch("builtins.open", new=open_patcher("1"))
@patch("psutil.pid_exists")
@patch("psutil.Process")
@patch("os.path.exists")
def test_pidfile_exists_process_running(
self, exists_mock, Process_mock,
pid_exists_mock,
):
exists_mock.return_value = True
pid_exists_mock.return_value = True
Process_mock.return_value = psutil.Process(os.getpid())
with self.assertRaises(pidfile.AlreadyRunningError):
with pidfile.PIDFile():
assert True

@patch(open_name, new=open_patcher('1'))
@patch('psutil.pid_exists')
@patch('os.path.exists')
def test_pidfile_exists_process_not_running(self, exists_mock,
pid_exists_mock):
@patch("builtins.open", new=open_patcher("1"))
@patch("psutil.pid_exists")
@patch("os.path.exists")
def test_pidfile_exists_process_not_running(
self, exists_mock,
pid_exists_mock,
):
exists_mock.return_value = True
pid_exists_mock.return_value = False
with pidfile.PIDFile():
assert True

@patch(open_name, new=open_patcher(''))
@patch('psutil.pid_exists')
@patch('os.path.exists')
@patch("builtins.open", new=open_patcher(""))
@patch("psutil.pid_exists")
@patch("os.path.exists")
def test_pidfile_exists_empty(self, exists_mock, pid_exists_mock):
exists_mock.return_value = True
pid_exists_mock.return_value = True
with pidfile.PIDFile():
assert True

@patch(open_name, new=open_patcher_exception())
@patch('psutil.pid_exists')
@patch('os.path.exists')
@patch("builtins.open", new=open_patcher_exception())
@patch("psutil.pid_exists")
@patch("os.path.exists")
def test_pidfile_exists_read_fail(self, exists_mock, pid_exists_mock):
exists_mock.return_value = True
pid_exists_mock.return_value = True
Expand Down

0 comments on commit b710226

Please sign in to comment.