• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16from typing import BinaryIO, Dict, Optional, Tuple
17
18# Limit parsing file to 1MB to maintain parity with the UI
19MAX_BYTES_LOADED = 1 * 1024 * 1024
20
21
22def file_generator(path: str):
23  with open(path, 'rb') as f:
24    yield from read_generator(f)
25
26
27def read_generator(trace: BinaryIO):
28  while True:
29    chunk = trace.read(MAX_BYTES_LOADED)
30    if not chunk:
31      break
32    yield chunk
33
34
35def merge_dicts(a: Dict[str, str], b: Dict[str, str]):
36  return {**a, **b}
37
38
39def parse_trace_uri(uri: str) -> Tuple[Optional[str], str]:
40  # This is definitely a path and not a URI
41  if uri.startswith('/') or uri.startswith('.'):
42    return None, uri
43
44  # If there's no colon, it cannot be a URI
45  idx = uri.find(':')
46  if idx == -1:
47    return None, uri
48
49  # If there is only a single character before the colon
50  # this is likely a Windows path; throw an error on other platforms
51  # to prevent single character names causing issues on Windows.
52  if idx == 1:
53    if os.name != 'nt':
54      raise Exception('Single character resolvers are not allowed')
55    return None, uri
56
57  return (uri[:idx], uri[idx + 1:])
58