Debian Codegraph @

infra/bots/recipe_modules/flavor/api.py

raw
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
9 from recipe_engine import recipe_api
10
11 from . import android
12 from . import chromebook
13 from . import default
14 from . import ios
15
16
17 """Abstractions for running code on various platforms.
18
19 The methods in this module define how certain high-level functions should work.
20 Each flavor should correspond to a subclass of DefaultFlavor which may override
21 any of these functions as appropriate for that flavor.
22
23 For example, the AndroidFlavor will override the functions for copying files
24 between the host and Android device, as well as the 'step' function, so that
25 commands may be run through ADB.
26 """
27
28
29 VERSION_FILE_LOTTIE = 'LOTTIE_VERSION'
30 VERSION_FILE_SK_IMAGE = 'SK_IMAGE_VERSION'
31 VERSION_FILE_SKP = 'SKP_VERSION'
32 VERSION_FILE_SVG = 'SVG_VERSION'
33 VERSION_FILE_TEXTTRACES = 'TEXTTRACES_VERSION'
34
35 VERSION_NONE = -1
36
37 def is_android(vars_api):
38 return ('Android' in vars_api.extra_tokens or
39 'Android' in vars_api.builder_cfg.get('os', ''))
40
41 def is_chromebook(vars_api):
42 return ('Chromebook' in vars_api.extra_tokens or
43 'ChromeOS' in vars_api.builder_cfg.get('os', ''))
44
45 def is_ios(vars_api):
46 return ('iOS' in vars_api.extra_tokens or
47 'iOS' in vars_api.builder_cfg.get('os', ''))
48
49 class SkiaFlavorApi(recipe_api.RecipeApi):
50 def get_flavor(self, vars_api, app_name):
51 """Return a flavor utils object specific to the given builder."""
52 if is_chromebook(vars_api):
53 return chromebook.ChromebookFlavor(self, app_name)
54 if is_android(vars_api):
55 return android.AndroidFlavor(self, app_name)
56 elif is_ios(vars_api):
57 return ios.iOSFlavor(self, app_name)
58 else:
59 return default.DefaultFlavor(self, app_name)
60
61 def setup(self, app_name):
62 self._f = self.get_flavor(self.m.vars, app_name)
63 self.device_dirs = self._f.device_dirs
64 self.host_dirs = self._f.host_dirs
65 self._skia_dir = self.m.path.start_dir.joinpath('skia')
66
67 def step(self, name, cmd, **kwargs):
68 return self._f.step(name, cmd, **kwargs)
69
70 def device_path_join(self, *args):
71 return self._f.device_path_join(*args)
72
73 def copy_directory_contents_to_device(self, host_dir, device_dir):
74 return self._f.copy_directory_contents_to_device(host_dir, device_dir)
75
76 def copy_directory_contents_to_host(self, device_dir, host_dir):
77 return self._f.copy_directory_contents_to_host(device_dir, host_dir)
78
79 def copy_file_to_device(self, host_path, device_path):
80 return self._f.copy_file_to_device(host_path, device_path)
81
82 def create_clean_host_dir(self, path):
83 return self._f.create_clean_host_dir(path)
84
85 def create_clean_device_dir(self, path):
86 return self._f.create_clean_device_dir(path)
87
88 def read_file_on_device(self, path, **kwargs):
89 return self._f.read_file_on_device(path, **kwargs)
90
91 def remove_file_on_device(self, path):
92 return self._f.remove_file_on_device(path)
93
94 def install(self, skps=False, images=False, lotties=False, svgs=False,
95 resources=False, texttraces=False):
96 self._f.install()
97
98 if texttraces:
99 self._copy_texttraces()
100 # TODO(borenet): Only copy files which have changed.
101 if resources:
102 self.copy_directory_contents_to_device(
103 self.m.path.start_dir.joinpath('skia', 'resources'),
104 self.device_dirs.resource_dir)
105
106 if skps:
107 self._copy_skps()
108 if images:
109 self._copy_images()
110 if lotties:
111 self._copy_lotties()
112 if svgs:
113 self._copy_svgs()
114
115 def cleanup_steps(self):
116 return self._f.cleanup_steps()
117
118 def _copy_dir(self, host_version, version_file, tmp_dir,
119 host_path, device_path):
120 actual_version_file = self.m.path.join(tmp_dir, version_file)
121 # Copy to device.
122 device_version_file = self.device_path_join(
123 self.device_dirs.tmp_dir, version_file)
124 if str(actual_version_file) != str(device_version_file):
125 device_version = self.read_file_on_device(device_version_file,
126 abort_on_failure=False,
127 fail_build_on_failure=False)
128 if not device_version:
129 device_version = VERSION_NONE
130 if device_version != host_version:
131 self.remove_file_on_device(device_version_file)
132 self.create_clean_device_dir(device_path)
133 self.copy_directory_contents_to_device(
134 host_path, device_path)
135
136 # Copy the new version file.
137 self.copy_file_to_device(actual_version_file, device_version_file)
138
139 def _copy_images(self):
140 """Copy test images if needed."""
141 version = self.m.run.asset_version('skimage', self._skia_dir)
142 self.m.run.writefile(
143 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_SK_IMAGE),
144 version)
145 self._copy_dir(
146 version,
147 VERSION_FILE_SK_IMAGE,
148 self.m.vars.tmp_dir,
149 self.host_dirs.images_dir,
150 self.device_dirs.images_dir)
151 return version
152
153 def _copy_lotties(self):
154 """Copy test lotties if needed."""
155 version = self.m.run.asset_version('lottie-samples', self._skia_dir)
156 self.m.run.writefile(
157 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_LOTTIE),
158 version)
159 self._copy_dir(
160 version,
161 VERSION_FILE_LOTTIE,
162 self.m.vars.tmp_dir,
163 self.host_dirs.lotties_dir,
164 self.device_dirs.lotties_dir)
165 return version
166
167 def _copy_skps(self):
168 """Copy the SKPs if needed."""
169 version = self.m.run.asset_version('skp', self._skia_dir)
170 self.m.run.writefile(
171 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_SKP),
172 version)
173 self._copy_dir(
174 version,
175 VERSION_FILE_SKP,
176 self.m.vars.tmp_dir,
177 self.host_dirs.skp_dir,
178 self.device_dirs.skp_dir)
179 return version
180
181 def _copy_svgs(self):
182 """Copy the SVGs if needed."""
183 version = self.m.run.asset_version('svg', self._skia_dir)
184 self.m.run.writefile(
185 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_SVG),
186 version)
187 self._copy_dir(
188 version,
189 VERSION_FILE_SVG,
190 self.m.vars.tmp_dir,
191 self.host_dirs.svg_dir,
192 self.device_dirs.svg_dir)
193 return version
194
195 def _copy_texttraces(self):
196 """Copy the text traces if needed."""
197 version = self.m.run.asset_version('text_blob_traces', self._skia_dir)
198 self.m.run.writefile(
199 self.m.path.join(self.m.vars.tmp_dir, VERSION_FILE_TEXTTRACES),
200 version)
201 self._copy_dir(
202 version,
203 VERSION_FILE_TEXTTRACES,
204 self.m.vars.tmp_dir,
205 self.host_dirs.texttraces_dir,
206 self.device_dirs.texttraces_dir)
207 return version