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 |
| 21 | |
| 22 | |
| 23 | """Run a command, with stdout and stderr directed to devnull""" |
| 24 | |
| 25 | |
| 26 | def popen_devnull(cmd): |
| 27 | devnull = open(os.devnull, 'w') |
| 28 | return subprocess.Popen(cmd, stdout=devnull, stderr=devnull) |
| 29 | |
| 30 | |
| 31 | """End a process. |
| 32 | |
| 33 | If the process doesn't appear to exist (for instance, is None), do nothing""" |
| 34 | |
| 35 | |
| 36 | def end_proc(proc): |
| 37 | if proc: |
| 38 | proc.kill() |
| 39 | proc.wait() |
| 40 | |
| 41 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 42 | """Add a directory to sys.path, try to import a config file.""" |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 43 | |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 44 | def importappconf_or_quit(dirname, confname, p_set): |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 45 | if dirname not in sys.path: |
| 46 | sys.path.append(dirname) |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 47 | try: |
| 48 | return importlib.import_module(confname) |
| 49 | except ImportError as e: |
| 50 | if p_set: |
| 51 | print >> sys.stderr, "osmoappdesc not found in %s" % dirname |
| 52 | else: |
| 53 | print >> sys.stderr, "set osmoappdesc location with -p <dir>" |
| 54 | sys.exit(1) |