blob: 7b200431cf5ecf7b72d4794e8405f5c7be25cf20 [file] [log] [blame]
Kata7185c62013-04-04 17:31:13 +02001#!/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
17import subprocess
18import os
19import sys
20import importlib
Neels Hofmeyrb59b6772017-02-24 20:49:39 +010021import time
Kata7185c62013-04-04 17:31:13 +020022
23
24"""Run a command, with stdout and stderr directed to devnull"""
25
26
Neels Hofmeyr3ef39e92017-02-24 20:49:27 +010027def popen_devnull(cmd, verbose=True):
Kata7185c62013-04-04 17:31:13 +020028 devnull = open(os.devnull, 'w')
Neels Hofmeyr3ef39e92017-02-24 20:49:27 +010029 if verbose:
30 print "Launching: PWD=%s %s" % (os.getcwd(), ' '.join([repr(c) for c in cmd]))
Kata7185c62013-04-04 17:31:13 +020031 return subprocess.Popen(cmd, stdout=devnull, stderr=devnull)
32
33
34"""End a process.
35
36If the process doesn't appear to exist (for instance, is None), do nothing"""
37
38
39def end_proc(proc):
Neels Hofmeyrb59b6772017-02-24 20:49:39 +010040 if not proc:
41 return
42
43 proc.terminate()
44 time.sleep(.1)
45 rc = proc.poll()
46 if rc is not None:
47 print "Terminated child process"
48 else:
Kata7185c62013-04-04 17:31:13 +020049 proc.kill()
Neels Hofmeyrb59b6772017-02-24 20:49:39 +010050 print "Killed child process"
51 proc.wait()
Kata7185c62013-04-04 17:31:13 +020052
53
Kat0270be42013-04-05 21:34:52 +020054"""Add a directory to sys.path, try to import a config file."""
Kata7185c62013-04-04 17:31:13 +020055
Kat0270be42013-04-05 21:34:52 +020056def importappconf_or_quit(dirname, confname, p_set):
Kata7185c62013-04-04 17:31:13 +020057 if dirname not in sys.path:
58 sys.path.append(dirname)
Kat0270be42013-04-05 21:34:52 +020059 try:
60 return importlib.import_module(confname)
61 except ImportError as e:
62 if p_set:
63 print >> sys.stderr, "osmoappdesc not found in %s" % dirname
64 else:
65 print >> sys.stderr, "set osmoappdesc location with -p <dir>"
66 sys.exit(1)