2015-11-19 12:02:30 +00:00
|
|
|
import os
|
|
|
|
import errno
|
2015-11-18 16:39:03 +00:00
|
|
|
import json
|
2015-11-16 14:52:47 +00:00
|
|
|
import datetime
|
2015-11-26 12:15:02 +00:00
|
|
|
import sys
|
2015-12-11 21:27:51 +00:00
|
|
|
import subprocess
|
2016-03-04 19:35:35 +00:00
|
|
|
import string
|
2019-04-24 19:15:22 +00:00
|
|
|
import signal
|
2015-11-20 10:00:43 +00:00
|
|
|
|
|
|
|
from opendm import context
|
2015-11-26 12:15:02 +00:00
|
|
|
from opendm import log
|
2015-11-18 16:39:03 +00:00
|
|
|
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2015-11-18 16:39:03 +00:00
|
|
|
def get_ccd_widths():
|
|
|
|
"""Return the CCD Width of the camera listed in the JSON defs file."""
|
2016-03-04 19:35:35 +00:00
|
|
|
with open(context.ccd_widths_path) as f:
|
|
|
|
sensor_data = json.loads(f.read())
|
|
|
|
return dict(zip(map(string.lower, sensor_data.keys()), sensor_data.values()))
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2019-04-24 19:15:22 +00:00
|
|
|
running_subprocesses = []
|
2019-05-10 16:33:16 +00:00
|
|
|
cleanup_callbacks = []
|
|
|
|
|
|
|
|
def add_cleanup_callback(func):
|
|
|
|
global cleanup_callbacks
|
|
|
|
cleanup_callbacks.append(func)
|
|
|
|
|
|
|
|
def remove_cleanup_callback(func):
|
|
|
|
global cleanup_callbacks
|
|
|
|
|
|
|
|
try:
|
|
|
|
cleanup_callbacks.remove(func)
|
|
|
|
except ValueError as e:
|
|
|
|
log.ODM_EXCEPTION("Tried to remove %s from cleanup_callbacks but got: %s" % (str(func), str(e)))
|
|
|
|
|
|
|
|
def exit_gracefully():
|
2019-04-24 19:15:22 +00:00
|
|
|
global running_subprocesses
|
2019-05-10 16:33:16 +00:00
|
|
|
global cleanup_callbacks
|
2019-04-24 19:15:22 +00:00
|
|
|
|
|
|
|
log.ODM_WARNING("Caught TERM/INT signal, attempting to exit gracefully...")
|
|
|
|
|
2019-05-10 16:33:16 +00:00
|
|
|
for cb in cleanup_callbacks:
|
|
|
|
cb()
|
|
|
|
|
2019-04-24 19:15:22 +00:00
|
|
|
for sp in running_subprocesses:
|
|
|
|
log.ODM_WARNING("Sending TERM signal to PID %s..." % sp.pid)
|
|
|
|
os.killpg(os.getpgid(sp.pid), signal.SIGTERM)
|
2019-04-27 22:37:07 +00:00
|
|
|
|
2019-05-10 16:33:16 +00:00
|
|
|
os._exit(1)
|
|
|
|
|
|
|
|
def sighandler(signum, frame):
|
|
|
|
exit_gracefully()
|
2019-04-24 19:15:22 +00:00
|
|
|
|
2019-05-10 16:33:16 +00:00
|
|
|
signal.signal(signal.SIGINT, sighandler)
|
|
|
|
signal.signal(signal.SIGTERM, sighandler)
|
2016-10-24 23:47:38 +00:00
|
|
|
|
2020-09-09 17:23:53 +00:00
|
|
|
def run(cmd, env_paths=[context.superbuild_bin_path], env_vars={}, packages_paths=context.python_packages_paths):
|
2015-11-16 14:52:47 +00:00
|
|
|
"""Run a system command"""
|
2019-04-24 19:15:22 +00:00
|
|
|
global running_subprocesses
|
|
|
|
|
2019-06-28 15:10:08 +00:00
|
|
|
log.ODM_INFO('running %s' % cmd)
|
2017-06-29 14:55:43 +00:00
|
|
|
|
2019-04-12 17:58:25 +00:00
|
|
|
env = os.environ.copy()
|
2017-06-29 14:55:43 +00:00
|
|
|
if len(env_paths) > 0:
|
|
|
|
env["PATH"] = env["PATH"] + ":" + ":".join(env_paths)
|
2019-04-12 17:58:25 +00:00
|
|
|
|
2020-09-09 17:23:53 +00:00
|
|
|
if len(packages_paths) > 0:
|
|
|
|
env["PYTHONPATH"] = env.get("PYTHONPATH", "") + ":" + ":".join(packages_paths)
|
|
|
|
|
2019-04-12 17:58:25 +00:00
|
|
|
for k in env_vars:
|
|
|
|
env[k] = str(env_vars[k])
|
2017-06-29 14:55:43 +00:00
|
|
|
|
2019-04-24 19:15:22 +00:00
|
|
|
p = subprocess.Popen(cmd, shell=True, env=env, preexec_fn=os.setsid)
|
|
|
|
running_subprocesses.append(p)
|
|
|
|
retcode = p.wait()
|
|
|
|
running_subprocesses.remove(p)
|
2016-10-24 23:47:38 +00:00
|
|
|
if retcode < 0:
|
|
|
|
raise Exception("Child was terminated by signal {}".format(-retcode))
|
|
|
|
elif retcode > 0:
|
|
|
|
raise Exception("Child returned {}".format(retcode))
|
2015-11-16 14:52:47 +00:00
|
|
|
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2015-11-16 14:52:47 +00:00
|
|
|
def now():
|
|
|
|
"""Return the current time"""
|
|
|
|
return datetime.datetime.now().strftime('%a %b %d %H:%M:%S %Z %Y')
|
|
|
|
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2016-02-29 14:45:00 +00:00
|
|
|
def now_raw():
|
|
|
|
return datetime.datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
def benchmark(start, benchmarking_file, process):
|
|
|
|
"""
|
|
|
|
runs a benchmark with a start datetime object
|
|
|
|
:return: the running time (delta)
|
|
|
|
"""
|
|
|
|
# Write to benchmark file
|
|
|
|
delta = (datetime.datetime.now() - start).total_seconds()
|
|
|
|
with open(benchmarking_file, 'a') as b:
|
2016-02-29 14:47:54 +00:00
|
|
|
b.write('%s runtime: %s seconds\n' % (process, delta))
|
2016-02-29 14:45:00 +00:00
|
|
|
|
2015-11-16 14:52:47 +00:00
|
|
|
def mkdir_p(path):
|
2016-02-26 18:50:12 +00:00
|
|
|
"""Make a directory including parent directories.
|
|
|
|
"""
|
2015-11-16 14:52:47 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(path)
|
|
|
|
except os.error as exc:
|
|
|
|
if exc.errno != errno.EEXIST or not os.path.isdir(path):
|
|
|
|
raise
|
|
|
|
|
2019-04-24 22:33:12 +00:00
|
|
|
# Python2 shutil.which
|
|
|
|
def which(program):
|
|
|
|
path=os.getenv('PATH')
|
|
|
|
for p in path.split(os.path.pathsep):
|
|
|
|
p=os.path.join(p,program)
|
|
|
|
if os.path.exists(p) and os.access(p,os.X_OK):
|
|
|
|
return p
|