-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathlinuxbrew.py
103 lines (86 loc) · 3.86 KB
/
linuxbrew.py
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
102
103
# Copyright (c) YugaByte, Inc.
#
# 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.
#
"""
Helps find the Linuxbrew installation.
"""
import os
import sys
from yb.command_util import run_program
from yb.common_util import safe_path_join, YB_SRC_ROOT
from typing import Optional
g_linuxbrew_dir: Optional[str] = None
g_build_root: Optional[str] = None
class LinuxbrewHome:
def __init__(self, build_root: Optional[str] = None) -> None:
old_build_root = os.environ.get('BUILD_ROOT')
if build_root is not None:
os.environ['BUILD_ROOT'] = build_root
else:
build_root = os.environ.get('BUILD_ROOT')
self.linuxbrew_dir = None
self.linuxbrew_link_target = None
self.cellar_glibc_dir = None
self.ldd_path = None
self.ld_so_path = None
self.patchelf_path = None
try:
find_script_result = run_program(os.path.join(
YB_SRC_ROOT, 'build-support', 'find_linuxbrew.sh'))
linuxbrew_dir = find_script_result.stdout.strip()
if not linuxbrew_dir:
return
if not os.path.isdir(linuxbrew_dir) and os.path.exists('/etc/centos-release'):
raise RuntimeError(
("Directory returned by the '{}' script does not exist: '{}'. " +
"This is only an error on CentOS. Details: {}").format(
find_script_result.program_path,
linuxbrew_dir,
find_script_result))
self.linuxbrew_dir = os.path.realpath(linuxbrew_dir)
# Directories derived from the Linuxbrew top-level one.
self.linuxbrew_link_target = os.path.realpath(linuxbrew_dir)
self.cellar_glibc_dir = safe_path_join([self.linuxbrew_dir, 'Cellar', 'glibc'])
self.ldd_path = safe_path_join([self.linuxbrew_dir, 'bin', 'ldd'])
self.ld_so_path = safe_path_join([self.linuxbrew_dir, 'lib', 'ld.so'])
self.patchelf_path = safe_path_join([self.linuxbrew_dir, 'bin', 'patchelf'])
finally:
if old_build_root is None:
if 'BUILD_ROOT' in os.environ:
del os.environ['BUILD_ROOT']
else:
os.environ['BUILD_ROOT'] = old_build_root
def path_is_in_linuxbrew_dir(self, path: str) -> bool:
if not self.is_enabled():
return False
path = os.path.abspath(path)
assert self.linuxbrew_dir is not None
assert self.linuxbrew_link_target is not None
return (path.startswith(self.linuxbrew_dir + '/') or
path.startswith(self.linuxbrew_link_target + '/'))
def get_human_readable_dirs(self) -> str:
assert self.linuxbrew_dir is not None
assert self.linuxbrew_link_target is not None
return ', '.join("'%s'" % d for d in
sorted(set([self.linuxbrew_dir, self.linuxbrew_link_target])))
def is_enabled(self) -> bool:
return self.linuxbrew_dir is not None
def set_build_root(build_root: str) -> None:
global g_build_root
g_build_root = build_root
def get_linuxbrew_dir() -> Optional[str]:
if not sys.platform.startswith('linux'):
return None
global g_linuxbrew_dir
if not g_linuxbrew_dir:
g_linuxbrew_dir = LinuxbrewHome(g_build_root).linuxbrew_dir
return g_linuxbrew_dir