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 arrays giving the folders to check and their | |
25 relevant scale factors. For example: | |
26 | |
27 [[1, 'default_100_percent'], [2, 'default_200_percent']] | |
benrg
2012/07/19 21:47:56
Nit: I'd prefer tuples: [(1, '...'), ...].
You co
flackr
2012/07/20 13:53:34
Done.
benrg
2012/07/20 19:51:24
Grit (currently) requires the _###_percent suffix.
| |
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 PIL import Image | |
44 def ExpectedSize(base_width, base_height, scale): | |
45 return round(base_width * scale), round(base_height * scale) | |
benrg
2012/07/19 21:47:56
This doesn't need to be fixed right now, but when
flackr
2012/07/20 13:53:34
Yes, I fully expected this to change when we get n
benrg
2012/07/20 19:51:24
Sounds good.
| |
46 | |
47 repository_path = self.input_api.os_path.relpath( | |
48 self.input_api.PresubmitLocalPath(), | |
49 self.input_api.change.RepositoryRoot()) | |
50 results = [] | |
51 | |
52 # Check for affected files in any of the paths specified. | |
53 affected_files = self.input_api.AffectedFiles(include_deletes=False) | |
54 files = [] | |
55 for f in affected_files: | |
56 for path_spec in self.paths: | |
57 path_root = self.input_api.os_path.join( | |
58 repository_path, path_spec[1]) | |
59 if (f.LocalPath().endswith('.png') and | |
60 f.LocalPath().startswith(path_root)): | |
61 # Only save the relative path from the resource directory. | |
62 relative_path = self.input_api.os_path.relpath(f.LocalPath(), | |
63 path_root) | |
64 if relative_path not in files: | |
65 files.append(relative_path) | |
66 | |
67 for f in files: | |
68 base_image = self.input_api.os_path.join(self.paths[0][1], f) | |
69 if not os.path.exists(base_image): | |
70 results.append(self.output_api.PresubmitError( | |
71 'Base image %s does not exist' % self.input_api.os_path.join( | |
72 repository_path, base_image))) | |
73 continue | |
74 base_width, base_height = Image.open(open(base_image)).size | |
75 # Find all scaled versions of the base image and verify their sizes. | |
76 for i in range(1, len(self.paths)): | |
77 image_path = self.input_api.os_path.join(self.paths[i][1], f) | |
78 if not os.path.exists(image_path): | |
79 continue | |
80 # Ensure that each image for a particular scale factor is the | |
81 # correct scale of the base image. | |
82 exp_width, exp_height = ExpectedSize(base_width, base_height, | |
83 self.paths[i][0]) | |
84 width, height = Image.open(open(image_path)).size | |
85 if width != exp_width or height != exp_height: | |
86 results.append(self.output_api.PresubmitError( | |
87 'Image %s is %dx%d, expected to be %dx%d' % ( | |
88 self.input_api.os_path.join(repository_path, image_path), | |
89 width, height, exp_width, exp_height))) | |
90 return results | |
OLD | NEW |