Changeset 6649


Ignore:
Timestamp:
Mar 27, 2009, 4:00:15 PM (15 years ago)
Author:
rwilson
Message:

Can unpack remote-copied files now, on to setup to run simulation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_validation/automated_validation_tests/patong_validation/validate_patong.py

    r6639 r6649  
    1010import glob
    1111import unittest
    12 import logging
     12#import logging
     13import time
    1314import project
    14 from anuga.utilities.system_tools import get_web_file
     15from anuga.utilities.system_tools import get_web_file, untar_file
    1516import anuga.utilities.log as log
    16 
    17 # set logging levels
    18 log.console_logging_level = logging.DEBUG
    19 log.log_logging_level = logging.DEBUG
    2017
    2118
     
    3330
    3431def update_local_data():
    35    
    3632    # update one data object
    3733    def update_object(obj, auth):
     34        # Get a unique date+time string to defeat caching.  The idea is to add
     35        # this to the end of any URL so proxy sees a different request.
     36        cache_defeat = '?' + time.strftime('%Y%m%d%H%M%S')
     37
     38        # create local and remote paths, URLs, etc.
    3839        remote_path = os.path.join(Remote_Data_Directory, obj)
    3940        remote_digest = remote_path + '.tgz.digest'
    4041       
    4142        local_path = os.path.join(Local_Data_Directory, obj)
    42         local_digest = local_path + '.tgz.digest'
     43        local_file = local_path + '.tgz'
     44        local_digest = local_file + '.digest'
    4345       
    44         if not os.path.exists(local_digest):
    45             # no object digest, download both digest and object
    46             object_url = ANUGA_URL + obj + '.tgz.digest'
     46        object_url = ANUGA_URL + obj + '.tgz'
     47        digest_url = object_url + '.digest'
     48
     49        # see if have both digest and object .tgz
     50        if not os.path.exists(local_digest) or not os.path.exists(local_file):
     51            # no digest or no object, download both digest and object
     52            log.debug('Fetching remote file %s' % digest_url)
     53            auth = get_web_file(digest_url+cache_defeat,
     54                                local_digest, auth=auth)
    4755            log.debug('Fetching remote file %s' % object_url)
    48             auth = get_web_file(object_url, local_digest, auth=auth)
    49             object_url = ANUGA_URL + obj + '.tgz'
    50             local_file = local_path + '.tgz'
    51             log.debug('Fetching remote file %s' % object_url)
    52             auth = get_web_file(object_url, local_file, auth=auth)
     56            auth = get_web_file(object_url+cache_defeat, local_file, auth=auth)
    5357        else:
    5458            # download object digest to remote data directory
    5559            object_url = ANUGA_URL + obj + '.tgz.digest'
    5660            log.debug('Fetching remote file %s' % object_url)
    57             auth = get_web_file(object_url, remote_digest, auth=auth)
     61            auth = get_web_file(object_url+cache_defeat,
     62                                remote_digest, auth=auth)
    5863           
    59             # compare remote with local digest, refresh if necessary
     64            # compare remote with local digest
    6065            fd = open(local_digest, 'r')
    6166            local_digest_data = fd.read()
     
    6671            fd.close()
    6772
     73            # if local digest != remote digest, refresh object
    6874            if local_digest_data != remote_digest_data:
    69                 # refresh local digest & object
     75                log.debug('Local file %s is out of date' % obj)
    7076                fd = open(local_digest, 'w')
    7177                fd.write(remote_digest_data)
     
    7581                local_file = local_path + '.tgz'
    7682                log.debug('Fetching remote file %s' % object_url)
    77                 auth = get_web_file(object_url, local_file, auth=auth)
     83                auth = get_web_file(object_url+cache_defeat,
     84                                    local_file, auth=auth)
    7885        return auth
    7986
     
    94101    for data_object in Local_Data_Objects:
    95102        auth = update_object(data_object, auth)
     103
     104    # unpack *.tgz files
     105    for data_object in Local_Data_Objects:
     106        tar_path = os.path.join(Local_Data_Directory, data_object+'.tgz')
     107        log.debug('Untarring %s in dir %s' % (tar_path, Local_Data_Directory))
     108        untar_file(tar_path, target_dir=Local_Data_Directory)
    96109       
    97110
     
    111124    def tearDown(self):
    112125        pass
     126##        # clear all data objects from local data directory
     127##        for data_object in Local_Data_Objects:
     128##            try:
     129##                log.debug('Deleting %s' % os.path.join(Local_Data_Directory,
     130##                                                       data_object))
     131##                os.remove(os.path.join(Local_Data_Directory, data_object))
     132##            except IOError:
     133##                pass
    113134
    114135    def test_that_output_is_as_expected(self):
     
    117138        print 'update done'
    118139
    119 ##        # run the simulation, produce SWW file
    120 ##        s = 'run_model.py'
    121 ##        cmd = 'python %s > %s.stdout' % (s, s)
    122 ##        print 'cmd=%s' % cmd
    123 ##        res = os.system(cmd)
    124 ##        print 'res=%s' % str(res)
    125 ##        assert res == 0
    126 ##
    127 ##        # is SWW file as expected?
    128 ##        print 'About to run compare_output_with_expected.py'
    129 ##        s = 'compare_output_with_expected.py'
    130 ##        res = os.system('python %s > %s.stdout'
    131 ##                        % (s, s))
    132 ##        print 'Result from %s is %d' % (s, res)
    133 ##        assert res == 0
     140        # modify environment so we use the local data
     141        # INUNDATIONHOME points into the 'anuga' local data directory
     142        # N:\georisk_models\inundation\data\thailand\patong_tsunami_scenario\anuga
     143        old_inundationhome = os.getenv(project.ENV_INUNDATIONHOME)
     144        os.putenv(project.ENV_INUNDATIONHOME,
     145                  os.path.join(Local_Data_Directory, 'anuga'))
     146        # MUXHOME isn't used, but must exist
     147        undo_muxhome = False
     148        if os.getenv(project.ENV_MUXHOME) is None:
     149            os.putenv(project.ENV_MUXHOME,
     150                      os.path.join(Local_Data_Directory, 'data'))
     151            undo_muxhome = True
     152       
     153        # run the simulation, produce SWW file
     154        s = 'run_model.py'
     155        cmd = 'python %s > %s.stdout' % (s, s)
     156        print 'cmd=%s' % cmd
     157        res = os.system(cmd)
     158        print 'res=%s' % str(res)
     159        assert res == 0
     160
     161        # is SWW file as expected?
     162        print 'About to run compare_output_with_expected.py'
     163        s = 'compare_output_with_expected.py'
     164        res = os.system('python %s > %s.stdout'
     165                        % (s, s))
     166        print 'Result from %s is %d' % (s, res)
     167        assert res == 0
     168
     169        # undo environment changes
     170        if old_inundationhome:
     171            os.putenv(project.ENV_INUNDATIONHOME, old_inundationhome)
     172        if undo_muxhome:
     173            os.putenv(project.ENV_MUXHOME, None)
    134174
    135175################################################################################
    136176
    137177if __name__ == "__main__":
     178    # set logging levels
     179    #log.console_logging_level = log.CRITICAL   # no console logging
     180    log.console_logging_level = log.DEBUG
     181
    138182    suite = unittest.makeSuite(Test_Patong, 'test')
    139183    runner = unittest.TextTestRunner(verbosity=2)
Note: See TracChangeset for help on using the changeset viewer.