Example #1
0
def run():
    
    # Set CASA_ENGINGE env. variable so that:
    # - taskinit does not initialize the viewer
    # - taskmanmager is not initialized
    os.environ['CASA_ENGINE']="YES"

    # Initialize MPI environment
    from mpi4casa.MPIEnvironment import MPIEnvironment

    # Initialize MPICommunicator singleton
    from mpi4casa.MPICommunicator import MPICommunicator
    communicator = MPICommunicator()

    # Wait to receive start service signal from MPI client processor
    start_service_signal_available = False
    while not start_service_signal_available:
        start_service_signal_available = communicator.control_service_request_probe()
        if start_service_signal_available:
            # Receive CASA global dictionary
            request = communicator.control_service_request_recv()
        else:
            time.sleep(MPIEnvironment.mpi_start_service_sleep_time)
      
    # Check if request is start or stop signal
    if request['signal'] == 'start':
    
        # Get CASA environment dictionary
        global casa
        casa = request['casa']
        global _casa_top_frame_
        _casa_top_frame_ = True
        
        # Re-set log file
        if request['logmode'] == 'separated' or request['logmode'] == 'redirect':
            casa['files']['logfile'] = '%s-server-%s-host-%s-pid-%s' % (casa['files']['logfile'],
                                                                       str(MPIEnvironment.mpi_processor_rank),
                                                                       str(MPIEnvironment.hostname),
                                                                       str(MPIEnvironment.mpi_pid))
    
        # Import logger, logfile is set at taskinit retrieving from the casa dict. from the stack
        from taskinit import casalog
        
        # Set log origin so that the processor origin is updated
        casalog_call_origin = "mpi4casapy"
        casalog.origin(casalog_call_origin)
        
        # If log mode is separated activate showconsole to have all logs sorted by time at the terminal
        if request['logmode'] == 'redirect':
            casalog.showconsole(True)
            
        # Install filter to remove MSSelectionNullSelection errors
        casalog.filter('NORMAL1')
        casalog.filterMsg('MSSelectionNullSelection')
        casalog.filterMsg('non-existent spw')
        
        # Post MPI welcome msg
        casalog.post(MPIEnvironment.mpi_info_msg,"INFO",casalog_call_origin)
    
        # Initialize MPICommandServer and start serving
        from mpi4casa.MPICommandServer import MPICommandServer
        server = MPICommandServer()
                        
        server.serve()
        
    else:
        
        MPIEnvironment.finalize_mpi_environment()
    
        
Example #2
0
from taskinit import casalog
import sys
import os

casalog.showconsole(onconsole=True)

print sys.argv
i = sys.argv.index("-c")
data_dir = [sys.argv[i + 2]]
work_dir = sys.argv[i + 3]
test_name = sys.argv[i + 4]
if sys.argv[i + 5] != "0":
    p = True
else:
    p = False

os.chdir(work_dir)
sys.path.append(work_dir + '/admin/')
import publish_summary

try:
    publish_summary.runTest(test_name,
                            data_dir,
                            work_dir + '/work',
                            work_dir + '/result',
                            CPP_PROFILE=p)
except:
    print "runTest execution failed"
    raise
    exit()  # sys.exit(1) does not work with IPython
exit()
Example #3
0
def run():

    # Set CASA_ENGINGE env. variable so that:
    # - taskinit does not initialize the viewer
    # - taskmanmager is not initialized
    os.environ['CASA_ENGINE'] = "YES"

    # Initialize MPI environment
    from mpi4casa.MPIEnvironment import MPIEnvironment

    # Initialize MPICommunicator singleton
    from mpi4casa.MPICommunicator import MPICommunicator
    communicator = MPICommunicator()

    # Wait to receive start service signal from MPI client processor
    start_service_signal_available = False
    while not start_service_signal_available:
        start_service_signal_available = communicator.control_service_request_probe(
        )
        if start_service_signal_available:
            # Receive CASA global dictionary
            request = communicator.control_service_request_recv()
        else:
            time.sleep(MPIEnvironment.mpi_start_service_sleep_time)

    # Check if request is start or stop signal
    if request['signal'] == 'start':

        # Get CASA environment dictionary
        global casa
        casa = request['casa']
        global _casa_top_frame_
        _casa_top_frame_ = True

        # Re-set log file
        if request['logmode'] == 'separated' or request[
                'logmode'] == 'redirect':
            casa['files']['logfile'] = '%s-server-%s-host-%s-pid-%s' % (
                casa['files']['logfile'], str(
                    MPIEnvironment.mpi_processor_rank),
                str(MPIEnvironment.hostname), str(MPIEnvironment.mpi_pid))

        # Import logger, logfile is set at taskinit retrieving from the casa dict. from the stack
        from taskinit import casalog

        # Set log origin so that the processor origin is updated
        casalog_call_origin = "mpi4casapy"
        casalog.origin(casalog_call_origin)

        # If log mode is separated activate showconsole to have all logs sorted by time at the terminal
        if request['logmode'] == 'redirect':
            casalog.showconsole(True)

        # Install filter to remove MSSelectionNullSelection errors
        casalog.filter('NORMAL1')
        casalog.filterMsg('MSSelectionNullSelection')
        casalog.filterMsg('non-existent spw')

        # Post MPI welcome msg
        casalog.post(MPIEnvironment.mpi_info_msg, "INFO", casalog_call_origin)

        # Initialize MPICommandServer and start serving
        from mpi4casa.MPICommandServer import MPICommandServer
        server = MPICommandServer()

        server.serve()

    else:

        MPIEnvironment.finalize_mpi_environment()
