xref: /aosp_15_r20/libcore/tools/testmapping/util.py (revision 89a6322812dc8573315e60046e7959c50dad91d4)
1*89a63228SAndroid Build Coastguard Worker#
2*89a63228SAndroid Build Coastguard Worker#   Copyright 2020 - The Android Open Source Project
3*89a63228SAndroid Build Coastguard Worker#
4*89a63228SAndroid Build Coastguard Worker#   Licensed under the Apache License, Version 2.0 (the "License");
5*89a63228SAndroid Build Coastguard Worker#   you may not use this file except in compliance with the License.
6*89a63228SAndroid Build Coastguard Worker#   You may obtain a copy of the License at
7*89a63228SAndroid Build Coastguard Worker#
8*89a63228SAndroid Build Coastguard Worker#       http://www.apache.org/licenses/LICENSE-2.0
9*89a63228SAndroid Build Coastguard Worker#
10*89a63228SAndroid Build Coastguard Worker#   Unless required by applicable law or agreed to in writing, software
11*89a63228SAndroid Build Coastguard Worker#   distributed under the License is distributed on an "AS IS" BASIS,
12*89a63228SAndroid Build Coastguard Worker#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*89a63228SAndroid Build Coastguard Worker#   See the License for the specific language governing permissions and
14*89a63228SAndroid Build Coastguard Worker#   limitations under the License.
15*89a63228SAndroid Build Coastguard Worker
16*89a63228SAndroid Build Coastguard Worker"""Common functions shared between the scripts."""
17*89a63228SAndroid Build Coastguard Worker
18*89a63228SAndroid Build Coastguard Workerimport os
19*89a63228SAndroid Build Coastguard Worker
20*89a63228SAndroid Build Coastguard Worker
21*89a63228SAndroid Build Coastguard Workerdef android_repository_root():
22*89a63228SAndroid Build Coastguard Worker  """Returns the root of the tree."""
23*89a63228SAndroid Build Coastguard Worker  if 'ANDROID_BUILD_TOP' not in os.environ:
24*89a63228SAndroid Build Coastguard Worker    raise Exception('Environment variable ANDROID_BUILD_TOP not set')
25*89a63228SAndroid Build Coastguard Worker  return os.environ['ANDROID_BUILD_TOP']
26*89a63228SAndroid Build Coastguard Worker
27*89a63228SAndroid Build Coastguard Worker
28*89a63228SAndroid Build Coastguard Workerdef printable_path(filename):
29*89a63228SAndroid Build Coastguard Worker  """Returns the filename relative to the current dir, if it's under it."""
30*89a63228SAndroid Build Coastguard Worker  relative_path = os.path.relpath(filename)
31*89a63228SAndroid Build Coastguard Worker  if not relative_path.startswith('../'):
32*89a63228SAndroid Build Coastguard Worker    return relative_path
33*89a63228SAndroid Build Coastguard Worker  else:
34*89a63228SAndroid Build Coastguard Worker    return filename
35