#!/usr/bin/python3

# Copyright 2019 Fiona Klute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Python modules for the mod_gnutls test suite."""

import sys

import fcntl
from contextlib import contextmanager

class TestExpectationFailed(Exception):
    """Raise if a test failed. The constructor should be called with a
    string describing the problem."""
    pass



@contextmanager
def lockfile(file, nolock=False):
    """Context manager for an optional file-based mutex.

    Unless nolock=True the process must hold a lock on the given file
    before entering the context. The lock is released when leaving the
    context.

    """
    if nolock:
        yield
    else:
        with open(file, 'w') as lockfile:
            try:
                print(f'Aquiring lock on {file}...', file=sys.stderr)
                fcntl.flock(lockfile, fcntl.LOCK_EX)
                print(f'Got lock on {file}.', file=sys.stderr)
                yield
            finally:
                print(f'Unlocking {file}...', file=sys.stderr)
                fcntl.flock(lockfile, fcntl.LOCK_UN)
                print(f'Unlocked {file}.', file=sys.stderr)

#!/usr/bin/python3

# Copyright 2019 Fiona Klute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Python modules for the mod_gnutls test suite."""

import sys

import fcntl
from contextlib import contextmanager

class TestExpectationFailed(Exception):
    """Raise if a test failed. The constructor should be called with a
    string describing the problem."""
    pass



@contextmanager
def lockfile(file, nolock=False):
    """Context manager for an optional file-based mutex.

    Unless nolock=True the process must hold a lock on the given file
    before entering the context. The lock is released when leaving the
    context.

    """
    if nolock:
        try:
            yield None
        finally:
            pass
    else:
        with open(file, 'w') as lockfile:
            try:
                print(f'Aquiring lock on {file}...', file=sys.stderr)
                fcntl.flock(lockfile, fcntl.LOCK_EX)
                print(f'Got lock on {file}.', file=sys.stderr)
                yield lockfile
            finally:
                print(f'Unlocking {file}...', file=sys.stderr)
                fcntl.flock(lockfile, fcntl.LOCK_UN)
                print(f'Unlocked {file}.', file=sys.stderr)

Use try/finally in lockfile context even when locking is disabled