Home | History | Annotate | Download | only in flavor
      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.
      4 
      5 # Disable warning about setting self.device_dirs in install(); we need to.
      6 # pylint: disable=W0201
      7 
      8 
      9 from . import default
     10 
     11 
     12 """iOS flavor, used for running code on iOS."""
     13 
     14 
     15 class iOSFlavor(default.DefaultFlavor):
     16   def __init__(self, m):
     17     super(iOSFlavor, self).__init__(m)
     18     self.device_dirs = default.DeviceDirs(
     19         bin_dir='[unused]',
     20         dm_dir='dm',
     21         perf_data_dir='perf',
     22         resource_dir='resources',
     23         images_dir='images',
     24         lotties_dir='lotties',
     25         skp_dir='skps',
     26         svg_dir='svgs',
     27         tmp_dir='tmp')
     28 
     29   def install(self):
     30     # Set up the device
     31     self.m.run(self.m.step, 'setup_device', cmd=['ios.py'], infra_step=True)
     32 
     33     # Install the app.
     34     for app_name in ['dm', 'nanobench']:
     35       app_package = self.host_dirs.bin_dir.join('%s.app' % app_name)
     36 
     37       def uninstall_app(attempt):
     38         # If app ID changes, upgrade will fail, so try uninstalling.
     39         self.m.run(self.m.step,
     40                    'uninstall_' + app_name,
     41                    cmd=['ideviceinstaller', '-U', 'com.google.%s' % app_name],
     42                    infra_step=True,
     43                    # App may not be installed.
     44                    abort_on_failure=False, fail_build_on_failure=False)
     45 
     46       num_attempts = 2
     47       self.m.run.with_retry(self.m.step, 'install_' + app_name, num_attempts,
     48                             cmd=['ideviceinstaller', '-i', app_package],
     49                             between_attempts_fn=uninstall_app,
     50                             infra_step=True)
     51 
     52   def step(self, name, cmd, env=None, **kwargs):
     53     bundle_id = 'com.google.%s' % cmd[0]
     54     self.m.run(self.m.step, name,
     55                cmd=['idevice-app-runner', '-s', bundle_id, '--args'] +
     56                     map(str, cmd[1:]))
     57 
     58   def _run_ios_script(self, script, first, *rest):
     59     full = self.m.path['start_dir'].join(
     60         'skia', 'platform_tools', 'ios', 'bin', 'ios_' + script)
     61     self.m.run(self.m.step,
     62                name = '%s %s' % (script, first),
     63                cmd = [full, first] + list(rest),
     64                infra_step=True)
     65 
     66   def copy_file_to_device(self, host, device):
     67     self._run_ios_script('push_file', host, device)
     68 
     69   def copy_directory_contents_to_device(self, host, device):
     70     self._run_ios_script('push_if_needed', host, device)
     71 
     72   def copy_directory_contents_to_host(self, device, host):
     73     self._run_ios_script('pull_if_needed', device, host)
     74 
     75   def remove_file_on_device(self, path):
     76     self._run_ios_script('rm', path)
     77 
     78   def create_clean_device_dir(self, path):
     79     self._run_ios_script('rm',    path)
     80     self._run_ios_script('mkdir', path)
     81 
     82   def read_file_on_device(self, path, **kwargs):
     83     full = self.m.path['start_dir'].join(
     84         'skia', 'platform_tools', 'ios', 'bin', 'ios_cat_file')
     85     rv = self.m.run(self.m.step,
     86                     name = 'cat_file %s' % path,
     87                     cmd = [full, path],
     88                     stdout=self.m.raw_io.output(),
     89                     infra_step=True,
     90                     **kwargs)
     91     return rv.stdout.rstrip() if rv and rv.stdout else None
     92