Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(810)

Side by Side Diff: tools/telemetry/telemetry/core/forwarders/android_forwarder.py

Issue 358993003: [Android] Switch to DeviceUtils versions of file functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import logging 5 import logging
6 import os 6 import os
7 import re 7 import re
8 import socket 8 import socket
9 import struct 9 import struct
10 import subprocess 10 import subprocess
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 169
170 assert self._IsRndisSupported(), 'Device does not support RNDIS.' 170 assert self._IsRndisSupported(), 'Device does not support RNDIS.'
171 self._CheckConfigureNetwork() 171 self._CheckConfigureNetwork()
172 172
173 @property 173 @property
174 def host_ip(self): 174 def host_ip(self):
175 return self._host_ip 175 return self._host_ip
176 176
177 def _IsRndisSupported(self): 177 def _IsRndisSupported(self):
178 """Checks that the device has RNDIS support in the kernel.""" 178 """Checks that the device has RNDIS support in the kernel."""
179 return self._device.old_interface.FileExistsOnDevice( 179 return self._device.FileExists('%s/f_rndis/device' % self._RNDIS_DEVICE)
180 '%s/f_rndis/device' % self._RNDIS_DEVICE)
181 180
182 def _WaitForDevice(self): 181 def _WaitForDevice(self):
183 self._device.old_interface.Adb().SendCommand('wait-for-device') 182 self._device.old_interface.Adb().SendCommand('wait-for-device')
184 183
185 def _FindDeviceRndisInterface(self): 184 def _FindDeviceRndisInterface(self):
186 """Returns the name of the RNDIS network interface if present.""" 185 """Returns the name of the RNDIS network interface if present."""
187 config = self._device.RunShellCommand('netcfg') 186 config = self._device.RunShellCommand('netcfg')
188 interfaces = [line.split()[0] for line in config] 187 interfaces = [line.split()[0] for line in config]
189 candidates = [iface for iface in interfaces if re.match('rndis|usb', iface)] 188 candidates = [iface for iface in interfaces if re.match('rndis|usb', iface)]
190 if candidates: 189 if candidates:
191 assert len(candidates) == 1, 'Found more than one rndis device!' 190 assert len(candidates) == 1, 'Found more than one rndis device!'
192 return candidates[0] 191 return candidates[0]
193 192
194 def _EnumerateHostInterfaces(self): 193 def _EnumerateHostInterfaces(self):
195 host_platform = platform.GetHostPlatform().GetOSName() 194 host_platform = platform.GetHostPlatform().GetOSName()
196 if host_platform == 'linux': 195 if host_platform == 'linux':
197 return subprocess.check_output(['ip', 'addr']).splitlines() 196 return subprocess.check_output(['ip', 'addr']).splitlines()
198 if host_platform == 'mac': 197 if host_platform == 'mac':
199 return subprocess.check_output(['ifconfig']).splitlines() 198 return subprocess.check_output(['ifconfig']).splitlines()
200 raise NotImplementedError('Platform %s not supported!' % host_platform) 199 raise NotImplementedError('Platform %s not supported!' % host_platform)
201 200
202 def _FindHostRndisInterface(self): 201 def _FindHostRndisInterface(self):
203 """Returns the name of the host-side network interface.""" 202 """Returns the name of the host-side network interface."""
204 interface_list = self._EnumerateHostInterfaces() 203 interface_list = self._EnumerateHostInterfaces()
205 ether_address = self._device.old_interface.GetFileContents( 204 ether_address = self._device.ReadFile(
206 '%s/f_rndis/ethaddr' % self._RNDIS_DEVICE)[0] 205 '%s/f_rndis/ethaddr' % self._RNDIS_DEVICE)[0]
207 interface_name = None 206 interface_name = None
208 for line in interface_list: 207 for line in interface_list:
209 if not line.startswith((' ', '\t')): 208 if not line.startswith((' ', '\t')):
210 interface_name = line.split(':')[-2].strip() 209 interface_name = line.split(':')[-2].strip()
211 elif ether_address in line: 210 elif ether_address in line:
212 return interface_name 211 return interface_name
213 212
214 def _WriteProtectedFile(self, path, contents): 213 def _WriteProtectedFile(self, path, contents):
215 subprocess.check_call( 214 subprocess.check_call(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 # For some combinations of devices and host kernels, adb won't work unless the 247 # For some combinations of devices and host kernels, adb won't work unless the
249 # interface is up, but if we bring it up immediately, it will break adb. 248 # interface is up, but if we bring it up immediately, it will break adb.
250 #sleep 1 249 #sleep 1
251 #ifconfig rndis0 192.168.42.2 netmask 255.255.255.0 up 250 #ifconfig rndis0 192.168.42.2 netmask 255.255.255.0 up
252 echo DONE >> %(prefix)s.log 251 echo DONE >> %(prefix)s.log
253 } 252 }
254 253
255 doit & 254 doit &
256 """ % {'dev': self._RNDIS_DEVICE, 'functions': 'rndis,adb', 255 """ % {'dev': self._RNDIS_DEVICE, 'functions': 'rndis,adb',
257 'prefix': script_prefix } 256 'prefix': script_prefix }
258 self._device.old_interface.SetFileContents('%s.sh' % script_prefix, script) 257 self._device.WriteFile('%s.sh' % script_prefix, script)
259 # TODO(szym): run via su -c if necessary. 258 # TODO(szym): run via su -c if necessary.
260 self._device.RunShellCommand('rm %s.log' % script_prefix) 259 self._device.RunShellCommand('rm %s.log' % script_prefix)
261 self._device.RunShellCommand('. %s.sh' % script_prefix) 260 self._device.RunShellCommand('. %s.sh' % script_prefix)
262 self._WaitForDevice() 261 self._WaitForDevice()
263 result = self._device.old_interface.GetFileContents( 262 result = self._device.ReadFile('%s.log' % script_prefix)
264 '%s.log' % script_prefix)
265 assert any('DONE' in line for line in result), 'RNDIS script did not run!' 263 assert any('DONE' in line for line in result), 'RNDIS script did not run!'
266 264
267 def _CheckEnableRndis(self, force): 265 def _CheckEnableRndis(self, force):
268 """Enables the RNDIS network interface, retrying if necessary. 266 """Enables the RNDIS network interface, retrying if necessary.
269 Args: 267 Args:
270 force: Disable RNDIS first, even if it appears already enabled. 268 force: Disable RNDIS first, even if it appears already enabled.
271 Returns: 269 Returns:
272 device_iface: RNDIS interface name on the device 270 device_iface: RNDIS interface name on the device
273 host_iface: corresponding interface name on the host 271 host_iface: corresponding interface name on the host
274 """ 272 """
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 def _CheckConfigureNetwork(self): 436 def _CheckConfigureNetwork(self):
439 """Enables RNDIS and configures it, retrying until we have connectivity.""" 437 """Enables RNDIS and configures it, retrying until we have connectivity."""
440 force = False 438 force = False
441 for _ in range(3): 439 for _ in range(3):
442 device_iface, host_iface = self._CheckEnableRndis(force) 440 device_iface, host_iface = self._CheckEnableRndis(force)
443 self._ConfigureNetwork(device_iface, host_iface) 441 self._ConfigureNetwork(device_iface, host_iface)
444 if self._TestConnectivity(): 442 if self._TestConnectivity():
445 return 443 return
446 force = True 444 force = True
447 raise Exception('No connectivity, giving up.') 445 raise Exception('No connectivity, giving up.')
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698