1*288bf522SAndroid Build Coastguard Worker# 2*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2024 The Android Open Source Project 3*288bf522SAndroid Build Coastguard Worker# 4*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 5*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 6*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at 7*288bf522SAndroid Build Coastguard Worker# 8*288bf522SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 9*288bf522SAndroid Build Coastguard Worker# 10*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 11*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 12*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 14*288bf522SAndroid Build Coastguard Worker# limitations under the License. 15*288bf522SAndroid Build Coastguard Worker# 16*288bf522SAndroid Build Coastguard Worker# Implementation taken from external/perfetto/tools/record_android_trace. 17*288bf522SAndroid Build Coastguard Worker# 18*288bf522SAndroid Build Coastguard Worker 19*288bf522SAndroid Build Coastguard Workerimport webbrowser 20*288bf522SAndroid Build Coastguard Workerimport socketserver 21*288bf522SAndroid Build Coastguard Workerimport http.server 22*288bf522SAndroid Build Coastguard Workerimport os 23*288bf522SAndroid Build Coastguard Worker 24*288bf522SAndroid Build Coastguard Worker 25*288bf522SAndroid Build Coastguard Workerclass HttpHandler(http.server.SimpleHTTPRequestHandler): 26*288bf522SAndroid Build Coastguard Worker 27*288bf522SAndroid Build Coastguard Worker def end_headers(self): 28*288bf522SAndroid Build Coastguard Worker self.send_header("Access-Control-Allow-Origin", self.server.allow_origin) 29*288bf522SAndroid Build Coastguard Worker self.send_header("Cache-Control", "no-cache") 30*288bf522SAndroid Build Coastguard Worker super().end_headers() 31*288bf522SAndroid Build Coastguard Worker 32*288bf522SAndroid Build Coastguard Worker def do_GET(self): 33*288bf522SAndroid Build Coastguard Worker if self.path != "/" + self.server.expected_fname: 34*288bf522SAndroid Build Coastguard Worker self.send_error(404, "File not found") 35*288bf522SAndroid Build Coastguard Worker return 36*288bf522SAndroid Build Coastguard Worker self.server.fname_get_completed = True 37*288bf522SAndroid Build Coastguard Worker super().do_GET() 38*288bf522SAndroid Build Coastguard Worker 39*288bf522SAndroid Build Coastguard Worker def do_POST(self): 40*288bf522SAndroid Build Coastguard Worker self.send_error(404, "File not found") 41*288bf522SAndroid Build Coastguard Worker 42*288bf522SAndroid Build Coastguard Worker def log_message(self, format, *args): 43*288bf522SAndroid Build Coastguard Worker pass 44*288bf522SAndroid Build Coastguard Worker 45*288bf522SAndroid Build Coastguard Worker 46*288bf522SAndroid Build Coastguard Workerdef open_trace(path, origin): 47*288bf522SAndroid Build Coastguard Worker PORT = 9001 48*288bf522SAndroid Build Coastguard Worker path = os.path.abspath(path) 49*288bf522SAndroid Build Coastguard Worker os.chdir(os.path.dirname(path)) 50*288bf522SAndroid Build Coastguard Worker fname = os.path.basename(path) 51*288bf522SAndroid Build Coastguard Worker socketserver.TCPServer.allow_reuse_address = True 52*288bf522SAndroid Build Coastguard Worker with socketserver.TCPServer(("127.0.0.1", PORT), HttpHandler) as httpd: 53*288bf522SAndroid Build Coastguard Worker address = (f"{origin}/#!/?url=http://127.0.0.1:" 54*288bf522SAndroid Build Coastguard Worker f"{PORT}/{fname}&referrer=open_trace_in_ui") 55*288bf522SAndroid Build Coastguard Worker webbrowser.open_new_tab(address) 56*288bf522SAndroid Build Coastguard Worker httpd.expected_fname = fname 57*288bf522SAndroid Build Coastguard Worker httpd.fname_get_completed = None 58*288bf522SAndroid Build Coastguard Worker httpd.allow_origin = origin 59*288bf522SAndroid Build Coastguard Worker while httpd.fname_get_completed is None: 60*288bf522SAndroid Build Coastguard Worker httpd.handle_request() 61