• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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
6from recipe_engine import recipe_api
7
8import default
9import ntpath
10import ssh
11import subprocess  # TODO(borenet): No! Remove this.
12
13
14"""Win SSH flavor, used for running code on Windows via an SSH connection.
15
16Copied from chromebook.py and modified for Windows.
17"""
18
19
20class WinSSHFlavor(ssh.SSHFlavor):
21
22  def __init__(self, m):
23    super(WinSSHFlavor, self).__init__(m)
24    self.remote_homedir = 'C:\\Users\\chrome-bot\\botdata\\'
25    self.device_dirs = default.DeviceDirs(
26      bin_dir       = self.device_path_join(self.remote_homedir, 'bin'),
27      dm_dir        = self.device_path_join(self.remote_homedir, 'dm_out'),
28      perf_data_dir = self.device_path_join(self.remote_homedir, 'perf'),
29      resource_dir  = self.device_path_join(self.remote_homedir, 'resources'),
30      images_dir    = self.device_path_join(self.remote_homedir, 'images'),
31      lotties_dir   = self.device_path_join(self.remote_homedir, 'lotties'),
32      skp_dir       = self.device_path_join(self.remote_homedir, 'skps'),
33      svg_dir       = self.device_path_join(self.remote_homedir, 'svgs'),
34      mskp_dir      = self.device_path_join(self.remote_homedir, 'mskp'),
35      tmp_dir       = self.remote_homedir)
36    self._empty_dir = self.device_path_join(self.remote_homedir, 'empty')
37
38
39  def _cmd(self, title, cmd, infra_step=True, fail_errorlevel=1, **kwargs):
40    return self.m.run(self.m.python, title,
41                      script=self.module.resource('win_ssh_cmd.py'),
42                      args=[self.user_ip, cmd, fail_errorlevel],
43                      infra_step=infra_step, **kwargs)
44
45  def ensure_device_dir(self, path):
46    self._cmd('mkdir %s' % path, 'if not exist "%s" md "%s"' % (path, path))
47
48  def _rmdir(self, path):
49    self._cmd('rmdir %s' % path, 'if exist "%s" rd "%s"' % (path, path))
50
51  def device_path_join(self, *args):
52    return ntpath.join(*args)
53
54  def install(self):
55    super(WinSSHFlavor, self).install()
56
57    # Ensure that our empty dir is actually empty.
58    self._rmdir(self._empty_dir)
59    self.ensure_device_dir(self._empty_dir)
60
61    self.create_clean_device_dir(self.device_dirs.bin_dir)
62
63  def create_clean_device_dir(self, path):
64    # Based on https://stackoverflow.com/a/98069 and
65    # https://superuser.com/a/346112.
66    self._cmd('clean %s' % path,
67              'robocopy /mir "%s" "%s"' % (self._empty_dir, path),
68              fail_errorlevel=8)
69
70  def read_file_on_device(self, path, **kwargs):
71    with self.m.step.nest('read %s' % path):
72      with self.m.tempfile.temp_dir('read_file_on_device') as tmp:
73        host_path = tmp.join(ntpath.basename(path))
74        device_path = self.scp_device_path(path)
75        ok = self._run('scp %s %s' % (device_path, host_path),
76                       cmd=['scp', device_path, host_path],
77                       infra_step=True, **kwargs)
78        # TODO(dogben): Should readfile respect fail_build_on_failure and
79        # abort_on_failure?
80        if ok:
81          return self.m.run.readfile(host_path)
82
83  def remove_file_on_device(self, path):
84    self._cmd('rm %s' % path, 'if exist "%s" del "%s"' % (path, path))
85
86  def _copy_dir(self, src, dest):
87    self._run('scp -r %s %s' % (src, dest),
88              cmd=['scp', '-r', src, dest], infra_step=True)
89
90  def copy_directory_contents_to_device(self, host_path, device_path):
91    # Callers expect that the destination directory is replaced, which is not
92    # how scp works when the destination is a directory. Instead scp to tmp_dir
93    # and then robocopy to the correct destination.
94    # Other flavors use a glob and subprocess with shell=True to copy the
95    # contents of host_path; however, there are a lot of ways POSIX shell
96    # interpretation could mess up Windows path names.
97    with self.m.step.nest('copy %s to device' % host_path):
98      tmp_pardir = self.device_path_join(
99          self.device_dirs.tmp_dir,
100          'tmp_copy_directory_contents_to_device')
101      self.create_clean_device_dir(tmp_pardir)
102      tmpdir = self.device_path_join(tmp_pardir,
103                                     self.m.path.basename(host_path))
104      self._copy_dir(host_path, self.scp_device_path(tmpdir))
105      self._cmd('copy %s to %s' % (tmpdir, device_path),
106                'robocopy /mir "%s" "%s"' % (tmpdir, device_path),
107                fail_errorlevel=8)
108
109  def copy_directory_contents_to_host(self, device_path, host_path):
110    # Note that the glob in src is interpreted by the remote shell.
111    src = self.scp_device_path(self.device_path_join(device_path, '*'))
112    self._copy_dir(src, host_path)
113
114  def step(self, name, cmd, infra_step=False, **kwargs):
115    # There may be DLLs in the same dir as the executable that must be loaded
116    # (yes, Windows allows overriding system DLLs with files in the local
117    # directory). For simplicity, just copy the entire dir to the device.
118    self.copy_directory_contents_to_device(self.host_dirs.bin_dir,
119                                           self.device_dirs.bin_dir)
120    device_bin = self.device_path_join(self.device_dirs.bin_dir, cmd[0])
121
122    # Copy PowerShell script to device.
123    ps = 'win_run_and_check_log.ps1'
124    device_ps = self.device_path_join(self.device_dirs.bin_dir, ps)
125    self.copy_file_to_device(self.module.resource(ps), device_ps)
126
127    cmd = ['powershell', '-ExecutionPolicy', 'Unrestricted', '-File', device_ps,
128           device_bin] + cmd[1:]
129    self._cmd(name, subprocess.list2cmdline(map(str, cmd)),
130              infra_step=infra_step, **kwargs)
131