forked from facebook/buck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_lookup.py
More file actions
163 lines (144 loc) · 6.04 KB
/
java_lookup.py
File metadata and controls
163 lines (144 loc) · 6.04 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (c) Facebook, Inc. and its affiliates.
#
# 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.
import logging
import os
import re
import sys
import textwrap
from programs.buck_tool import BuckToolException
from programs.subprocutils import which
JDK_8_AND_UNDER_PATH_VERSION_REGEX_STRINGS = [
r"jdk(?P<full>1\.(?P<major>\d+)(\.\d+(_\d+)?)?)(\.jdk)?",
r"adoptopenjdk-(?P<full>(?P<major>[6-8])(\.\d+(_\d+)?)?)(\.jdk|\.jre)?",
]
JDK_9_AND_OVER_PATH_VERSION_REGEX_STRINGS = [
r"jdk-(?P<full>(?P<major>\d+)(\.\d+(\.\d+(_\d+)?)?)?)(\.jdk)?",
r"adoptopenjdk-(?P<full>(?P<major>9|\d{2,})(\.\d+(\.\d+(_\d+)?)?)?)(\.jdk|\.jre)?",
]
JDK_8_AND_UNDER_PATH_VERSION_REGEXES = [
re.compile("^" + r + "$") for r in JDK_8_AND_UNDER_PATH_VERSION_REGEX_STRINGS
]
JDK_9_AND_OVER_PATH_VERSION_REGEXES = [
re.compile("^" + r + "$") for r in JDK_9_AND_OVER_PATH_VERSION_REGEX_STRINGS
]
# Note: Group 1 of these regexes contains the entire version string, group 2 contains major version.
JDK_PATH_REGEXES = (
[
re.compile(r"^/Library/Java/JavaVirtualMachines/" + r + "/Contents/Home/*$")
for r in JDK_8_AND_UNDER_PATH_VERSION_REGEX_STRINGS
]
+ [
re.compile(r"^/Library/Java/JavaVirtualMachines/" + r + "/Contents/Home/*$")
for r in JDK_9_AND_OVER_PATH_VERSION_REGEX_STRINGS
]
+ [
re.compile("^C:\\\\Program Files\\\\Java\\\\" + r + "(\\\\)*$")
for r in JDK_8_AND_UNDER_PATH_VERSION_REGEX_STRINGS
]
+ [
re.compile("^C:\\\\Program Files\\\\Java\\\\" + r + "(\\\\)*$")
for r in JDK_9_AND_OVER_PATH_VERSION_REGEX_STRINGS
]
)
def _get_suspected_java_version_from_java_path(java_path):
for regex in JDK_PATH_REGEXES:
match = regex.match(java_path)
if match:
return int(match.group("major"))
return None
def _get_java_path_for_highest_minor_version(base_path, desired_major_version):
if not os.path.isdir(base_path):
return None
max_version = None
max_dir = None
regexes = (
JDK_8_AND_UNDER_PATH_VERSION_REGEXES
if desired_major_version <= 8
else JDK_9_AND_OVER_PATH_VERSION_REGEXES
)
for dir in sorted(os.listdir(base_path)):
for regex in regexes:
match = regex.match(dir)
if match:
major_version = int(match.group("major"))
if major_version == desired_major_version:
version_string = match.group("full")
version = tuple(
map(int, version_string.replace("_", ".").split("."))
)
if not max_version or version > max_version:
max_version = version
max_dir = dir
return os.path.join(base_path, max_dir) if max_dir else None
def _get_known_java_path_for_version(java_major_version):
java_path = None
if sys.platform == "darwin":
java_path = _get_java_path_for_highest_minor_version(
"/Library/Java/JavaVirtualMachines", java_major_version
)
if java_path:
java_path += "/Contents/Home"
elif sys.platform == "win32":
java_path = _get_java_path_for_highest_minor_version(
r"C:\Program Files\Java", java_major_version
)
return java_path if java_path and os.path.isdir(java_path) else None
def _get_java_exec(java_base_path):
java_exec = "java.exe" if os.name == "nt" else "java"
return os.path.join(java_base_path, "bin", java_exec)
def get_java_path(required_java_version):
java_home_path = os.getenv("JAVA_HOME")
if java_home_path:
# Though we try to respect JAVA_HOME, if the path looks like the wrong version of Java, try
# to use a known location of the JDK for the right version instead.
suspected_java_version = _get_suspected_java_version_from_java_path(
java_home_path
)
if suspected_java_version and suspected_java_version != required_java_version:
message = (
'Warning: JAVA_HOME is set to "{}", which looks like a Java {} path, '
+ "but Buck requires Java {}."
).format(java_home_path, suspected_java_version, required_java_version)
if os.getenv("BUCK_RESPECT_JAVA_HOME") != "1":
message += " Ignoring JAVA_HOME. Set BUCK_RESPECT_JAVA_HOME to 1 to disable this behavior."
java_home_path = None
logging.warning(message)
if java_home_path is None:
# Default to a known location of the JDK for the right version of Java, regardless of what
# version of Java is on the PATH.
java_base_path = _get_known_java_path_for_version(required_java_version)
java_path = None
if java_base_path:
java_path = _get_java_exec(java_base_path)
if not os.path.isfile(java_path):
java_path = None
if not java_path:
java_path = which("java")
if java_path is None:
raise BuckToolException(
"Could not find Java executable. \
Make sure it is on PATH or JAVA_HOME is set."
)
else:
java_path = _get_java_exec(java_home_path)
if not os.path.isfile(java_path):
message = textwrap.dedent(
"""
Could not find Java executable under JAVA_HOME at: '{}'.
Please make sure your JAVA_HOME environment variable is set correctly.
Then restart buck (buck kill) and try again.
"""
).format(java_path)
raise BuckToolException(message)
return java_path