Skip to content

Commit

Permalink
format and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Sep 18, 2022
1 parent eabc889 commit f9af1b0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 37 deletions.
14 changes: 12 additions & 2 deletions keyutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@

from __future__ import absolute_import


from . import _keyutils


for k, v in _keyutils.constants.__dict__.items():
globals()[k] = v
del k, v

from errno import EINVAL, ENOMEM, EDQUOT, EINTR, EACCES

from errno import ( # noqa: F401,E402 , imported for reexport; TODO: better reexport
EACCES,
EDQUOT,
EINTR,
EINVAL,
ENOMEM,
)


Error = _keyutils.error


def add_key(key, value, keyring, keyType=b"user"):
return _keyutils.add_key(keyType, key, value, keyring)

Expand Down
1 change: 1 addition & 0 deletions keyutils/_keyutils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from libc cimport stdlib


cdef extern from "Python.h":
object PyErr_SetFromErrno(exc)
object PyBytes_FromStringAndSize(char *str, Py_ssize_t size)
Expand Down
32 changes: 17 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@


import io
from setuptools import setup, Extension

with io.open('README.rst', "r", encoding="utf-8") as f:
from setuptools import Extension, setup


with io.open("README.rst", "r", encoding="utf-8") as f:
long_description = f.read()

setup(
name='keyutils',
version='0.6',
description='keyutils bindings for Python',
name="keyutils",
version="0.6",
description="keyutils bindings for Python",
long_description=long_description,
author='Mihai Ibanescu',
author_email='[email protected]',
url='https://github.com/sassoftware/python-keyutils',
license='Apache 2.0',
packages=['keyutils'],
author="Mihai Ibanescu",
author_email="[email protected]",
url="https://github.com/sassoftware/python-keyutils",
license="Apache 2.0",
packages=["keyutils"],
classifiers=[
"Topic :: Security",
"Operating System :: POSIX :: Linux",
Expand All @@ -45,15 +47,15 @@
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
],
platforms=[
"Linux",
],
],
ext_modules=[
Extension(
'keyutils._keyutils',
['keyutils/_keyutils.pyx'],
libraries=['keyutils'],
"keyutils._keyutils",
["keyutils/_keyutils.pyx"],
libraries=["keyutils"],
),
],
setup_requires=[],
Expand Down
41 changes: 21 additions & 20 deletions test/keyutils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import keyutils


class BasicTest(unittest.TestCase):
def testSet(self):
keyDesc = b"test:key:01"
Expand All @@ -36,7 +37,7 @@ def testSet(self):
try:
keyutils.read_key(12345)
except keyutils.Error as e:
self.assertEqual(e.args, (126, 'Required key not available'))
self.assertEqual(e.args, (126, "Required key not available"))

keyutils.add_key(keyDesc, keyVal, keyring)
keyId = keyutils.request_key(keyDesc, keyring)
Expand All @@ -49,17 +50,16 @@ def testSession(self):
val = b"asdfasdfasdf"
session = keyutils.join_session_keyring()
keyId = keyutils.add_key(desc, val, session)
self.assertEqual(keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING,
desc), keyId)
self.assertEqual(
keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING, desc), keyId
)
keyutils.join_session_keyring()
self.assertEqual(keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING,
desc), None)
self.assertEqual(keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING, desc), None)

def testRevoke(self):
desc = b"dummy"
session = keyutils.join_session_keyring()
self.assertEqual(keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING,
desc), None)
self.assertEqual(keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING, desc), None)
keyutils.revoke(session)
try:
keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING, desc)
Expand All @@ -77,8 +77,8 @@ def testRevoke(self):
pid, exitcode = os.waitpid(childpid, 0)
self.assertEqual(childpid, pid)
self.assertTrue(
os.WIFEXITED(exitcode) and os.WEXITSTATUS(exitcode) == 0,
exitcode)
os.WIFEXITED(exitcode) and os.WEXITSTATUS(exitcode) == 0, exitcode
)
else:
rc = 1
try:
Expand All @@ -88,14 +88,16 @@ def testRevoke(self):
finally:
os._exit(rc)

self.assertEqual(keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING,
desc), None)

self.assertEqual(keyutils.search(keyutils.KEY_SPEC_SESSION_KEYRING, desc), None)

def testLink(self):
desc = b"key1"
child = keyutils.add_key(b"ring1", None, keyutils.KEY_SPEC_PROCESS_KEYRING, b"keyring")
parent = keyutils.add_key(b"ring2", None, keyutils.KEY_SPEC_PROCESS_KEYRING, b"keyring")
child = keyutils.add_key(
b"ring1", None, keyutils.KEY_SPEC_PROCESS_KEYRING, b"keyring"
)
parent = keyutils.add_key(
b"ring2", None, keyutils.KEY_SPEC_PROCESS_KEYRING, b"keyring"
)
keyId = keyutils.add_key(desc, b"dummy", child)
self.assertEqual(keyutils.search(child, desc), keyId)
self.assertEqual(keyutils.search(parent, desc), None)
Expand Down Expand Up @@ -140,7 +142,7 @@ def testDescribe(self):
key_id = keyutils.add_key(desc, value, keyring)

ret = keyutils.describe_key(key_id)
ktype, _, _, kperm, kdesc = ret.split(b';', 4)
ktype, _, _, kperm, kdesc = ret.split(b";", 4)
self.assertEqual(ktype, b"user")
self.assertEqual(desc, kdesc)

Expand All @@ -162,16 +164,15 @@ def testSetPerm(self):

key_id = keyutils.add_key(desc, value, keyring)

ktype, _, _, kperm, kdesc = keyutils.describe_key(key_id).split(b';', 4)
ktype, _, _, kperm, kdesc = keyutils.describe_key(key_id).split(b";", 4)
kperm = int(kperm, base=16)
self.assertEqual(
keyutils.KEY_POS_READ, kperm & keyutils.KEY_POS_READ)
self.assertEqual(keyutils.KEY_POS_READ, kperm & keyutils.KEY_POS_READ)
keyutils.set_perm(key_id, kperm - keyutils.KEY_POS_READ)

ktype, _, _, kperm, kdesc = keyutils.describe_key(key_id).split(b';', 4)
ktype, _, _, kperm, kdesc = keyutils.describe_key(key_id).split(b";", 4)
kperm = int(kperm, base=16)
self.assertEqual(0, kperm & keyutils.KEY_POS_READ)


if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(unittest.main())
35 changes: 35 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,38 @@ deps = radon
commands =
radon cc -s --total-average --no-assert -nb keyutils/
radon mi -m -s keyutils/

[flake8]
max_line_length = 180
hang-closing = true
ignore =
W293
W503
D412
D105
W191
E133
per-file-ignores =
setup.py:E501
tests/*:D102,D103,D100
docstring-convention = numpy

[isort]
profile=black
line_length = 88
multi_line_output = 3
include_trailing_comma = true
lines_after_imports = 2
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
#known_future_library=future,pies
extra_standard_library=typing_extensions
known_first_party =
keyutils
usage
conftest
tests
# you should add here your known thirdparties, it will facilitate
# the job to isort
known_third_party =
hypothesis
pytest

0 comments on commit f9af1b0

Please sign in to comment.