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 | |
| 17 | import subprocess |
| 18 | import os |
| 19 | import sys |
| 20 | import importlib |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 21 | import time |
Neels Hofmeyr | 119bed5 | 2017-02-28 02:44:25 +0100 | [diff] [blame] | 22 | import unittest |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 23 | |
| 24 | |
| 25 | """Run a command, with stdout and stderr directed to devnull""" |
| 26 | |
Neels Hofmeyr | f6ab3d8 | 2017-02-24 20:49:51 +0100 | [diff] [blame] | 27 | devnull = None |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 28 | |
Neels Hofmeyr | 3ef39e9 | 2017-02-24 20:49:27 +0100 | [diff] [blame] | 29 | def popen_devnull(cmd, verbose=True): |
Neels Hofmeyr | f6ab3d8 | 2017-02-24 20:49:51 +0100 | [diff] [blame] | 30 | global devnull |
| 31 | if devnull is None: |
| 32 | if verbose: |
| 33 | print "Opening /dev/null" |
| 34 | devnull = open(os.devnull, 'w') |
Neels Hofmeyr | 3ef39e9 | 2017-02-24 20:49:27 +0100 | [diff] [blame] | 35 | if verbose: |
| 36 | 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] | 37 | return subprocess.Popen(cmd, stdout=devnull, stderr=devnull) |
| 38 | |
| 39 | |
| 40 | """End a process. |
| 41 | |
| 42 | If the process doesn't appear to exist (for instance, is None), do nothing""" |
| 43 | |
| 44 | |
| 45 | def end_proc(proc): |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 46 | if not proc: |
| 47 | return |
| 48 | |
| 49 | proc.terminate() |
Neels Hofmeyr | 9b0a51f | 2017-02-27 02:38:42 +0100 | [diff] [blame] | 50 | time_to_wait_for_term = 5 |
| 51 | wait_step = 0.001 |
| 52 | waited_time = 0 |
| 53 | while True: |
| 54 | # poll returns None if proc is still running |
| 55 | rc = proc.poll() |
| 56 | if rc is not None: |
| 57 | break |
| 58 | waited_time += wait_step |
| 59 | # make wait_step approach 1.0 |
| 60 | wait_step = (1. + 5. * wait_step) / 6. |
| 61 | if waited_time >= time_to_wait_for_term: |
| 62 | break |
| 63 | time.sleep(wait_step) |
| 64 | |
| 65 | if proc.poll() is None: |
| 66 | # termination seems to be slower than that, let's just kill |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 67 | proc.kill() |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 68 | print "Killed child process" |
Neels Hofmeyr | 9b0a51f | 2017-02-27 02:38:42 +0100 | [diff] [blame] | 69 | elif waited_time > .002: |
| 70 | print "Terminating took %.3fs" % waited_time |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 71 | proc.wait() |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 72 | |
| 73 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 74 | """Add a directory to sys.path, try to import a config file.""" |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 75 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 76 | def importappconf_or_quit(dirname, confname, p_set): |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 77 | if dirname not in sys.path: |
| 78 | sys.path.append(dirname) |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 79 | try: |
| 80 | return importlib.import_module(confname) |
| 81 | except ImportError as e: |
| 82 | if p_set: |
| 83 | print >> sys.stderr, "osmoappdesc not found in %s" % dirname |
| 84 | else: |
| 85 | print >> sys.stderr, "set osmoappdesc location with -p <dir>" |
| 86 | sys.exit(1) |
Neels Hofmeyr | 119bed5 | 2017-02-28 02:44:25 +0100 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def pick_tests(suite, *name_snippets): |
| 90 | '''for unittest: Non-standard way of picking only selected tests to run, |
| 91 | by name. Kind of stupid of python unittest to not provide this feature |
| 92 | more easily.''' |
| 93 | |
| 94 | new_tests = [] |
| 95 | for t in suite._tests: |
| 96 | if isinstance(t, unittest.suite.TestSuite): |
| 97 | pick_tests(t, *name_snippets) |
| 98 | new_tests.append(t) |
| 99 | continue |
| 100 | |
| 101 | if not isinstance(t, unittest.TestCase): |
| 102 | new_tests.append(t) |
| 103 | continue |
| 104 | |
| 105 | if any([n.lower() in t._testMethodName.lower() for n in name_snippets]): |
| 106 | new_tests.append(t) |
| 107 | suite._tests = new_tests |