1#!/usr/bin/env python3 2 3"""A tool to delete permalinks and traces behind them. 4 5To be used by the Perfetto team, who has write access to the GCS bucket. 6""" 7 8import json 9import logging 10import re 11import subprocess 12import sys 13 14from typing import List 15 16GCS_BUCKET = 'perfetto-ui-data' 17GCS_HTTP = 'https://storage.googleapis.com/%s/' % GCS_BUCKET 18 19 20def delete_gcs_obj(url: str, gcs_delete_list: List[str]): 21 if not url.startswith(GCS_HTTP): 22 logging.error('The URL %s should start with %s', url, GCS_HTTP) 23 return 24 gs_url = 'gs://%s/%s' % (GCS_BUCKET, url[len(GCS_HTTP):]) 25 gcs_delete_list.append(gs_url) 26 27 28def delete_permalink_uuid(uuid: str, gcs_delete_list: List[str]): 29 state_url = GCS_HTTP + uuid 30 delete_gcs_obj(state_url, gcs_delete_list) 31 state_json = subprocess.check_output(['curl', '-Ls', state_url]) 32 state = json.loads(state_json) 33 trace_url = state['engine']['source']['url'] 34 delete_gcs_obj(trace_url, gcs_delete_list) 35 36 37def main(): 38 gcs_delete_list = [] 39 if sys.stdin.isatty(): 40 logging.warn('This tool expects a list of uuids or https://ui.perfetto.dev/#!#?s=deadbeef') 41 42 for line in sys.stdin.readlines(): 43 line = line.strip() 44 m = re.match(r'.*?\b([a-f0-9]{64})', line) 45 if not m: 46 logging.error('Could not find a 64 hex UUID from %s', line) 47 continue 48 uuid = m.group(1) 49 delete_permalink_uuid(uuid, gcs_delete_list) 50 51 if len(gcs_delete_list) == 0: 52 logging.info('No object to delete, quitting without taking any action') 53 return 0 54 55 print('Removing the following objects:') 56 for gs_uri in gcs_delete_list: 57 print(' ', gs_uri) 58 subprocess.check_call(['gsutil', '-m', 'rm', '-f', '-a'] + gcs_delete_list) 59 60 61if __name__ == '__main__': 62 sys.exit(main()) 63