| 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 logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import os.path | 9 import os.path |
| 10 import random | 10 import random |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 'MojoFileHelper', | 27 'MojoFileHelper', |
| 28 'MojoShellApplication', | 28 'MojoShellApplication', |
| 29 'ShellService', | 29 'ShellService', |
| 30 ] | 30 ] |
| 31 | 31 |
| 32 _MOJO_SHELL_PACKAGE_NAME = 'org.chromium.mojo.shell' | 32 _MOJO_SHELL_PACKAGE_NAME = 'org.chromium.mojo.shell' |
| 33 | 33 |
| 34 # Used to parse the output of `adb devices`. | 34 # Used to parse the output of `adb devices`. |
| 35 _ADB_DEVICES_HEADER = 'List of devices attached' | 35 _ADB_DEVICES_HEADER = 'List of devices attached' |
| 36 | 36 |
| 37 # Fixed port on which Flutter observatory is run. |
| 38 _FLUTTER_OBSERVATORY_PORT = 8181 |
| 39 |
| 37 | 40 |
| 38 _logger = logging.getLogger() | 41 _logger = logging.getLogger() |
| 39 | 42 |
| 40 | 43 |
| 41 def _exit_if_needed(process): | 44 def _exit_if_needed(process): |
| 42 """Exits |process| if it is still alive.""" | 45 """Exits |process| if it is still alive.""" |
| 43 if process.poll() is None: | 46 if process.poll() is None: |
| 44 process.kill() | 47 process.kill() |
| 45 | 48 |
| 46 | 49 |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 device_port = int(match.group(1)) | 394 device_port = int(match.group(1)) |
| 392 host_port = self._forward_host_port_to_device(0, device_port) | 395 host_port = self._forward_host_port_to_device(0, device_port) |
| 393 print ('Dart observatory available at the host at http://127.0.0.1:%d' | 396 print ('Dart observatory available at the host at http://127.0.0.1:%d' |
| 394 % host_port) | 397 % host_port) |
| 395 | 398 |
| 396 logcat_watch_thread = threading.Thread( | 399 logcat_watch_thread = threading.Thread( |
| 397 target=_forward_observatories_as_needed) | 400 target=_forward_observatories_as_needed) |
| 398 logcat_watch_thread.daemon = True | 401 logcat_watch_thread.daemon = True |
| 399 logcat_watch_thread.start() | 402 logcat_watch_thread.start() |
| 400 | 403 |
| 404 def forward_flutter_observatory_port(self): |
| 405 """Forwards the fixed port on which Flutter observatory is run.""" |
| 406 self._forward_host_port_to_device(_FLUTTER_OBSERVATORY_PORT, |
| 407 _FLUTTER_OBSERVATORY_PORT) |
| 408 |
| 401 @overrides(Shell) | 409 @overrides(Shell) |
| 402 def serve_local_directories(self, mappings, port, reuse_servers=False): | 410 def serve_local_directories(self, mappings, port, reuse_servers=False): |
| 403 assert mappings | 411 assert mappings |
| 404 if reuse_servers: | 412 if reuse_servers: |
| 405 assert port, 'Cannot reuse the server when |port| is 0.' | 413 assert port, 'Cannot reuse the server when |port| is 0.' |
| 406 server_address = ('127.0.0.1', port) | 414 server_address = ('127.0.0.1', port) |
| 407 else: | 415 else: |
| 408 server_address = http_server.start_http_server(mappings, port) | 416 server_address = http_server.start_http_server(mappings, port) |
| 409 | 417 |
| 410 return 'http://127.0.0.1:%d/' % self._forward_device_port_to_host( | 418 return 'http://127.0.0.1:%d/' % self._forward_device_port_to_host( |
| 411 port, server_address[1]) | 419 port, server_address[1]) |
| 412 | 420 |
| 413 @overrides(Shell) | 421 @overrides(Shell) |
| 414 def forward_host_port_to_shell(self, host_port): | 422 def forward_host_port_to_shell(self, host_port): |
| 415 self._forward_host_port_to_device(host_port, host_port) | 423 self._forward_host_port_to_device(host_port, host_port) |
| 416 | 424 |
| 417 @overrides(Shell) | 425 @overrides(Shell) |
| 418 def run(self, arguments): | 426 def run(self, arguments): |
| 419 self.clean_logs() | 427 self.clean_logs() |
| 420 self.forward_observatory_ports() | 428 self.forward_observatory_ports() |
| 429 self.forward_flutter_observatory_port() |
| 421 | 430 |
| 422 p = self.show_logs() | 431 p = self.show_logs() |
| 423 self.start_shell(arguments, sys.stdout, p.terminate) | 432 self.start_shell(arguments, sys.stdout, p.terminate) |
| 424 p.wait() | 433 p.wait() |
| 425 return None | 434 return None |
| 426 | 435 |
| 427 @overrides(Shell) | 436 @overrides(Shell) |
| 428 def run_and_get_output(self, arguments, timeout=None): | 437 def run_and_get_output(self, arguments, timeout=None): |
| 429 class Results: | 438 class Results: |
| 430 """Workaround for Python scoping rules that prevent assigning to variables | 439 """Workaround for Python scoping rules that prevent assigning to variables |
| 431 from the outer scope. | 440 from the outer scope. |
| 432 """ | 441 """ |
| 433 output = None | 442 output = None |
| 434 | 443 |
| 435 def do_run(): | 444 def do_run(): |
| 436 (r, w) = os.pipe() | 445 (r, w) = os.pipe() |
| 437 with os.fdopen(r, 'r') as rf: | 446 with os.fdopen(r, 'r') as rf: |
| 438 with os.fdopen(w, 'w') as wf: | 447 with os.fdopen(w, 'w') as wf: |
| 439 self.start_shell(arguments, wf, wf.close) | 448 self.start_shell(arguments, wf, wf.close) |
| 440 Results.output = rf.read() | 449 Results.output = rf.read() |
| 441 | 450 |
| 442 run_thread = threading.Thread(target=do_run) | 451 run_thread = threading.Thread(target=do_run) |
| 443 run_thread.start() | 452 run_thread.start() |
| 444 run_thread.join(timeout) | 453 run_thread.join(timeout) |
| 445 | 454 |
| 446 if run_thread.is_alive(): | 455 if run_thread.is_alive(): |
| 447 self.stop_shell() | 456 self.stop_shell() |
| 448 return None, Results.output, True | 457 return None, Results.output, True |
| 449 return None, Results.output, False | 458 return None, Results.output, False |
| OLD | NEW |