1 # Copyright 2017 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.
9 """iOS flavor, used for running code on iOS."""
12 class iOSFlavor(default.DefaultFlavor):
13 def __init__(self, m, app_name):
14 super(iOSFlavor, self).__init__(m, app_name)
15 self.device_dirs = default.DeviceDirs(
19 resource_dir='resources',
21 lotties_dir='lotties',
31 'IOS_BUNDLE_ID': 'com.google.%s' % self.app_name,
32 'IOS_MOUNT_POINT': self.m.vars.workdir.join('mnt_iosdevice'),
36 return self.m.context(env=self.env)
38 def _run(self, title, *cmd, **kwargs):
40 self.m.python.inline('sleep before attempt %d' % attempt, """
43 """) # pragma: nocover
44 return self.m.run.with_retry(self.m.step, title, 3, cmd=list(cmd),
45 between_attempts_fn=sleep, **kwargs)
52 # We assume a single device is connected.
56 self.m.run(self.m.step, 'check if device is paired',
57 cmd=['idevicepair', 'validate'],
58 infra_step=True, abort_on_failure=True,
59 fail_build_on_failure=False)
60 except self.m.step.StepFailure: # pragma: nocover
61 self._run('pair device', 'idevicepair', 'pair') # pragma: nocover
63 # Mount developer image.
64 image_info = self._run('list mounted image',
65 'ideviceimagemounter', '--list',
66 stdout=self.m.raw_io.output())
68 image_info.stdout.decode('utf-8').strip() if image_info.stdout else '')
70 if 'ImageSignature' not in image_info_out:
71 image_pkgs = self.m.file.glob_paths('locate ios-dev-image package',
72 self.m.path['start_dir'],
74 test_data=['ios-dev-image-13.2'])
75 if len(image_pkgs) != 1:
76 raise Exception('glob for ios-dev-image* returned %s'
77 % image_pkgs) # pragma: nocover
79 image_pkg = image_pkgs[0]
80 contents = self.m.file.listdir(
81 'locate image and signature', image_pkg,
82 test_data=['DeveloperDiskImage.dmg',
83 'DeveloperDiskImage.dmg.signature'])
87 if str(f).endswith('.dmg'):
89 if str(f).endswith('.dmg.signature'):
91 if not image or not sig:
92 raise Exception('%s does not contain *.dmg and *.dmg.signature' %
93 image_pkg) # pragma: nocover
95 self._run('mount developer image', 'ideviceimagemounter', image, sig)
97 # Install app (necessary before copying any resources to the device).
99 app_package = self.host_dirs.bin_dir.join('%s.app' % self.app_name)
101 def uninstall_app(attempt):
102 # If app ID changes, upgrade will fail, so try uninstalling.
103 self.m.run(self.m.step,
104 'uninstall %s' % self.app_name,
105 cmd=['ideviceinstaller', '-U',
106 'com.google.%s' % self.app_name],
108 # App may not be installed.
109 abort_on_failure=False, fail_build_on_failure=False)
112 self.m.run.with_retry(self.m.step, 'install %s' % self.app_name,
114 cmd=['ideviceinstaller', '-i', app_package],
115 between_attempts_fn=uninstall_app,
118 def step(self, name, cmd, **kwargs):
120 bundle_id = 'com.google.%s' % app_name
121 args = [bundle_id] + [str(ele) for ele in cmd[1:]]
125 self.m.run(self.m.step, name, cmd=['idevicedebug', 'run'] + args)
129 self.m.run(self.m.python, '%s with full debug output' % name,
130 script=self.module.resource('ios_debug_cmd.py'),
133 def _run_ios_script(self, script, first, *rest):
135 full = self.m.path['start_dir'].join(
136 'skia', 'platform_tools', 'ios', 'bin', 'ios_' + script)
137 self.m.run(self.m.step,
138 name = '%s %s' % (script, first),
139 cmd = [full, first] + list(rest),
142 def copy_file_to_device(self, host, device):
143 self._run_ios_script('push_file', host, device)
145 def copy_directory_contents_to_device(self, host, device):
146 self._run_ios_script('push_if_needed', host, device)
148 def copy_directory_contents_to_host(self, device, host):
149 self._run_ios_script('pull_if_needed', device, host)
151 def remove_file_on_device(self, path):
152 self._run_ios_script('rm', path)
154 def create_clean_device_dir(self, path):
155 self._run_ios_script('rm', path)
156 self._run_ios_script('mkdir', path)
158 def read_file_on_device(self, path, **kwargs):
160 full = self.m.path['start_dir'].join(
161 'skia', 'platform_tools', 'ios', 'bin', 'ios_cat_file')
162 rv = self.m.run(self.m.step,
163 name = 'cat_file %s' % path,
165 stdout=self.m.raw_io.output(),
168 return rv.stdout.decode('utf-8').rstrip() if rv and rv.stdout else None