Newer
Older
from __future__ import with_statement
from subprocess import Popen, CalledProcessError, PIPE, STDOUT

Marcel Loose
committed
import errno
import os
import tempfile

Marcel Loose
committed
import shutil
import sys
from lofarpipe.support.lofarnode import LOFARnodeTCP
from lofarpipe.support.utilities import log_time
from lofarpipe.support.pipelinelogging import CatchLog4CPlus
from lofarpipe.support.utilities import catch_segfaults
class sourcedb(LOFARnodeTCP):

Marcel Loose
committed
def run(self, executable, catalogue, skydb):
with log_time(self.logger):

Marcel Loose
committed
# Create output directory if it does not yet exist.
skydb_dir = os.path.dirname(skydb)
try:
os.makedirs(skydb_dir)
self.logger.debug("Created output directory %s" % skydb_dir)
except OSError, err:
# Ignore error if directory already exists, otherwise re-raise
if err[0] != errno.EEXIST:
raise
# Remove any old sky database

Marcel Loose
committed
shutil.rmtree(skydb, ignore_errors=True)

Marcel Loose
committed
self.logger.info("Creating skymodel: %s" % (skydb))
scratch_dir = tempfile.mkdtemp()
try:

Marcel Loose
committed
cmd = [executable,
"format=<",
"in=%s" % (catalogue),
"out=%s" % (skydb)
]
with CatchLog4CPlus(

Marcel Loose
committed
scratch_dir,
self.logger.name + "." + os.path.basename(skydb),
os.path.basename(executable)
) as logger:

Marcel Loose
committed
catch_segfaults(cmd, scratch_dir, None, logger)
except CalledProcessError, e:
# For CalledProcessError isn't properly propagated by IPython
# Temporary workaround...
self.logger.error(str(e))
return 1
finally:

Marcel Loose
committed
shutil.rmtree(scratch_dir)
return 0
if __name__ == "__main__":
# If invoked directly, parse command line arguments for logger information
# and pass the rest to the run() method defined above
# --------------------------------------------------------------------------
jobid, jobhost, jobport = sys.argv[1:4]
sys.exit(sourcedb(jobid, jobhost, jobport).run_with_stored_arguments())