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 |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 22 | |
| 23 | |
| 24 | """Run a command, with stdout and stderr directed to devnull""" |
| 25 | |
Neels Hofmeyr | f6ab3d8 | 2017-02-24 20:49:51 +0100 | [diff] [blame^] | 26 | devnull = None |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 27 | |
Neels Hofmeyr | 3ef39e9 | 2017-02-24 20:49:27 +0100 | [diff] [blame] | 28 | def popen_devnull(cmd, verbose=True): |
Neels Hofmeyr | f6ab3d8 | 2017-02-24 20:49:51 +0100 | [diff] [blame^] | 29 | global devnull |
| 30 | if devnull is None: |
| 31 | if verbose: |
| 32 | print "Opening /dev/null" |
| 33 | devnull = open(os.devnull, 'w') |
Neels Hofmeyr | 3ef39e9 | 2017-02-24 20:49:27 +0100 | [diff] [blame] | 34 | if verbose: |
| 35 | 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] | 36 | return subprocess.Popen(cmd, stdout=devnull, stderr=devnull) |
| 37 | |
| 38 | |
| 39 | """End a process. |
| 40 | |
| 41 | If the process doesn't appear to exist (for instance, is None), do nothing""" |
| 42 | |
| 43 | |
| 44 | def end_proc(proc): |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 45 | if not proc: |
| 46 | return |
| 47 | |
| 48 | proc.terminate() |
| 49 | time.sleep(.1) |
| 50 | rc = proc.poll() |
| 51 | if rc is not None: |
| 52 | print "Terminated child process" |
| 53 | else: |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 54 | proc.kill() |
Neels Hofmeyr | b59b677 | 2017-02-24 20:49:39 +0100 | [diff] [blame] | 55 | print "Killed child process" |
| 56 | proc.wait() |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 57 | |
| 58 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 59 | """Add a directory to sys.path, try to import a config file.""" |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 60 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 61 | def importappconf_or_quit(dirname, confname, p_set): |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 62 | if dirname not in sys.path: |
| 63 | sys.path.append(dirname) |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 64 | try: |
| 65 | return importlib.import_module(confname) |
| 66 | except ImportError as e: |
| 67 | if p_set: |
| 68 | print >> sys.stderr, "osmoappdesc not found in %s" % dirname |
| 69 | else: |
| 70 | print >> sys.stderr, "set osmoappdesc location with -p <dir>" |
| 71 | sys.exit(1) |