1# Copyright (C) 2016 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 15"""Various utility functions""" 16 17import logging 18import os 19import shlex 20import subprocess 21 22 23# The default location in which symbols and minidumps will be saved. 24_DEFAULT_ARTIFACT_CACHE_ROOT = os.environ.get('ARC_ARTIFACT_CACHE_ROOT', 25 '/tmp/arc-artifact-cache') 26 27 28def get_command_str(command): 29 """Returns a quoted version of the command, friendly to copy/paste.""" 30 return ' '.join(shlex.quote(arg) for arg in command) 31 32 33def check_call(*subprocess_args, dryrun=False, **kwargs): 34 """Runs a subprocess and returns its exit code.""" 35 if logging.getLogger().isEnabledFor(logging.DEBUG): 36 logging.debug('Calling: %s', get_command_str(subprocess_args)) 37 if dryrun: 38 return 39 try: 40 return subprocess.check_call(subprocess_args, **kwargs) 41 except subprocess.CalledProcessError as e: 42 logging.error('Error while executing %s', get_command_str(subprocess_args)) 43 logging.error(e.output) 44 raise 45 46 47def check_output(*subprocess_args, dryrun=False, **kwargs): 48 """Runs a subprocess and returns its output.""" 49 if logging.getLogger().isEnabledFor(logging.DEBUG): 50 logging.debug('Calling: %s', get_command_str(subprocess_args)) 51 if dryrun: 52 logging.info('Cannot return any output without running the command. ' 53 'Returning an empty string instead.') 54 return '' 55 try: 56 return subprocess.check_output(subprocess_args, universal_newlines=True, 57 **kwargs) 58 except subprocess.CalledProcessError as e: 59 logging.error('Error while executing %s', get_command_str(subprocess_args)) 60 logging.error(e.output) 61 raise 62 63 64def makedirs(path): 65 """Makes directories if necessary, like 'mkdir -p'""" 66 if not os.path.exists(path): 67 os.makedirs(path) 68 69 70def get_prebuilt(tool): 71 """Locates a prebuilt file to run.""" 72 return os.path.abspath(os.path.join( 73 os.path.dirname(os.path.dirname(__file__)), 'prebuilt/x86-linux/', tool)) 74 75 76def helper_temp_path(*path, artifact_cache_root=_DEFAULT_ARTIFACT_CACHE_ROOT): 77 """Returns the path to use for temporary/cached files.""" 78 return os.path.join(artifact_cache_root, *path) 79 80 81def get_product_arch(product): 82 """Returns the architecture of a given target |product|.""" 83 # The prefix can itself have other prefixes, like 'generic_' or 'aosp_'. 84 product_prefix = 'cheets_' 85 86 idx = product.index(product_prefix) 87 assert idx >= 0, 'Unrecognized product name: %s' % product 88 return product[idx + len(product_prefix):] 89