| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Copyright 2014 Google Inc. |
| 5 | |
| 6 | Use of this source code is governed by a BSD-style license that can be |
| 7 | found in the LICENSE file. |
| 8 | |
| 9 | ImagePair class (see class docstring for details) |
| 10 | """ |
| 11 | |
| 12 | import posixpath |
| 13 | |
| 14 | # Keys used within ImagePair dictionary representations. |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 15 | KEY__DIFFERENCE_DATA = 'differenceData' |
| 16 | KEY__EXPECTATIONS_DATA = 'expectations' |
| 17 | KEY__EXTRA_COLUMN_VALUES = 'extraColumns' |
| 18 | KEY__IMAGE_A_URL = 'imageAUrl' |
| 19 | KEY__IMAGE_B_URL = 'imageBUrl' |
| 20 | KEY__IS_DIFFERENT = 'isDifferent' |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class ImagePair(object): |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 24 | """Describes a pair of images, pixel difference info, and optional metadata. |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 25 | """ |
| 26 | |
| 27 | def __init__(self, image_diff_db, |
| 28 | base_url, imageA_relative_url, imageB_relative_url, |
| 29 | expectations=None, extra_columns=None): |
| 30 | """ |
| 31 | Args: |
| 32 | image_diff_db: ImageDiffDB instance we use to generate/store image diffs |
| 33 | base_url: base of all image URLs |
| 34 | imageA_relative_url: URL pointing at an image, relative to base_url |
| 35 | imageB_relative_url: URL pointing at an image, relative to base_url |
| 36 | expectations: optional dictionary containing expectations-specific |
| 37 | metadata (ignore-failure, bug numbers, etc.) |
| 38 | extra_columns: optional dictionary containing more metadata (test name, |
| 39 | builder name, etc.) |
| 40 | """ |
| 41 | self.base_url = base_url |
| 42 | self.imageA_relative_url = imageA_relative_url |
| 43 | self.imageB_relative_url = imageB_relative_url |
| 44 | self.expectations_dict = expectations |
| 45 | self.extra_columns_dict = extra_columns |
| 46 | if imageA_relative_url == imageB_relative_url: |
| 47 | self.diff_record = None |
| 48 | else: |
| 49 | # TODO(epoger): Rather than blocking until image_diff_db can read in |
| 50 | # the image pair and generate diffs, it would be better to do it |
| 51 | # asynchronously: tell image_diff_db to download a bunch of file pairs, |
| 52 | # and only block later if we're still waiting for diff_records to come |
| 53 | # back. |
| 54 | image_diff_db.add_image_pair( |
| 55 | expected_image_locator=imageA_relative_url, |
| 56 | expected_image_url=posixpath.join(base_url, imageA_relative_url), |
| 57 | actual_image_locator=imageB_relative_url, |
| 58 | actual_image_url=posixpath.join(base_url, imageB_relative_url)) |
| 59 | self.diff_record = image_diff_db.get_diff_record( |
| 60 | expected_image_locator=imageA_relative_url, |
| 61 | actual_image_locator=imageB_relative_url) |
| 62 | |
| 63 | def as_dict(self): |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 64 | """Returns a dictionary describing this ImagePair. |
| 65 | |
| 66 | Uses the KEY__* constants as keys. |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 67 | """ |
| 68 | asdict = { |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 69 | KEY__IMAGE_A_URL: self.imageA_relative_url, |
| 70 | KEY__IMAGE_B_URL: self.imageB_relative_url, |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 71 | } |
| 72 | if self.expectations_dict: |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 73 | asdict[KEY__EXPECTATIONS_DATA] = self.expectations_dict |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 74 | if self.extra_columns_dict: |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 75 | asdict[KEY__EXTRA_COLUMN_VALUES] = self.extra_columns_dict |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 76 | if self.diff_record and (self.diff_record.get_num_pixels_differing() > 0): |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 77 | asdict[KEY__IS_DIFFERENT] = True |
| 78 | asdict[KEY__DIFFERENCE_DATA] = self.diff_record.as_dict() |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 79 | else: |
| commit-bot@chromium.org | 536b15f | 2014-02-13 17:17:05 +0000 | [diff] [blame] | 80 | asdict[KEY__IS_DIFFERENT] = False |
| commit-bot@chromium.org | 9985ef5 | 2014-02-10 18:19:30 +0000 | [diff] [blame] | 81 | return asdict |