OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 """Presubmit script for Chromium browser resources. | |
6 | |
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | |
8 for more details about the presubmit API built into gcl/git cl, and see | |
9 http://www.chromium.org/developers/web-development-style-guide for the rules | |
10 we're checking against here. | |
11 """ | |
12 | |
13 | |
14 import os | |
15 | |
16 | |
17 class ResourceScaleFactors(object): | |
18 """Verifier of image dimensions for Chromium resources. | |
19 | |
20 This class verifies the image dimensions of resources in the various | |
21 resource subdirectories. | |
22 | |
23 Attributes: | |
24 paths: An array of tuples giving the folders to check and their | |
25 relevant scale factors. For example: | |
26 | |
27 [(1, 'default_100_percent'), (2, 'default_200_percent')] | |
28 """ | |
29 | |
30 def __init__(self, input_api, output_api, paths): | |
31 """ Initializes ResourceScaleFactors with paths.""" | |
32 self.input_api = input_api | |
33 self.output_api = output_api | |
34 self.paths = paths | |
35 | |
36 def RunChecks(self): | |
37 """Verifies the scale factors of resources being added or modified. | |
38 | |
39 Returns: | |
40 An array of presubmit errors if any images were detected not | |
41 having the correct dimensions. | |
42 """ | |
43 from third_party.pypng import png | |
44 def ImageSize(filename): | |
45 image = png.Reader(filename).read() | |
46 return (image[0], image[1]) | |
benrg
2012/07/20 19:51:25
I think this reads and decodes the whole file, unl
flackr
2012/07/20 20:01:15
Ah okay, I didn't realize the standard guaranteed
| |
47 | |
48 # TODO(flackr): This should allow some flexibility for non-integer scale | |
49 # factors such as allowing any size between the floor and ceiling of | |
50 # base * scale. | |
51 def ExpectedSize(base_width, base_height, scale): | |
52 return round(base_width * scale), round(base_height * scale) | |
53 | |
54 repository_path = self.input_api.os_path.relpath( | |
55 self.input_api.PresubmitLocalPath(), | |
56 self.input_api.change.RepositoryRoot()) | |
57 results = [] | |
58 | |
59 # Check for affected files in any of the paths specified. | |
60 affected_files = self.input_api.AffectedFiles(include_deletes=False) | |
61 files = [] | |
62 for f in affected_files: | |
63 for path_spec in self.paths: | |
64 path_root = self.input_api.os_path.join( | |
65 repository_path, path_spec[1]) | |
66 if (f.LocalPath().endswith('.png') and | |
67 f.LocalPath().startswith(path_root)): | |
68 # Only save the relative path from the resource directory. | |
69 relative_path = self.input_api.os_path.relpath(f.LocalPath(), | |
70 path_root) | |
71 if relative_path not in files: | |
72 files.append(relative_path) | |
73 | |
74 for f in files: | |
75 base_image = self.input_api.os_path.join(self.paths[0][1], f) | |
76 if not os.path.exists(base_image): | |
77 results.append(self.output_api.PresubmitError( | |
78 'Base image %s does not exist' % self.input_api.os_path.join( | |
79 repository_path, base_image))) | |
80 continue | |
81 base_width, base_height = ImageSize(base_image) | |
82 # Find all scaled versions of the base image and verify their sizes. | |
83 for i in range(1, len(self.paths)): | |
84 image_path = self.input_api.os_path.join(self.paths[i][1], f) | |
85 if not os.path.exists(image_path): | |
86 continue | |
87 # Ensure that each image for a particular scale factor is the | |
88 # correct scale of the base image. | |
89 exp_width, exp_height = ExpectedSize(base_width, base_height, | |
90 self.paths[i][0]) | |
91 width, height = ImageSize(image_path) | |
92 if width != exp_width or height != exp_height: | |
93 results.append(self.output_api.PresubmitError( | |
94 'Image %s is %dx%d, expected to be %dx%d' % ( | |
95 self.input_api.os_path.join(repository_path, image_path), | |
96 width, height, exp_width, exp_height))) | |
97 return results | |
OLD | NEW |