aboutsummaryrefslogtreecommitdiff
path: root/brunch/lib/core/util.py
blob: f825a4a737827a682a74825f33b4f48a3385263c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Brunch util functions"""

import os
import string

import core

ANDROID_PRODUCTS_MK = 'AndroidProducts.mk'
_BRUNCH_BASE = None

def GetBrunchConfigDir():
  global _BRUNCH_BASE
  if _BRUNCH_BASE:
    return _BRUNCH_BASE
  if os.environ.get('BRILLO_CONFIG_DIR'):
    path = os.environ['BRILLO_CONFIG_DIR']
    if not os.path.isdir(path):
      # TODO(leecam) Change to BrunchException
      raise
  else:
    basedir = os.path.expanduser("~")
    path = os.path.join(basedir, '.brillo')
    if not os.path.isdir(path):
      os.makedirs(path)
  _BRUNCH_BASE = path
  return path

def GetBDKPath(relpath = None):
  """Find the base of the BDK"""
  core_path = os.path.dirname(core.__file__)
  result = os.path.abspath(os.path.join(core_path,
      '..', '..', '..', '..', '..'))
  if relpath:
    result = os.path.join(result, relpath)
  return result

def GetDevToolsPath(relpath = None):
  result = GetBDKPath('tools/bdk')
  if relpath:
    result = os.path.join(result, relpath)
  return result

USER_DB = os.path.join(GetBrunchConfigDir(), 'config.db')
METRICS_DB = os.path.join(GetBrunchConfigDir(), 'metrics.db')

def GetBDKVersion():
  """Find the BDK version"""
  # Cache the version.
  # TODO(wad) make this less hacky.
  if vars().get('bdk_version') is not None:
    return vars().get('bdk_version')
  core_path = os.path.dirname(core.__file__)
  version_path = os.path.abspath(os.path.join(core_path,
      '..', '..', '..', 'VERSION'))
  version = 0  # Default version.
  with open(version_path, 'r') as f:
    version = f.readline().strip()
  vars()['bdk_version'] = version
  return version

def GetProductDir():
   """Walks from cwd upward to find the product root"""
   p = os.getcwd()
   while p != '/':
     if os.path.isfile(os.path.join(p, ANDROID_PRODUCTS_MK)):
       return p
     p = os.path.abspath(os.path.join(p, '..'))
   return None

def AsShellArgs(args):
  return ' '.join(['"%s"' % a.replace('"', '\\"') for a in args])

def GetExitCode(status):
  """Convert an os.wait status code to a shell return value"""
  if os.WIFEXITED(status):
    return os.WEXITSTATUS(status)
  if os.WIFSTOPPED(status):
    return 128+os.WSTOPSIG(status)
  if os.WIFSIGNALED(status):
    return 128+os.WTERMSIG(status)
  return 1