summaryrefslogtreecommitdiff
path: root/lib/bootstrap_lib.py
blob: 9734f2ecba667e6f418140893eb53978b572162b (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
98
99
100
101
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Common utilities used by the chromium/bootstrap scripts."""

from __future__ import print_function

import os

from chromite.lib import cros_build_lib
from chromite.lib import project_sdk
from chromite.lib import workspace_lib


# This is the subdirectory of the bootstrap, where we store SDKs.
SDK_CHECKOUTS = 'sdk_checkouts'


# This env is used to remember the bootstrap path in child processes.
BOOTSTRAP_PATH_ENV = 'BRILLO_BOOTSTRAP_PATH'


def FindBootstrapPath(save_to_env=False):
  """Find the bootstrap directory.

  This is only possible, if the process was initially launched from a bootstrap
  environment, and isn't inside a chroot.

  Args:
    save_to_env: If true, preserve the bootstrap path in an ENV for child
                 processes. Only intended for the brillo bootstrap wrapper.

  Returns:
   Path to root of bootstrap, or None.
  """
  # We never have access to bootstrap if we are inside the chroot.
  if cros_build_lib.IsInsideChroot():
    return None

  # See if the path has already been determined, especially in a parent wrapper
  # process.
  env_path = os.environ.get(BOOTSTRAP_PATH_ENV)
  if env_path:
    return env_path

  # Base the bootstrap location on our current location, and remember it.
  new_path = os.path.realpath(os.path.join(
      os.path.abspath(__file__), '..', '..'))

  # No repo checkout is a valid bootstrap environment, because the bootstrap
  # environment holds repo checkouts inside SDK_CHECKOUTS, and repos cannot
  # exist inside other repos.
  if project_sdk.FindRepoRoot(new_path):
    return None

  if save_to_env:
    os.environ[BOOTSTRAP_PATH_ENV] = new_path

  return new_path


def ComputeSdkPath(bootstrap_path, version):
  """What directory should an SDK be in.

  Args:
    bootstrap_path: Bootstrap root directory, or None.
    version: Version of the SDK.

  Returns:
    Path in which SDK version should be stored, or None.
  """
  if bootstrap_path is None:
    return None

  return os.path.join(bootstrap_path, SDK_CHECKOUTS, version)


def GetActiveSdkPath(bootstrap_path, workspace_path):
  """Find the SDK Path associated with a given workspace.

  Most code should use constants.SOURCE_ROOT instead.

  Args:
    bootstrap_path: Path directory of the bootstrap dir (FindBootstrapPath()).
    workspace_path: Path directory of the workspace (FindWorkspacePath()).

  Returns:
    Path to root directory of SDK, if there is an active one, and it exists.
  """
  if bootstrap_path is None:
    return None

  version = workspace_lib.GetActiveSdkVersion(workspace_path)
  if version is None:
    return None

  sdk_root = ComputeSdkPath(bootstrap_path, version)

  # Returns None if there is no active SDK version, or if it's not installed.
  return sdk_root if os.path.isdir(sdk_root) else None