Example #4
0
def run():

    # Set CASA_ENGINGE env. variable so that:
    # - taskinit does not initialize the viewer
    # - taskmanmager is not initialized
    os.environ['CASA_ENGINE'] = "YES"

    # Initialize MPI environment
    from mpi4casa.MPIEnvironment import MPIEnvironment

    # Initialize MPICommunicator singleton
    from mpi4casa.MPICommunicator import MPICommunicator
    communicator = MPICommunicator()

    # Wait to receive start service signal from MPI client processor
    start_service_signal_available = False
    while not start_service_signal_available:
        start_service_signal_available = communicator.control_service_request_probe(
        )
        if start_service_signal_available:
            # Receive CASA global dictionary (filtered)
            request = communicator.control_service_request_recv()
        else:
            time.sleep(MPIEnvironment.mpi_start_service_sleep_time)

    # Check if request is start or stop signal
    if request['signal'] == 'start':

        cli_logfile_name, logmode_name = 'casa_filtered', 'logmode'
        for entry_name in [cli_logfile_name, logmode_name]:
            if entry_name not in request:
                raise RuntimeError(
                    'A \'start\' MPI request must have a {0} entry but it '
                    'was not found. Host name: {1}. MPI rank: {2}, '.format(
                        entry_name, MPIEnvironment.hostname,
                        MPIEnvironment.mpi_processor_rank))

        # Get CASA environment dictionary
        global casa
        casa_filtered = request['casa_filtered']
        casa.update(casa_filtered)
        global _casa_top_frame_
        _casa_top_frame_ = True

        # Re-set log file
        if request[logmode_name] == 'separated' or request[
                logmode_name] == 'redirect':
            casa['files']['logfile'] = (
                '{0}-server-{1}-host-{2}-pid-{3}'.format(
                    casa['files']['logfile'],
                    MPIEnvironment.mpi_processor_rank, MPIEnvironment.hostname,
                    MPIEnvironment.mpi_pid))

        # Import logger, logfile is set at taskinit retrieving from the casa dict. from the stack
        from taskinit import casalog

        # Set log origin so that the processor origin is updated
        casalog_call_origin = "mpi4casapy"
        casalog.origin(casalog_call_origin)

        # If log mode is separated activate showconsole to have all logs sorted by time at the terminal
        if request[logmode_name] == 'redirect':
            casalog.showconsole(True)

        # Install filter to remove MSSelectionNullSelection errors
        casalog.filter('NORMAL1')
        casalog.filterMsg('MSSelectionNullSelection')
        casalog.filterMsg('non-existent spw')

        # Post MPI welcome msg
        casalog.post(MPIEnvironment.mpi_info_msg, "INFO", casalog_call_origin)

        # Initialize MPICommandServer and start serving
        from mpi4casa.MPICommandServer import MPICommandServer
        server = MPICommandServer()

        server.serve()

    else:

        MPIEnvironment.finalize_mpi_environment()
