Skip to content
Snippets Groups Projects
sourcedb.py 2.27 KiB
Newer Older
from __future__ import with_statement
from subprocess import Popen, CalledProcessError, PIPE, STDOUT
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):
    def run(self, executable, catalogue, skydb):
            # 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
            shutil.rmtree(skydb, ignore_errors=True)
            self.logger.info("Creating skymodel: %s" % (skydb))
            scratch_dir = tempfile.mkdtemp()
                cmd = [executable,
                       "format=<",
                       "in=%s" % (catalogue),
                       "out=%s" % (skydb)
                      ]
                    scratch_dir,
                    self.logger.name + "." + os.path.basename(skydb),
                    os.path.basename(executable)
                ) as logger:
                    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:

        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())