1# Copyright 2016 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 6# pylint: disable=W0201 7 8 9from recipe_engine import recipe_api 10 11from . import default_flavor 12from . import flutter_flavor 13from . import gn_android_flavor 14from . import gn_chromebook_flavor 15from . import gn_chromecast_flavor 16from . import gn_flavor 17from . import ios_flavor 18from . import pdfium_flavor 19from . import valgrind_flavor 20 21 22TEST_EXPECTED_SKP_VERSION = '42' 23TEST_EXPECTED_SVG_VERSION = '42' 24TEST_EXPECTED_SK_IMAGE_VERSION = '42' 25 26VERSION_FILE_SK_IMAGE = 'SK_IMAGE_VERSION' 27VERSION_FILE_SKP = 'SKP_VERSION' 28VERSION_FILE_SVG = 'SVG_VERSION' 29 30VERSION_NONE = -1 31 32def is_android(builder_cfg): 33 return 'Android' in builder_cfg.get('extra_config', '') 34 35def is_chromecast(builder_cfg): 36 return ('Chromecast' in builder_cfg.get('extra_config', '') or 37 'Chromecast' in builder_cfg.get('os', '')) 38 39def is_chromebook(builder_cfg): 40 return ('Chromebook' in builder_cfg.get('extra_config', '') or 41 'ChromeOS' in builder_cfg.get('os', '')) 42 43def is_flutter(builder_cfg): 44 return 'Flutter' in builder_cfg.get('extra_config', '') 45 46def is_ios(builder_cfg): 47 return ('iOS' in builder_cfg.get('extra_config', '') or 48 'iOS' == builder_cfg.get('os', '')) 49 50def is_pdfium(builder_cfg): 51 return 'PDFium' in builder_cfg.get('extra_config', '') 52 53def is_valgrind(builder_cfg): 54 return 'Valgrind' in builder_cfg.get('extra_config', '') 55 56 57class SkiaFlavorApi(recipe_api.RecipeApi): 58 def get_flavor(self, builder_cfg): 59 """Return a flavor utils object specific to the given builder.""" 60 if is_flutter(builder_cfg): 61 return flutter_flavor.FlutterFlavorUtils(self) 62 if is_chromecast(builder_cfg): 63 return gn_chromecast_flavor.GNChromecastFlavorUtils(self) 64 if is_chromebook(builder_cfg): 65 return gn_chromebook_flavor.GNChromebookFlavorUtils(self) 66 if is_android(builder_cfg): 67 return gn_android_flavor.GNAndroidFlavorUtils(self) 68 elif is_ios(builder_cfg): 69 return ios_flavor.iOSFlavorUtils(self) 70 elif is_pdfium(builder_cfg): 71 return pdfium_flavor.PDFiumFlavorUtils(self) 72 elif is_valgrind(builder_cfg): 73 return valgrind_flavor.ValgrindFlavorUtils(self) 74 else: 75 return gn_flavor.GNFlavorUtils(self) 76 77 def setup(self): 78 self._f = self.get_flavor(self.m.vars.builder_cfg) 79 80 def step(self, name, cmd, **kwargs): 81 return self._f.step(name, cmd, **kwargs) 82 83 def compile(self, target): 84 return self._f.compile(target) 85 86 def copy_extra_build_products(self, swarming_out_dir): 87 return self._f.copy_extra_build_products(swarming_out_dir) 88 89 @property 90 def out_dir(self): 91 return self._f.out_dir 92 93 def device_path_join(self, *args): 94 return self._f.device_path_join(*args) 95 96 def copy_directory_contents_to_device(self, host_dir, device_dir): 97 return self._f.copy_directory_contents_to_device(host_dir, device_dir) 98 99 def copy_directory_contents_to_host(self, device_dir, host_dir): 100 return self._f.copy_directory_contents_to_host(device_dir, host_dir) 101 102 def copy_file_to_device(self, host_path, device_path): 103 return self._f.copy_file_to_device(host_path, device_path) 104 105 def create_clean_host_dir(self, path): 106 return self._f.create_clean_host_dir(path) 107 108 def create_clean_device_dir(self, path): 109 return self._f.create_clean_device_dir(path) 110 111 def read_file_on_device(self, path, **kwargs): 112 return self._f.read_file_on_device(path, **kwargs) 113 114 def remove_file_on_device(self, path): 115 return self._f.remove_file_on_device(path) 116 117 def install_everything(self): 118 self.install(skps=True, images=True, svgs=True, resources=True) 119 120 def install(self, skps=False, images=False, svgs=False, resources=False): 121 self._f.install() 122 self.device_dirs = self._f.device_dirs 123 124 # TODO(borenet): Only copy files which have changed. 125 if resources: 126 self.copy_directory_contents_to_device( 127 self.m.vars.resource_dir, 128 self.device_dirs.resource_dir) 129 130 if skps: 131 self._copy_skps() 132 if images: 133 self._copy_images() 134 if svgs: 135 self._copy_svgs() 136 137 def cleanup_steps(self): 138 return self._f.cleanup_steps() 139 140 def _copy_dir(self, host_version, version_file, tmp_dir, 141 host_path, device_path, test_expected_version, 142 test_actual_version): 143 actual_version_file = self.m.path.join(tmp_dir, version_file) 144 # Copy to device. 145 device_version_file = self.device_path_join( 146 self.device_dirs.tmp_dir, version_file) 147 if str(actual_version_file) != str(device_version_file): 148 device_version = self.read_file_on_device(device_version_file, 149 abort_on_failure=False, 150 fail_build_on_failure=False) 151 if not device_version: 152 device_version = VERSION_NONE 153 if device_version != host_version: 154 self.remove_file_on_device(device_version_file) 155 self.create_clean_device_dir(device_path) 156 self.copy_directory_contents_to_device( 157 host_path, device_path) 158 159 # Copy the new version file. 160 self.copy_file_to_device(actual_version_file, device_version_file) 161 162 def _copy_images(self): 163 """Download and copy test images if needed.""" 164 version_file = self.m.vars.infrabots_dir.join( 165 'assets', 'skimage', 'VERSION') 166 test_data = self.m.properties.get( 167 'test_downloaded_sk_image_version', TEST_EXPECTED_SK_IMAGE_VERSION) 168 version = self.m.run.readfile( 169 version_file, 170 name='Get downloaded skimage VERSION', 171 test_data=test_data).rstrip() 172 self.m.run.writefile( 173 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_SK_IMAGE), 174 version) 175 self._copy_dir( 176 version, 177 VERSION_FILE_SK_IMAGE, 178 self.m.vars.tmp_dir, 179 self.m.vars.images_dir, 180 self.device_dirs.images_dir, 181 test_expected_version=self.m.properties.get( 182 'test_downloaded_sk_image_version', 183 TEST_EXPECTED_SK_IMAGE_VERSION), 184 test_actual_version=self.m.properties.get( 185 'test_downloaded_sk_image_version', 186 TEST_EXPECTED_SK_IMAGE_VERSION)) 187 return version 188 189 def _copy_skps(self): 190 """Download and copy the SKPs if needed.""" 191 version_file = self.m.vars.infrabots_dir.join( 192 'assets', 'skp', 'VERSION') 193 test_data = self.m.properties.get( 194 'test_downloaded_skp_version', TEST_EXPECTED_SKP_VERSION) 195 version = self.m.run.readfile( 196 version_file, 197 name='Get downloaded SKP VERSION', 198 test_data=test_data).rstrip() 199 self.m.run.writefile( 200 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_SKP), 201 version) 202 self._copy_dir( 203 version, 204 VERSION_FILE_SKP, 205 self.m.vars.tmp_dir, 206 self.m.vars.local_skp_dir, 207 self.device_dirs.skp_dir, 208 test_expected_version=self.m.properties.get( 209 'test_downloaded_skp_version', TEST_EXPECTED_SKP_VERSION), 210 test_actual_version=self.m.properties.get( 211 'test_downloaded_skp_version', TEST_EXPECTED_SKP_VERSION)) 212 return version 213 214 def _copy_svgs(self): 215 """Download and copy the SVGs if needed.""" 216 version_file = self.m.vars.infrabots_dir.join( 217 'assets', 'svg', 'VERSION') 218 test_data = self.m.properties.get( 219 'test_downloaded_svg_version', TEST_EXPECTED_SVG_VERSION) 220 version = self.m.run.readfile( 221 version_file, 222 name='Get downloaded SVG VERSION', 223 test_data=test_data).rstrip() 224 self.m.run.writefile( 225 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_SVG), 226 version) 227 self._copy_dir( 228 version, 229 VERSION_FILE_SVG, 230 self.m.vars.tmp_dir, 231 self.m.vars.local_svg_dir, 232 self.device_dirs.svg_dir, 233 test_expected_version=self.m.properties.get( 234 'test_downloaded_svg_version', TEST_EXPECTED_SVG_VERSION), 235 test_actual_version=self.m.properties.get( 236 'test_downloaded_svg_version', TEST_EXPECTED_SVG_VERSION)) 237 return version 238