Example #5
0
    def __init__(self, test, \
                 DATA_REPOS=[AIPS_DIR+'/data'], \
                 WORKING_DIR='/tmp/casa_regression_work/', \
                 RESULT_DIR='/tmp/casa_regression_result/', \
                 retemplate=False,
                 cleanup=True,
                 CPP_PROFILE=False,
                 RESULT_SUBDIR='',
                 REDIRECT=True,
                 PY_PROFILE=True):
        """cleanup: set to False to keep data around.
        WORKING_DIR: directory for intermediate files
        RESULT_DIR: directory where final products go
        CPP_PROFILE: set to True to enable C++ profiling.  This requires that the command 'sudo opcontrol' must work.  You also need the 'dot' tool distributed as part of graphviz.  Run 'dot -Txxx' to verify that your dot installation supports PNG images.
        Note, a profile is created only for the casapy process. If you want to include profiles for async / child processes, refer to the documentation for opreport."""
        casalog.showconsole(onconsole=True)

        TEMPLATE_RESULT_DIR = AIPS_DIR + '/data/regression/'
        tests = [test]
        if type(tests) != type([]):
            raise TypeError
        self.resultdir = RESULT_DIR
        self.imdir = WORKING_DIR + '/IMAGES/'
        self.tester = testbase(WORKING_DIR)
        self.imagertests = []
        self.result = []
        self.numTests = 0
        ####Get the directories right
        self.tester.setDataBaseDir(DATA_REPOS)
        self.tester.setScriptsDir(SCRIPT_REPOS)
        self.tester.setResultDir(RESULT_DIR)
        self.tester.setWorkingDir(WORKING_DIR)
        self.resultsubdir = ''

        print SCRIPT_REPOS

        if ((len(tests) == 1) and (tests[0] == 'all')):
            self.numTests = self.tester.locateTests()
        else:
            self.numTests = self.tester.locateTests(tests)
        testName = ''

        #pdb.set_trace()
        for k in range(self.numTests):
            ### cleanup before each test
            if not dry and cleanup:
                self.tester.cleanup()

            self.tester.createDirs()

            uname1 = os.uname()[1]

            if self.tester.testname(k)[0:6] == 'tests/':
                testName = string.split(self.tester.testname(k)[6:], ".py")[0]
            else:
                testName = string.split(self.tester.testname(k), ".py")[0]
            if not RESULT_SUBDIR:
                self.resultsubdir = self.resultdir + "/result-" + \
                                    testName       + "-" + \
                                    uname1         + "-" + \
                                    time.strftime('%Y_%m_%d_%H_%M')
            else:
                self.resultsubdir = self.resultdir + "/" + RESULT_SUBDIR

            if not os.path.isdir(self.resultsubdir):
                os.mkdir(self.resultsubdir)

            logfilename = testName + '.log'
            if (os.path.isfile(self.resultsubdir + '/' + logfilename)):
                os.remove(self.resultsubdir + '/' + logfilename)

            # redirect stdout and stderr and casalog
            print 'Run test ' + testName
            if REDIRECT:
                print "Redirect stdout/stderr to", self.resultsubdir + '/' + logfilename
                save_stdout = sys.stdout
                save_stderr = sys.stderr
                fsock = open(self.resultsubdir + '/' + logfilename, 'w')
                sys.stdout = logger("STDOUT", [save_stdout, fsock])
                sys.stderr = logger("STDERR", [save_stderr, fsock])

                testlog = self.tester.workingDirectory + "/test.log"
                open(testlog, "w").close()  # create empty file
                casalog.setlogfile(
                    testlog)  # seems to append to an existing file

            try:
                self.tester.getTest(self.tester.testname(k), testName)

                if PY_PROFILE:
                    if RESULT_SUBDIR != testName:
                        profilepage = RESULT_DIR + '/' + time.strftime(
                            '%Y_%m_%d/') + testName + '_profile.html'
                    else:
                        profilepage = RESULT_DIR + '/' + RESULT_SUBDIR + '/' + 'profile.html'

                    process_data = "%s/profile.txt" % self.tester.workingDirectory

                    os.system("echo -n > " + process_data)
                    pp = SCRIPT_REPOS + '/profileplot.py'  # for release
                    pyt = sys.executable
                    if not os.path.isfile(pp):
                        pp = SCRIPT_REPOS + '/../profileplot.py'  # for devel
                    profileplot_pid = os.spawnlp(
                        os.P_NOWAIT, pyt, pyt, pp, testName, RESULT_DIR +
                        ("/" +
                         RESULT_SUBDIR if RESULT_SUBDIR == testName else ''),
                        profilepage, process_data, str(os.getpid()))
                    prof = cProfile.Profile()
                else:
                    prof = False

                presentDir = os.getcwd()
                os.chdir(self.tester.workingDirectory)

                short_description = self.tester.getDescription(testName, k)
                if short_description != None and short_description.find(
                        "'") >= 0:
                    print >> sys.stderr, \
                          "Warning: Short description contains ': '%s'" % \
                          short_description
                    short_description = short_description.replace("'", "")

                try:
                    self.op_init(CPP_PROFILE)
                    time1 = time.time()
                    mem1 = commands.getoutput('env -i ps -p ' +
                                              str(os.getpid()) +
                                              ' -o rss | tail -1')
                    if prof:
                        #prof.runctx("(leResult, leImages)=self.tester.runtests(testName, k, dry)", globals(), locals())
                        #prof.runctx("(leResult, leImages)=self.tester.runtests(testName, k, dry)", gl, lo)
                        #prof.run("(leResult, leImages) = self.tester.runtests(testName, k, dry)")
                        (leResult,
                         leImages) = prof.runcall(self.tester.runtests,
                                                  testName, k, dry)
                    else:
                        (leResult,
                         leImages) = self.tester.runtests(testName, k, dry)

                    # returns absolute_paths, relative_paths
                    exec_success = True
                except:
                    leResult = []
                    exec_success = False
                    print >> sys.stderr, "%s failed, dumping traceback:" % testName
                    traceback.print_exc()  # print and swallow exception

                mem2 = commands.getoutput('env -i ps -p ' + str(os.getpid()) +
                                          ' -o rss | tail -1')
                time2 = time.time()
                time2 = (time2 - time1) / 60.0

                print "Net memory allocated:", (int(mem2) -
                                                int(mem1)) / 1024, "MB"

                if prof:
                    try:
                        prof.dump_stats(self.resultsubdir +
                                        '/cProfile.profile')
                    except:
                        print >> sys.stderr, "Failed to write profiling data!"

                self.op_done(CPP_PROFILE)

                # Dump contents of any *.log file produced
                # by the regression script
                #
                # !! Does not handle out of diskspace
                #
                files = os.listdir('.')
                for f in files:
                    if f != 'casa.log' and \
                           re.compile('.log$').search(f) != None:

                        for line in open(f, 'r'):
                            #print f + '    ddd'+line
                            if REDIRECT:
                                fsock.write(f + ': ' + line.rstrip())
                            else:
                                print f + ': ' + line.rstrip()

                #
                # Report and deal with out of diskspace
                #
                space_left = commands.getoutput( \
                    "df -kP " + self.tester.workingDirectory + \
                    " | awk '{print $4}' | tail -1")
                space_left_h = commands.getoutput( \
                    "df -hP " + self.tester.workingDirectory + \
                    " | awk '{print $4}' | tail -1")
                space_used = commands.getoutput( \
                    "du -kc " + self.tester.workingDirectory + \
                    " | tail -1 | awk '{print $1}'")
                space_used_h = commands.getoutput( \
                    "du -hc " + self.tester.workingDirectory + \
                    " | tail -1 | awk '{print $1}'")

                if int(space_left) < 1000 * 1000:
                    print >> sys.stderr, "Warning: Only " + \
                          space_left_h + ' disk space left, ' + \
                          space_used_h + ' used'
                    # Clean up early, so that this infrastructure can continue
                    if not exec_success and cleanup:
                        self.tester.cleanup()

                # Copy C++ profiling info
                if CPP_PROFILE:
                    os.system('cp cpp_profile.* ' + self.resultsubdir)

                os.chdir(presentDir)

                if PY_PROFILE:
                    # Terminate profiling process
                    os.kill(profileplot_pid, signal.SIGHUP)
                    status = os.waitpid(profileplot_pid, 0)[1]
                    #print str(profileplot_pid) + ' exit: ' + str(status)

                pagename = time.strftime(
                    '%Y_%m_%d/') + testName + '_profile.html'

                # entries common for all tests based on this run
                self.result_common = {}
                self.result_common['CASA'] = "'" + self.get_casa_version(
                ) + "'", "CASA version"
                self.result_common['host'] = uname1, "os.uname[1]"
                self.result_common['platform'] = "'" + self.get_platform(
                )[0] + " " + self.get_platform()[1] + "'", "OS"

                self.result_common['date'] = time.strftime(
                    '%Y_%m_%d_%H_%M'), ""
                self.result_common['testid'] = testName, "test name"
                if short_description != None:
                    self.result_common[
                        'description'] = "'" + short_description + "'", "test short description"

                # Figure out data repository version
                if os.system("which svnversion >/dev/null") == 0:
                    (errorcode, datasvnr) = commands.getstatusoutput(
                        'cd ' + DATA_REPOS[0] +
                        ' && svnversion 2>&1 | grep -vi warning')
                else:
                    errorcode = 1
                if errorcode != 0 or datasvnr == "exported":
                    # If that didn't work, look at ./version in the toplevel dir
                    (errorcode, datasvnr) = commands.getstatusoutput( \
                        'cd '+DATA_REPOS[0]+" && grep -E 'Rev:' version" \
                        )
                    if errorcode != 0:
                        datasvnr = "Unknown version"

                self.result_common[
                    'data_version'] = "'" + datasvnr + "'", "Data repository version"

                # execution test
                exec_result = self.result_common.copy()
                exec_result['version'] = 2, "version of this file"
                exec_result['type'] = "exec", "test type"
                exec_result['time'] = time2 * 60, "execution time in seconds"
                exec_result[
                    'disk'] = space_used, "disk space (KB) in use after test"
                exec_result['runlog'] = logfilename, "execution logfile"

                if PY_PROFILE:
                    # read time/memory data
                    mem = ""
                    try:
                        process_file = open(process_data, "r")
                    except:
                        print "Warning: Failed to open file:", process_data
                        process_file = None
                else:
                    process_file = None

                if process_file != None:
                    lineno = 0
                    for line in process_file:
                        lineno += 1
                        if len(line) > 0 and line[0] != '#':
                            try:
                                (t, m_virtual, m_resident, nfiledesc, cpu_us,
                                 cpu_sy, cpu_id, cpu_wa) = line.split()
                                mem = mem + \
                                      str(t)          + ',' + \
                                      str(m_virtual)  + ',' + \
                                      str(m_resident) + ',' + \
                                      str(nfiledesc)  + ',' + \
                                      str(cpu_us)     + ',' + \
                                      str(cpu_sy)     + ',' + \
                                      str(cpu_id)     + ',' + \
                                      str(cpu_wa)     + ';'
                            except:
                                print >> sys.stderr, "Error parsing %s:%d: '%s'" % \
                                      (process_data, lineno, line)
                    process_file.close()
                    exec_result[
                        'resource'] = mem, "time(s),virtual(Mbytes),resident(Mbytes),nfiledesc,cpu_us,cpu_sy,cpu_id,cpu_wa"

                whatToTest = self.tester.whatQualityTest()
                keys = []
                #if len(whatToTest) != 0:
                #    keys=whatToTest.keys()
                #    print 'THE KEYS ARE ', keys
                for j in range(len(leResult)):
                    templateImage = TEMPLATE_RESULT_DIR + "/" + testName + "/reference/" + leImages[
                        j]
                    if retemplate:
                        if os.access(templateImage, os.F_OK):
                            shutil.rmtree(templateImage)
                    print 'TemplateImage ' + templateImage
                    print 'theImage ' + leResult[j]
                    print 'theImage ' + leImages[j]

                    product_exists = os.access(leResult[j], os.F_OK)
                    template_exists = os.access(templateImage, os.F_OK)

                    if product_exists and retemplate:
                        print 'Create template from', leResult[j]
                        if not os.path.isdir(TEMPLATE_RESULT_DIR + "/" +
                                             testName):
                            os.mkdir(TEMPLATE_RESULT_DIR + "/" + testName)
                        shutil.copytree(leResult[j], templateImage)

                    if not product_exists:
                        print >> sys.stderr, leResult[j], 'missing!'
                        exec_success = False
                        whatToTest[leResult[j]] = []

                    if not template_exists:
                        print >> sys.stderr, templateImage, 'missing!'

                    for leQualityTest in whatToTest[leResult[j]]:
                        print leResult[j] + ' WHAT : ', whatToTest[leResult[j]]
                        self.result = self.result_common.copy()
                        self.result['version'] = 1, "version of this file"
                        self.result['type'] = leQualityTest, "test type"
                        self.result['image'] = leImages[j], "test image"

                        if not product_exists:
                            self.result[
                                'status'] = 'fail', "result of regression test"
                            self.result[
                                'reason'] = "'Product image missing'", "reason of failure"
                        elif not template_exists:
                            self.result[
                                'status'] = 'fail', "result of regression test"
                            self.result[
                                'reason'] = "'Reference image missing'", "reason of failure"
                        else:
                            if os.access(self.imdir, os.F_OK):
                                shutil.rmtree(self.imdir)

                            if (leQualityTest == 'simple'):
                                self.simpleStats(leResult[j], templateImage,
                                                 testName, WORKING_DIR,
                                                 RESULT_DIR)
                            elif (leQualityTest == 'pol2'):
                                self.polImageTest(leResult[j], templateImage,
                                                  testName, WORKING_DIR,
                                                  RESULT_DIR, 2)
                            elif (leQualityTest == 'pol4'):
                                self.polImageTest(leResult[j], templateImage,
                                                  testName, WORKING_DIR,
                                                  RESULT_DIR, 4)
                            elif (leQualityTest == 'cube'):
                                self.cubeImageTest(leResult[j], templateImage,
                                                   testName, WORKING_DIR,
                                                   RESULT_DIR)
                            elif (leQualityTest == 'ms'):
                                self.visStats(leResult[j], templateImage,
                                              testName, WORKING_DIR,
                                              RESULT_DIR)


