| OLD | NEW |
| 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 atexit | 5 import atexit |
| 6 import hashlib | 6 import hashlib |
| 7 import json | 7 import json |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import os.path | 10 import os.path |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 fifo_path]), | 96 fifo_path]), |
| 97 stdout=pipe) | 97 stdout=pipe) |
| 98 atexit.register(_ExitIfNeeded, stdout_cat) | 98 atexit.register(_ExitIfNeeded, stdout_cat) |
| 99 stdout_cat.wait() | 99 stdout_cat.wait() |
| 100 if on_fifo_closed: | 100 if on_fifo_closed: |
| 101 on_fifo_closed() | 101 on_fifo_closed() |
| 102 | 102 |
| 103 thread = threading.Thread(target=Run, name="StdoutRedirector") | 103 thread = threading.Thread(target=Run, name="StdoutRedirector") |
| 104 thread.start() | 104 thread.start() |
| 105 | 105 |
| 106 def _MapPort(self, device_port, host_port): | 106 def _ForwardDevicePortToHost(self, device_port, host_port): |
| 107 """Maps the device port to the host port. If |device_port| is 0, a random | 107 """Maps the device port to the host port. If |device_port| is 0, a random |
| 108 available port is chosen. Returns the device port. | 108 available port is chosen. |
| 109 |
| 110 Returns: |
| 111 The device port. |
| 109 """ | 112 """ |
| 110 def _FindAvailablePortOnDevice(): | 113 def _FindAvailablePortOnDevice(): |
| 111 opened = subprocess.check_output( | 114 opened = subprocess.check_output( |
| 112 self._CreateADBCommand(['shell', 'netstat'])) | 115 self._CreateADBCommand(['shell', 'netstat'])) |
| 113 opened = [int(x.strip().split()[3].split(':')[1]) | 116 opened = [int(x.strip().split()[3].split(':')[1]) |
| 114 for x in opened if x.startswith(' tcp')] | 117 for x in opened if x.startswith(' tcp')] |
| 115 while True: | 118 while True: |
| 116 port = random.randint(4096, 16384) | 119 port = random.randint(4096, 16384) |
| 117 if port not in opened: | 120 if port not in opened: |
| 118 return port | 121 return port |
| 119 if device_port == 0: | 122 if device_port == 0: |
| 120 device_port = _FindAvailablePortOnDevice() | 123 device_port = _FindAvailablePortOnDevice() |
| 121 subprocess.check_call(self._CreateADBCommand([ | 124 subprocess.check_call(self._CreateADBCommand([ |
| 122 "reverse", | 125 "reverse", "tcp:%d" % device_port, "tcp:%d" % host_port])) |
| 123 "tcp:%d" % device_port, | |
| 124 "tcp:%d" % host_port])) | |
| 125 | 126 |
| 126 unmap_command = self._CreateADBCommand(["reverse", "--remove", | 127 unmap_command = self._CreateADBCommand([ |
| 127 "tcp:%d" % device_port]) | 128 "reverse", "--remove", "tcp:%d" % device_port]) |
| 128 | 129 |
| 129 def _UnmapPort(): | 130 def _UnmapPort(): |
| 130 subprocess.Popen(unmap_command) | 131 subprocess.Popen(unmap_command) |
| 131 atexit.register(_UnmapPort) | 132 atexit.register(_UnmapPort) |
| 132 return device_port | 133 return device_port |
| 133 | 134 |
| 134 def _RunAdbAsRoot(self): | 135 def _RunAdbAsRoot(self): |
| 135 if self.adb_running_as_root: | 136 if self.adb_running_as_root: |
| 136 return | 137 return |
| 137 | 138 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 port: port at which the server will be available to the shell | 210 port: port at which the server will be available to the shell |
| 210 | 211 |
| 211 Returns: | 212 Returns: |
| 212 The url that the shell can use to access the content of |local_dir_path|. | 213 The url that the shell can use to access the content of |local_dir_path|. |
| 213 """ | 214 """ |
| 214 assert local_dir_path | 215 assert local_dir_path |
| 215 print 'starting http for', local_dir_path | 216 print 'starting http for', local_dir_path |
| 216 server_address = StartHttpServer(local_dir_path) | 217 server_address = StartHttpServer(local_dir_path) |
| 217 | 218 |
| 218 print 'local port=%d' % server_address[1] | 219 print 'local port=%d' % server_address[1] |
| 219 return 'http://127.0.0.1:%d/' % self._MapPort(port, | 220 return 'http://127.0.0.1:%d/' % self._ForwardDevicePortToHost( |
| 220 server_address[1]) | 221 port, server_address[1]) |
| 222 |
| 223 def ForwardHostPortToShell(self, host_port): |
| 224 """Forwards a port on the host machine to the same port wherever the shell |
| 225 is running. |
| 226 |
| 227 This is a no-op if the shell is running locally. |
| 228 """ |
| 229 assert host_port |
| 230 device_port = host_port |
| 231 subprocess.check_call(self._CreateADBCommand([ |
| 232 "forward", 'tcp:%d' % host_port, 'tcp:%d' % device_port])) |
| 233 |
| 234 unmap_command = self._CreateADBCommand([ |
| 235 "forward", "--remove", "tcp:%d" % device_port]) |
| 236 |
| 237 def _UnmapPort(): |
| 238 subprocess.Popen(unmap_command) |
| 239 atexit.register(_UnmapPort) |
| 221 | 240 |
| 222 def StartShell(self, | 241 def StartShell(self, |
| 223 arguments, | 242 arguments, |
| 224 stdout=None, | 243 stdout=None, |
| 225 on_application_stop=None): | 244 on_application_stop=None): |
| 226 """Starts the mojo shell, passing it the given arguments. | 245 """Starts the mojo shell, passing it the given arguments. |
| 227 | 246 |
| 228 The |arguments| list must contain the "--origin=" arg. SetUpLocalOrigin() | 247 The |arguments| list must contain the "--origin=" arg. SetUpLocalOrigin() |
| 229 can be used to set up a local directory on the host machine as origin. | 248 can be used to set up a local directory on the host machine as origin. |
| 230 If |stdout| is not None, it should be a valid argument for subprocess.Popen. | 249 If |stdout| is not None, it should be a valid argument for subprocess.Popen. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 tags.extend(_LOGCAT_NATIVE_TAGS) | 340 tags.extend(_LOGCAT_NATIVE_TAGS) |
| 322 if self.additional_logcat_tags is not None: | 341 if self.additional_logcat_tags is not None: |
| 323 tags.extend(self.additional_logcat_tags.split(",")) | 342 tags.extend(self.additional_logcat_tags.split(",")) |
| 324 logcat = subprocess.Popen( | 343 logcat = subprocess.Popen( |
| 325 self._CreateADBCommand(['logcat', | 344 self._CreateADBCommand(['logcat', |
| 326 '-s', | 345 '-s', |
| 327 ' '.join(tags)]), | 346 ' '.join(tags)]), |
| 328 stdout=sys.stdout) | 347 stdout=sys.stdout) |
| 329 atexit.register(_ExitIfNeeded, logcat) | 348 atexit.register(_ExitIfNeeded, logcat) |
| 330 return logcat | 349 return logcat |
| OLD | NEW |