1#!/usr/bin/env python3 2# Copyright (C) 2020 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import subprocess 18import time 19from urllib import request, error 20 21from .loader import get_loader 22 23 24def load_shell(bin_path=None, unique_port=False): 25 shell_path = get_loader().get_shell_path(bin_path=bin_path) 26 port, url = get_loader().get_free_port(unique_port=unique_port) 27 p = subprocess.Popen([shell_path, '-D', '--http-port', port], 28 stdout=subprocess.DEVNULL) 29 30 while True: 31 try: 32 if p.poll() != None: 33 if unique_port: 34 raise Exception( 35 "Random port allocation failed, please file a bug at https://goto.google.com/perfetto-bug" 36 ) 37 raise Exception( 38 "Trace processor failed to start, please file a bug at https://goto.google.com/perfetto-bug" 39 ) 40 req = request.urlretrieve(f'http://{url}/status') 41 time.sleep(1) 42 break 43 except error.URLError: 44 pass 45 46 return url, p 47