# RI add visStats method here, image ones use ImageTest so going to have to build a MSTest class
                            else:
                                self.polImageTest(leResult[j], templateImage,
                                                  testName, WORKING_DIR,
                                                  RESULT_DIR, 1)

                            # Pick up any images produced in test
                            if os.path.isdir(self.imdir):
                                i = 0
                                for image in os.listdir(self.imdir):
                                    i = i + 1
                                    shutil.copy(self.imdir + '/' + image, \
                                                self.resultsubdir+'/'+os.path.basename(image))
                                    self.result[
                                        'imagefile_' +
                                        str(i)] = "'" + os.path.basename(
                                            image
                                        ) + "'", 'regression image ' + str(i)

                        self.create_log(leImages[j].replace('/', '-'))

                # Create exec log now that we now if
                # required images were produced
                exec_result['status'] = (
                    "fail", "pass")[exec_success], "execution status"
                self.result = exec_result
                self.create_log("")

                # Restore stdout/stderr
                if REDIRECT:
                    sys.stderr = save_stderr
                    sys.stdout = save_stdout
                    fsock.close()

                    casalog.setlogfile("casa.log")
                    os.system("sed 's/^/casa.log: /' " + testlog + " >> " +
                              self.resultsubdir + '/' + logfilename)

                if not dry and cleanup:
                    self.tester.cleanup()
            except:
                if REDIRECT:
                    sys.stderr = save_stderr
                    sys.stdout = save_stdout
                    fsock.close()
                    casalog.setlogfile("casa.log")
                    os.system("sed 's/^/casa.log: /' " + testlog + " >> " +
                              self.resultsubdir + '/' + logfilename)

                print "Unexpected error:", sys.exc_info()[0]
                raise

        # end for k...

        print "Created ", self.resultsubdir
