[6d3dc34] | 1 | #!/usr/bin/python3 |
---|
| 2 | |
---|
[0f65ea9] | 3 | # Copyright 2019-2020 Fiona Klute |
---|
[6d3dc34] | 4 | # |
---|
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 6 | # you may not use this file except in compliance with the License. |
---|
| 7 | # You may obtain a copy of the License at |
---|
| 8 | # |
---|
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | # |
---|
| 11 | # Unless required by applicable law or agreed to in writing, software |
---|
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | # See the License for the specific language governing permissions and |
---|
| 15 | # limitations under the License. |
---|
| 16 | |
---|
[0560bb9] | 17 | """Python modules for the mod_gnutls test suite.""" |
---|
| 18 | |
---|
[3deb86e] | 19 | import fcntl |
---|
[b1261cb] | 20 | import sys |
---|
| 21 | |
---|
| 22 | from contextlib import contextmanager |
---|
| 23 | |
---|
[6d3dc34] | 24 | class TestExpectationFailed(Exception): |
---|
| 25 | """Raise if a test failed. The constructor should be called with a |
---|
| 26 | string describing the problem.""" |
---|
| 27 | pass |
---|
[b1261cb] | 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | @contextmanager |
---|
| 32 | def lockfile(file, nolock=False): |
---|
| 33 | """Context manager for an optional file-based mutex. |
---|
| 34 | |
---|
| 35 | Unless nolock=True the process must hold a lock on the given file |
---|
| 36 | before entering the context. The lock is released when leaving the |
---|
| 37 | context. |
---|
| 38 | |
---|
| 39 | """ |
---|
| 40 | if nolock: |
---|
[d5c572b] | 41 | try: |
---|
| 42 | yield None |
---|
| 43 | finally: |
---|
| 44 | pass |
---|
[b1261cb] | 45 | else: |
---|
| 46 | with open(file, 'w') as lockfile: |
---|
| 47 | try: |
---|
| 48 | print(f'Aquiring lock on {file}...', file=sys.stderr) |
---|
| 49 | fcntl.flock(lockfile, fcntl.LOCK_EX) |
---|
| 50 | print(f'Got lock on {file}.', file=sys.stderr) |
---|
[d5c572b] | 51 | yield lockfile |
---|
[b1261cb] | 52 | finally: |
---|
| 53 | print(f'Unlocking {file}...', file=sys.stderr) |
---|
| 54 | fcntl.flock(lockfile, fcntl.LOCK_UN) |
---|
| 55 | print(f'Unlocked {file}.', file=sys.stderr) |
---|
[2d25c1c] | 56 | |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | def first_line_match(regexp, file): |
---|
| 60 | """Return the first match of the regular expression in file (by line), |
---|
| 61 | or None. Technically applicable to any iterable containing |
---|
| 62 | strings, not just files opened for reading. |
---|
| 63 | """ |
---|
| 64 | for line in file: |
---|
| 65 | m = regexp.search(line) |
---|
| 66 | if m: |
---|
| 67 | return m |
---|
| 68 | return None |
---|
[0f65ea9] | 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | def require_match(regexp, file, error_message=None): |
---|
| 73 | """Return the first match of the regular expression in file (by line), |
---|
| 74 | or raise TestExpectationFailed. |
---|
| 75 | |
---|
| 76 | If error_message is not None the exception message will be that |
---|
| 77 | string, otherwise a generic message containing the regular |
---|
| 78 | expression pattern. Technically applicable to any iterable |
---|
| 79 | containing strings, not just files opened for reading. |
---|
| 80 | |
---|
| 81 | """ |
---|
| 82 | m = first_line_match(regexp, file) |
---|
| 83 | if m: |
---|
| 84 | return m |
---|
| 85 | |
---|
| 86 | if error_message: |
---|
| 87 | raise TestExpectationFailed(error_message) |
---|
| 88 | else: |
---|
| 89 | raise TestExpectationFailed(f'No match found for {regexp.pattern}!') |
---|