Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # (C) 2013 by Katerina Barone-Adesi <kat.obsc@gmail.com> |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 17 | from __future__ import print_function |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 18 | import subprocess |
| 19 | import os |
| 20 | import sys |
| 21 | import importlib |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 22 | import time |
Neels Hofmeyr | 119bed5 | 2017-02-28 02:44:25 +0100 | [diff] [blame] | 23 | import unittest |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 24 | |
| 25 | |
| 26 | """Run a command, with stdout and stderr directed to devnull""" |
| 27 | |
Neels Hofmeyr | f6ab3d8 | 2017-02-24 20:49:51 +0100 | [diff] [blame] | 28 | devnull = None |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 29 | |
Neels Hofmeyr | 3ef39e9 | 2017-02-24 20:49:27 +0100 | [diff] [blame] | 30 | def popen_devnull(cmd, verbose=True): |
Neels Hofmeyr | f6ab3d8 | 2017-02-24 20:49:51 +0100 | [diff] [blame] | 31 | global devnull |
| 32 | if devnull is None: |
| 33 | if verbose: |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 34 | print("Opening /dev/null") |
Neels Hofmeyr | f6ab3d8 | 2017-02-24 20:49:51 +0100 | [diff] [blame] | 35 | devnull = open(os.devnull, 'w') |
Neels Hofmeyr | 3ef39e9 | 2017-02-24 20:49:27 +0100 | [diff] [blame] | 36 | if verbose: |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 37 | print("Launching: PWD=%s %s" % (os.getcwd(), ' '.join([repr(c) for c in cmd]))) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 38 | return subprocess.Popen(cmd, stdout=devnull, stderr=devnull) |
| 39 | |
| 40 | |
| 41 | """End a process. |
| 42 | |
| 43 | If the process doesn't appear to exist (for instance, is None), do nothing""" |
| 44 | |
| 45 | |
| 46 | def end_proc(proc): |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 47 | if not proc: |
| 48 | return |
| 49 | |
| 50 | proc.terminate() |
Neels Hofmeyr | 9b0a51f | 2017-02-27 02:38:42 +0100 | [diff] [blame] | 51 | time_to_wait_for_term = 5 |
| 52 | wait_step = 0.001 |
| 53 | waited_time = 0 |
| 54 | while True: |
| 55 | # poll returns None if proc is still running |
| 56 | rc = proc.poll() |
| 57 | if rc is not None: |
| 58 | break |
| 59 | waited_time += wait_step |
| 60 | # make wait_step approach 1.0 |
| 61 | wait_step = (1. + 5. * wait_step) / 6. |
| 62 | if waited_time >= time_to_wait_for_term: |
| 63 | break |
| 64 | time.sleep(wait_step) |
| 65 | |
| 66 | if proc.poll() is None: |
| 67 | # termination seems to be slower than that, let's just kill |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 68 | proc.kill() |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 69 | print("Killed child process") |
Neels Hofmeyr | 9b0a51f | 2017-02-27 02:38:42 +0100 | [diff] [blame] | 70 | elif waited_time > .002: |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 71 | print("Terminating took %.3fs" % waited_time) |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 72 | proc.wait() |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 73 | |
| 74 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 75 | """Add a directory to sys.path, try to import a config file.""" |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 76 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 77 | def importappconf_or_quit(dirname, confname, p_set): |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 78 | if dirname not in sys.path: |
| 79 | sys.path.append(dirname) |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 80 | try: |
| 81 | return importlib.import_module(confname) |
| 82 | except ImportError as e: |
| 83 | if p_set: |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 84 | print("osmoappdesc not found in %s" % dirname, file=sys.stderr) |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 85 | else: |
Max | 6ccd078 | 2017-12-15 12:15:39 +0100 | [diff] [blame] | 86 | print("set osmoappdesc location with -p <dir>", file=sys.stderr) |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 87 | sys.exit(1) |
Neels Hofmeyr | 119bed5 | 2017-02-28 02:44:25 +0100 | [diff] [blame] | 88 | |
| 89 | |
| 90 | def pick_tests(suite, *name_snippets): |
| 91 | '''for unittest: Non-standard way of picking only selected tests to run, |
| 92 | by name. Kind of stupid of python unittest to not provide this feature |
| 93 | more easily.''' |
| 94 | |
| 95 | new_tests = [] |
| 96 | for t in suite._tests: |
| 97 | if isinstance(t, unittest.suite.TestSuite): |
| 98 | pick_tests(t, *name_snippets) |
| 99 | new_tests.append(t) |
| 100 | continue |
| 101 | |
| 102 | if not isinstance(t, unittest.TestCase): |
| 103 | new_tests.append(t) |
| 104 | continue |
| 105 | |
| 106 | if any([n.lower() in t._testMethodName.lower() for n in name_snippets]): |
| 107 | new_tests.append(t) |
| 108 | suite._tests = new_tests |