Example #6
0
    def __init__(self, test, \
                 DATA_REPOS=[AIPS_DIR+'/data'], \
                 WORKING_DIR='/tmp/casa_regression_work/', \
                 RESULT_DIR='/tmp/casa_regression_result/', \
                 retemplate=False,
                 cleanup=True,
                 CPP_PROFILE=False,
                 RESULT_SUBDIR='',
                 REDIRECT=True,
                 PY_PROFILE=True):
        """cleanup: set to False to keep data around.
        WORKING_DIR: directory for intermediate files
        RESULT_DIR: directory where final products go
        CPP_PROFILE: set to True to enable C++ profiling.  This requires that the command 'sudo opcontrol' must work.  You also need the 'dot' tool distributed as part of graphviz.  Run 'dot -Txxx' to verify that your dot installation supports PNG images.
        Note, a profile is created only for the casapy process. If you want to include profiles for async / child processes, refer to the documentation for opreport."""
        casalog.showconsole(onconsole=True)
        
        TEMPLATE_RESULT_DIR=AIPS_DIR+'/data/regression/'
        tests = [test]
        if type(tests) != type([]):
            raise TypeError
        self.resultdir=RESULT_DIR
        self.imdir=WORKING_DIR+'/IMAGES/'
        self.tester=testbase(WORKING_DIR)
        self.imagertests=[]
        self.result=[]
        self.numTests=0
        ####Get the directories right
        self.tester.setDataBaseDir(DATA_REPOS)
        self.tester.setScriptsDir(SCRIPT_REPOS)
        self.tester.setResultDir(RESULT_DIR)
        self.tester.setWorkingDir(WORKING_DIR)
        self.resultsubdir = ''

        print SCRIPT_REPOS
        
        if((len(tests)==1) and (tests[0]=='all')):
            self.numTests=self.tester.locateTests()
        else:
            self.numTests=self.tester.locateTests(tests)
        testName=''
             
        #pdb.set_trace()
        for k in range(self.numTests) :
            ### cleanup before each test
            if not dry and cleanup:
                self.tester.cleanup()

            self.tester.createDirs()

            uname1 = os.uname()[1]

            if self.tester.testname(k)[0:6] == 'tests/':
                testName=string.split(self.tester.testname(k)[6:], ".py")[0]
            else:
                testName=string.split(self.tester.testname(k), ".py")[0]
            if not RESULT_SUBDIR:
                self.resultsubdir = self.resultdir + "/result-" + \
                                    testName       + "-" + \
                                    uname1         + "-" + \
                                    time.strftime('%Y_%m_%d_%H_%M')
            else:
                self.resultsubdir = self.resultdir + "/" + RESULT_SUBDIR

            if not os.path.isdir(self.resultsubdir):
                os.mkdir(self.resultsubdir)

            logfilename = testName+'.log'
            if (os.path.isfile(self.resultsubdir+'/'+logfilename)):
                os.remove(self.resultsubdir+'/'+logfilename)

            # redirect stdout and stderr and casalog
            print 'Run test '+testName
            if REDIRECT:
                print "Redirect stdout/stderr to", self.resultsubdir+'/'+logfilename
                save_stdout = sys.stdout
                save_stderr = sys.stderr
                fsock = open(self.resultsubdir+'/'+logfilename, 'w')
                sys.stdout = logger("STDOUT", [save_stdout, fsock])
                sys.stderr = logger("STDERR", [save_stderr, fsock])

                testlog = self.tester.workingDirectory+"/test.log"
                open(testlog, "w").close()  # create empty file
                casalog.setlogfile(testlog) # seems to append to an existing file

            try:
                self.tester.getTest(self.tester.testname(k), testName)

                if PY_PROFILE:
                    if RESULT_SUBDIR != testName:
                        profilepage = RESULT_DIR+'/'+time.strftime('%Y_%m_%d/')+testName+'_profile.html'
                    else:
                        profilepage = RESULT_DIR+'/'+RESULT_SUBDIR+'/'+'profile.html'

                    process_data = "%s/profile.txt"  % self.tester.workingDirectory

                    os.system("echo -n > " + process_data)
                    pp = SCRIPT_REPOS + '/profileplot.py'  # for release
                    pyt = sys.executable
                    if not os.path.isfile(pp):
                        pp = SCRIPT_REPOS + '/../profileplot.py' # for devel
                    profileplot_pid=os.spawnlp(os.P_NOWAIT,
                                               pyt,
                                               pyt,
                                               pp,
                                               testName, RESULT_DIR + ("/" + RESULT_SUBDIR if RESULT_SUBDIR == testName else ''),
                                               profilepage,
                                               process_data,
                                               str(os.getpid()))
                    prof = cProfile.Profile()
                else:
                    prof = False

                presentDir=os.getcwd()
                os.chdir(self.tester.workingDirectory)

                short_description = self.tester.getDescription(testName, k)
                if short_description != None and short_description.find("'") >= 0:
                    print >> sys.stderr, \
                          "Warning: Short description contains ': '%s'" % \
                          short_description
                    short_description = short_description.replace("'", "")

                try:
                    self.op_init(CPP_PROFILE)
                    time1=time.time()
                    mem1 = commands.getoutput('env -i ps -p ' + str(os.getpid()) + ' -o rss | tail -1')
                    if prof:
                        #prof.runctx("(leResult, leImages)=self.tester.runtests(testName, k, dry)", globals(), locals())
                        #prof.runctx("(leResult, leImages)=self.tester.runtests(testName, k, dry)", gl, lo)
                        #prof.run("(leResult, leImages) = self.tester.runtests(testName, k, dry)")
                        (leResult, leImages) = prof.runcall(self.tester.runtests, testName, k, dry)
                    else:
                        (leResult, leImages) = self.tester.runtests(testName, k, dry)

                    # returns absolute_paths, relative_paths
                    exec_success = True
                except:
                    leResult=[]
                    exec_success = False
                    print >> sys.stderr, "%s failed, dumping traceback:" % testName
                    traceback.print_exc() # print and swallow exception

                mem2 = commands.getoutput('env -i ps -p ' + str(os.getpid()) + ' -o rss | tail -1')
                time2=time.time()
                time2=(time2-time1)/60.0

                print "Net memory allocated:", (int(mem2) - int(mem1))/1024, "MB"

                if prof:
                    try:
                        prof.dump_stats(self.resultsubdir+'/cProfile.profile')
                    except:
                        print >> sys.stderr, "Failed to write profiling data!"
               
                self.op_done(CPP_PROFILE)

                # Dump contents of any *.log file produced
                # by the regression script
                #
                # !! Does not handle out of diskspace
                #
                files = os.listdir('.')
                for f in files:
                    if f != 'casapy.log' and \
                           re.compile('.log$').search(f) != None:

                        for line in open(f, 'r'):
                            #print f + '    ddd'+line
                            if REDIRECT:
                                fsock.write(f + ': ' + line.rstrip( ))
                            else:
                                print f + ': ' + line.rstrip( )

                #
                # Report and deal with out of diskspace
                #
                space_left = commands.getoutput( \
                    "df -kP " + self.tester.workingDirectory + \
                    " | awk '{print $4}' | tail -1")
                space_left_h = commands.getoutput( \
                    "df -hP " + self.tester.workingDirectory + \
                    " | awk '{print $4}' | tail -1")
                space_used = commands.getoutput( \
                    "du -kc " + self.tester.workingDirectory + \
                    " | tail -1 | awk '{print $1}'")
                space_used_h = commands.getoutput( \
                    "du -hc " + self.tester.workingDirectory + \
                    " | tail -1 | awk '{print $1}'")

                if int(space_left) < 1000*1000:
                    print >> sys.stderr, "Warning: Only " + \
                          space_left_h + ' disk space left, ' + \
                          space_used_h + ' used'
                    # Clean up early, so that this infrastructure can continue
                    if not exec_success and cleanup:
                        self.tester.cleanup()
                        
                # Copy C++ profiling info
                if CPP_PROFILE:
                    os.system('cp cpp_profile.* ' + self.resultsubdir)

                os.chdir(presentDir)

                if PY_PROFILE:
                    # Terminate profiling process
                    os.kill(profileplot_pid,signal.SIGHUP)
                    status = os.waitpid(profileplot_pid, 0)[1]
                    #print str(profileplot_pid) + ' exit: ' + str(status)

                pagename=time.strftime('%Y_%m_%d/')+testName+'_profile.html'

                # entries common for all tests based on this run
                self.result_common = {}
                self.result_common['CASA'] = "'" + self.get_casa_version() + "'", "CASA version"
                self.result_common['host'] = uname1, "os.uname[1]"
                self.result_common['platform'] = "'" + self.get_platform()[0] + " " + self.get_platform()[1] + "'", "OS"

                self.result_common['date'] = time.strftime('%Y_%m_%d_%H_%M'), ""
                self.result_common['testid'] = testName, "test name"
                if short_description != None:
                    self.result_common['description'] = "'" + short_description + "'", "test short description"

                # Figure out data repository version
                if os.system("which svnversion >/dev/null") == 0:
                    (errorcode, datasvnr) = commands.getstatusoutput('cd '+DATA_REPOS[0]+' && svnversion 2>&1 | grep -vi warning')
                else:
                    errorcode = 1
                if errorcode != 0 or datasvnr == "exported":
                    # If that didn't work, look at ./version in the toplevel dir
                    (errorcode, datasvnr) = commands.getstatusoutput( \
                        'cd '+DATA_REPOS[0]+" && grep -E 'Rev:' version" \
                        )
                    if errorcode != 0:
                        datasvnr = "Unknown version"
                        
                self.result_common['data_version'] = "'"+datasvnr+"'", "Data repository version"

                # execution test
                exec_result = self.result_common.copy()
                exec_result['version'] = 2, "version of this file"
                exec_result['type'] = "exec", "test type"
                exec_result['time'] = time2*60, "execution time in seconds"
                exec_result['disk'] = space_used, "disk space (KB) in use after test"
                exec_result['runlog'] = logfilename, "execution logfile"


                if PY_PROFILE:
                    # read time/memory data
                    mem = ""
                    try:
                        process_file = open(process_data, "r")
                    except:
                        print "Warning: Failed to open file:", process_data
                        process_file = None
                else:
                    process_file = None

                if process_file != None:
                    lineno = 0
                    for line in process_file:
                        lineno += 1
                        if len(line) > 0 and line[0] != '#':
                            try:
                                (t, m_virtual, m_resident, nfiledesc,
                                 cpu_us, cpu_sy, cpu_id, cpu_wa) = line.split()
                                mem = mem + \
                                      str(t)          + ',' + \
                                      str(m_virtual)  + ',' + \
                                      str(m_resident) + ',' + \
                                      str(nfiledesc)  + ',' + \
                                      str(cpu_us)     + ',' + \
                                      str(cpu_sy)     + ',' + \
                                      str(cpu_id)     + ',' + \
                                      str(cpu_wa)     + ';'
                            except:
                                print >> sys.stderr, "Error parsing %s:%d: '%s'" % \
                                      (process_data, lineno, line)
                    process_file.close()
                    exec_result['resource'] = mem, "time(s),virtual(Mbytes),resident(Mbytes),nfiledesc,cpu_us,cpu_sy,cpu_id,cpu_wa"

                whatToTest=self.tester.whatQualityTest()
                keys=[]
                #if len(whatToTest) != 0:
                #    keys=whatToTest.keys()
                #    print 'THE KEYS ARE ', keys
                for j in range(len(leResult)) :   
                    templateImage=TEMPLATE_RESULT_DIR+"/"+testName+"/reference/"+leImages[j]
                    if retemplate:
                        if os.access(templateImage, os.F_OK):
                            shutil.rmtree(templateImage)
                    print 'TemplateImage '+templateImage
                    print 'theImage '+leResult[j]
                    print 'theImage '+leImages[j]

                    product_exists  = os.access(leResult[j], os.F_OK)
                    template_exists = os.access(templateImage, os.F_OK)

                    if product_exists and retemplate:
                        print 'Create template from', leResult[j]
                        if not os.path.isdir(TEMPLATE_RESULT_DIR+"/"+testName):
                            os.mkdir(TEMPLATE_RESULT_DIR+"/"+testName)
                        shutil.copytree(leResult[j], templateImage)

                    if not product_exists:
                        print >> sys.stderr, leResult[j], 'missing!'
                        exec_success = False
                        whatToTest[leResult[j]] = []
                        
                    if not template_exists:
                        print >> sys.stderr, templateImage, 'missing!'

                    for leQualityTest in whatToTest[leResult[j]] :
                        print leResult[j]+' WHAT : ', whatToTest[leResult[j]]
                        self.result=self.result_common.copy()
                        self.result['version'] = 1, "version of this file"
                        self.result['type'] = leQualityTest, "test type"
                        self.result['image'] = leImages[j], "test image"

                        if not product_exists:
                            self.result['status'] = 'fail', "result of regression test"
                            self.result['reason'] = "'Product image missing'", "reason of failure"
                        elif not template_exists:
                            self.result['status'] = 'fail', "result of regression test"
                            self.result['reason'] = "'Reference image missing'", "reason of failure"
                        else:
                            if os.access(self.imdir, os.F_OK):
                                shutil.rmtree(self.imdir)
                                
                            if(leQualityTest=='simple'):
                                self.simpleStats(leResult[j], templateImage, testName, WORKING_DIR, RESULT_DIR)
                            elif(leQualityTest=='pol2'):
                                self.polImageTest(leResult[j], templateImage, testName, WORKING_DIR, RESULT_DIR, 2)
                            elif(leQualityTest=='pol4'):
                                self.polImageTest(leResult[j], templateImage, testName, WORKING_DIR, RESULT_DIR, 4)
                            elif(leQualityTest=='cube'):
                                self.cubeImageTest(leResult[j], templateImage, testName, WORKING_DIR, RESULT_DIR)
                            elif(leQualityTest=='ms'):
                                self.visStats(leResult[j], templateImage, testName, WORKING_DIR, RESULT_DIR)
