<s> """<STR_LIT>""" <EOL> __author__ = [ '<STR_LIT>' ] <EOL> import json <EOL> import logging <EOL> import os <EOL> import shutil <EOL> from tornado import gen , options , web <EOL> from viewfinder . backend . base import handler <EOL> from viewfinder . backend . base . environ import ServerEnvironment <EOL> from viewfinder . backend . db . identity import Identity <EOL> from viewfinder . backend . db . operation import Operation <EOL> from viewfinder . backend . db . viewpoint import Viewpoint <EOL> from viewfinder . backend . www import base <EOL> _TEST_HOOKS_NOT_SUPPORTED = '<STR_LIT>' + '<STR_LIT>' <EOL> _TEST_ACTION_NOT_SUPPORTED = '<STR_LIT>' <EOL> _STATIC_RESULTS_BASELINE = '<STR_LIT>' <EOL> _STATIC_RESULTS_CURRENT = '<STR_LIT>' <EOL> class TestHookHandler ( web . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def check_xsrf_cookie ( self ) : <EOL> pass <EOL> @ handler . asynchronous ( datastore = True , obj_store = True ) <EOL> @ gen . coroutine <EOL> def post ( self , action ) : <EOL> if not ServerEnvironment . IsDevBox ( ) : <EOL> raise web . HTTPError ( <NUM_LIT> , _TEST_HOOKS_NOT_SUPPORTED ) <EOL> from PIL import Image , ImageChops <EOL> if action == '<STR_LIT>' : <EOL> logging . info ( '<STR_LIT>' ) <EOL> urls = { } <EOL> body = json . loads ( self . request . body ) <EOL> testname = body [ '<STR_LIT>' ] <EOL> imagename = body [ '<STR_LIT>' ] <EOL> scheme = body [ '<STR_LIT>' ] <EOL> _FULL_RESULTS_BASELINE = '<STR_LIT>' % ( options . options . testing_path , scheme ) <EOL> _FULL_RESULTS_CURRENT = '<STR_LIT>' % ( options . options . testing_path , scheme ) <EOL> baseline_image = r'<STR_LIT>' % ( _FULL_RESULTS_BASELINE , testname , imagename ) <EOL> current_image = r'<STR_LIT>' % ( _FULL_RESULTS_CURRENT , testname , imagename ) <EOL> yield self . _UpdateImageMaskConfig ( testname , imagename , scheme ) <EOL> if os . path . exists ( current_image ) : <EOL> shutil . copy ( current_image , baseline_image ) <EOL> logging . info ( '<STR_LIT>' % testname ) <EOL> baseline_web_image = r'<STR_LIT>' % ( _STATIC_RESULTS_BASELINE , scheme , testname , imagename ) <EOL> current_web_image = r'<STR_LIT>' % ( _STATIC_RESULTS_CURRENT , scheme , testname , imagename ) <EOL> urls [ '<STR_LIT>' ] = baseline_web_image <EOL> urls [ '<STR_LIT>' ] = current_web_image <EOL> self . write ( urls ) <EOL> self . finish ( ) <EOL> return <EOL> if action == '<STR_LIT>' : <EOL> body = json . loads ( self . request . body ) <EOL> testname = body [ '<STR_LIT>' ] <EOL> imagename = body [ '<STR_LIT>' ] <EOL> current_image = r'<STR_LIT>' % ( _FULL_RESULTS_CURRENT , testname , imagename ) <EOL> if os . path . exists ( current_image ) is True : <EOL> os . remove ( current_image ) <EOL> logging . info ( '<STR_LIT>' % testname ) <EOL> self . finish ( ) <EOL> return <EOL> if action == '<STR_LIT>' : <EOL> body = json . loads ( self . request . body ) <EOL> identity_key = body [ '<STR_LIT>' ] [ '<STR_LIT>' ] ; <EOL> identity = yield gen . Task ( Identity . Query , self . _client , identity_key , None , must_exist = False ) <EOL> if identity is None : <EOL> raise web . HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . write ( identity . access_token ) <EOL> self . finish ( ) <EOL> return <EOL> if action == '<STR_LIT:image>' : <EOL> body = json . loads ( self . request . body ) <EOL> test_name = body [ '<STR_LIT>' ] <EOL> image_name = body [ '<STR_LIT>' ] <EOL> scheme = body [ '<STR_LIT>' ] <EOL> _FULL_RESULTS_BASELINE = '<STR_LIT>' % ( options . options . testing_path , scheme ) <EOL> _FULL_RESULTS_CURRENT = '<STR_LIT>' % ( options . options . testing_path , scheme ) <EOL> tmp = image_name [ : - <NUM_LIT:4> ] <EOL> base , num = tmp . split ( '<STR_LIT:|>' , <NUM_LIT:1> ) <EOL> image_base_name = '<STR_LIT>' % base <EOL> image1 = r'<STR_LIT>' % ( _FULL_RESULTS_BASELINE , test_name , image_base_name ) <EOL> image2 = r'<STR_LIT>' % ( _FULL_RESULTS_CURRENT , test_name , image_name ) <EOL> if os . path . exists ( image1 ) and os . path . exists ( image2 ) : <EOL> self . set_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> im1 = Image . open ( image1 ) <EOL> im2 = Image . open ( image2 ) <EOL> diff = ImageChops . difference ( im2 , im1 ) <EOL> result = diff . getbbox ( ) is None <EOL> response = { '<STR_LIT>' : result , '<STR_LIT>' : diff . getbbox ( ) } <EOL> self . write ( response ) <EOL> self . finish ( ) <EOL> return <EOL> raise web . HTTPError ( <NUM_LIT> , _TEST_ACTION_NOT_SUPPORTED ) <EOL> @ gen . coroutine <EOL> def _UpdateImageMaskConfig ( self , test_name , image_name , scheme ) : <EOL> _IMAGE_MASK_CONFIG_PATH = os . path . join ( options . options . testing_path , '<STR_LIT>' ) <EOL> _IMAGE_MASK_CONFIG_FILE = '<STR_LIT>' % ( _IMAGE_MASK_CONFIG_PATH , scheme ) <EOL> _FULL_RESULTS_BASELINE = '<STR_LIT>' % options . options . testing_path <EOL> _FULL_RESULTS_CURRENT = '<STR_LIT>' % options . options . testing_path <EOL> baseline_image = r'<STR_LIT>' % ( _FULL_RESULTS_BASELINE , scheme , test_name , image_name ) <EOL> current_image = r'<STR_LIT>' % ( _FULL_RESULTS_CURRENT , scheme , test_name , image_name ) <EOL> from PIL import Image , ImageChops <EOL> try : <EOL> im1 = Image . open ( baseline_image ) <EOL> im2 = Image . open ( current_image ) <EOL> _IMAGE_MASK_CONFIG = { } <EOL> diff = ImageChops . difference ( im2 , im1 ) <EOL> bbox = diff . getbbox ( ) <EOL> if bbox is not None : <EOL> if os . path . exists ( _IMAGE_MASK_CONFIG_FILE ) : <EOL> config_data = open ( _IMAGE_MASK_CONFIG_FILE , '<STR_LIT:r>' ) <EOL> _IMAGE_MASK_CONFIG = json . load ( config_data ) <EOL> width = bbox [ <NUM_LIT:2> ] - bbox [ <NUM_LIT:0> ] ; <EOL> height = bbox [ <NUM_LIT:3> ] - bbox [ <NUM_LIT:1> ] <EOL> if width <= <NUM_LIT:100> and height <= <NUM_LIT:100> : <EOL> x , y = <NUM_LIT:0> , <NUM_LIT:0> <EOL> mask = { "<STR_LIT:x>" : bbox [ <NUM_LIT:0> ] , "<STR_LIT:y>" : bbox [ <NUM_LIT:1> ] , "<STR_LIT>" : height - <NUM_LIT:1> , "<STR_LIT:width>" : width - <NUM_LIT:1> } <EOL> logging . info ( '<STR_LIT>' % mask ) <EOL> if test_name not in _IMAGE_MASK_CONFIG : <EOL> _IMAGE_MASK_CONFIG [ test_name ] = { } <EOL> _IMAGE_MASK_CONFIG [ test_name ] [ image_name ] = [ ] <EOL> else : <EOL> if image_name not in _IMAGE_MASK_CONFIG [ test_name ] : <EOL> _IMAGE_MASK_CONFIG [ test_name ] [ image_name ] = [ ] <EOL> else : <EOL> for dict in _IMAGE_MASK_CONFIG [ test_name ] [ image_name ] : <EOL> if dict [ '<STR_LIT:x>' ] == bbox [ <NUM_LIT:0> ] and dict [ '<STR_LIT:y>' ] == bbox [ <NUM_LIT:1> ] : <EOL> if dict [ '<STR_LIT>' ] == height or dict [ '<STR_LIT:width>' ] == width : <EOL> return ; <EOL> _IMAGE_MASK_CONFIG [ test_name ] [ image_name ] . append ( mask ) <EOL> cfg_data_file = open ( _IMAGE_MASK_CONFIG_FILE , '<STR_LIT:w>' ) <EOL> cfg_data_file . write ( json . dumps ( _IMAGE_MASK_CONFIG ) ) <EOL> logging . info ( '<STR_LIT>' % _IMAGE_MASK_CONFIG_FILE ) <EOL> except ( IOError , ValueError ) as e : <EOL> logging . exception ( '<STR_LIT>' ) </s>
