<s> '''<STR_LIT>''' <EOL> import os <EOL> import logging <EOL> from osgeo import gdal <EOL> from EOtools . utils import log_multiline <EOL> from EOtools . execute import execute <EOL> import sys <EOL> from time import sleep <EOL> from agdc import DataCube <EOL> console_handler = logging . StreamHandler ( sys . stdout ) <EOL> console_handler . setLevel ( logging . INFO ) <EOL> console_formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> console_handler . setFormatter ( console_formatter ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> if not logger . level : <EOL> logger . setLevel ( logging . DEBUG ) <EOL> logger . addHandler ( console_handler ) <EOL> class IntegrityChecker ( DataCube ) : <EOL> '''<STR_LIT>''' <EOL> def check_files ( self , path_prefix = None , level_name_tuple = None , tile_type_id = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> query_cursor = self . db_connection . cursor ( ) <EOL> check_cursor = self . db_connection . cursor ( ) <EOL> update_cursor = self . db_connection . cursor ( ) <EOL> query_sql = """<STR_LIT>""" <EOL> if level_name_tuple : <EOL> query_sql += """<STR_LIT>""" <EOL> query_sql += """<STR_LIT>""" <EOL> if level_name_tuple : <EOL> query_sql += """<STR_LIT>""" <EOL> if path_prefix : <EOL> query_sql += """<STR_LIT>""" <EOL> query_sql += """<STR_LIT>""" <EOL> query_params = { '<STR_LIT>' : tile_type_id , <EOL> '<STR_LIT>' : path_prefix , <EOL> '<STR_LIT>' : level_name_tuple <EOL> } <EOL> log_multiline ( logger . debug , query_cursor . mogrify ( query_sql , query_params ) , '<STR_LIT>' , '<STR_LIT:\t>' ) <EOL> while True : <EOL> while not self . lock_object ( '<STR_LIT>' ) : <EOL> sleep ( <NUM_LIT:10> ) <EOL> try : <EOL> query_cursor . execute ( query_sql , query_params ) <EOL> finally : <EOL> self . unlock_object ( '<STR_LIT>' ) <EOL> if not query_cursor : <EOL> break <EOL> for record in query_cursor : <EOL> tile_id = record [ <NUM_LIT:0> ] <EOL> tile_pathname = record [ <NUM_LIT:1> ] <EOL> check_sql = """<STR_LIT>""" <EOL> check_params = { '<STR_LIT>' : tile_id , <EOL> '<STR_LIT>' : tile_type_id <EOL> } <EOL> log_multiline ( logger . debug , check_cursor . mogrify ( check_sql , check_params ) , '<STR_LIT>' , '<STR_LIT:\t>' ) <EOL> check_cursor . execute ( check_sql , check_params ) <EOL> if not check_cursor : <EOL> continue <EOL> if self . lock_object ( tile_pathname ) : <EOL> tile_status = <NUM_LIT:0> <EOL> try : <EOL> if not os . path . exists ( tile_pathname ) : <EOL> tile_status = <NUM_LIT:1> <EOL> else : <EOL> dataset = gdal . Open ( tile_pathname ) <EOL> if dataset : <EOL> try : <EOL> array = dataset . GetRasterBand ( dataset . RasterCount ) . ReadAsArray ( ) <EOL> except Exception , e : <EOL> logger . debug ( '<STR_LIT>' , e . message ) <EOL> tile_status = <NUM_LIT:3> <EOL> else : <EOL> tile_status = <NUM_LIT:2> <EOL> logger . info ( '<STR_LIT>' , tile_pathname , tile_status ) <EOL> update_sql = """<STR_LIT>""" <EOL> update_params = { '<STR_LIT>' : tile_status , <EOL> '<STR_LIT>' : tile_id <EOL> } <EOL> log_multiline ( logger . debug , update_cursor . mogrify ( update_sql , update_params ) , '<STR_LIT>' , '<STR_LIT:\t>' ) <EOL> update_cursor . execute ( update_sql , update_params ) <EOL> self . db_connection . commit ( ) <EOL> except Exception , e : <EOL> logger . error ( e . message ) <EOL> self . db_connection . rollback ( ) <EOL> finally : <EOL> self . unlock_object ( tile_pathname ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> integrity_checker = IntegrityChecker ( ) <EOL> if integrity_checker . debug : <EOL> console_handler . setLevel ( logging . DEBUG ) <EOL> integrity_checker . check_files ( level_name_tuple = ( '<STR_LIT>' , ) ) </s>