# RI add visStats method here, image ones use ImageTest so going to have to build a MSTest class
                            else:
                                self.polImageTest(leResult[j], templateImage, testName, WORKING_DIR, RESULT_DIR, 1)

                            # Pick up any images produced in test
                            if os.path.isdir(self.imdir):
                                i = 0
                                for image in os.listdir(self.imdir):
                                    i = i + 1
                                    shutil.copy(self.imdir + '/' + image, \
                                                self.resultsubdir+'/'+os.path.basename(image))
                                    self.result['imagefile_'+str(i)] = "'"+os.path.basename(image)+"'", 'regression image '+str(i)

                        self.create_log(leImages[j].replace('/', '-'))

                # Create exec log now that we now if
                # required images were produced
                exec_result['status'] = ("fail", "pass") [exec_success], "execution status"
                self.result = exec_result
                self.create_log("")

                # Restore stdout/stderr
                if REDIRECT:
                    sys.stderr = save_stderr
                    sys.stdout = save_stdout
                    fsock.close()

                    casalog.setlogfile("casapy.log")
                    os.system("sed 's/^/casapy.log: /' "+testlog+" >> "+self.resultsubdir+'/'+logfilename)

                if not dry and cleanup:
                    self.tester.cleanup()
            except:
                if REDIRECT:
                    sys.stderr = save_stderr
                    sys.stdout = save_stdout
                    fsock.close()
                    casalog.setlogfile("casapy.log")
                    os.system("sed 's/^/casapy.log: /' "+testlog+" >> "+self.resultsubdir+'/'+logfilename)

                print "Unexpected error:", sys.exc_info()[0]
                raise
            
        # end for k...
                
        print "Created ", self.resultsubdir
Example #7
0
import sys
import os
from taskinit import casalog

casalog.showconsole(onconsole=True)

print sys.argv

i = sys.argv.index("-c")
admin_dir  = sys.argv[i+2]
reg_dir    = sys.argv[i+3]
report_dir = sys.argv[i+4]
if len(sys.argv) > i+5:
    rev = sys.argv[i+5]
    gp = False
else:
    rev = 'all'
    gp = True

sys.path.append(admin_dir)
print sys.path
import report

try:
    report.report(reg_dir, report_dir, gen_plot=gp, revision=rev, skull=admin_dir+'/skullnbones.jpg')
except:
    print "report execution failed"
    raise
    exit()  # sys.exit(1) does not work with IPython
exit()