forked from browserstack/browserstack-local-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalBinary.java
More file actions
222 lines (182 loc) · 6.75 KB
/
LocalBinary.java
File metadata and controls
222 lines (182 loc) · 6.75 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.browserstack.local;
import org.apache.commons.io.FileUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.File;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;
class LocalBinary {
private static final String BIN_URL = "https://www.browserstack.com/local-testing/downloads/binaries/";
private String httpPath;
private String binaryPath;
private boolean isOSWindows;
private final String orderedPaths[] = {
System.getProperty("user.home") + "/.browserstack",
System.getProperty("user.dir"),
System.getProperty("java.io.tmpdir")
};
LocalBinary(String path) throws LocalException {
initialize();
if (path != "") {
getBinaryOnPath(path);
} else {
getBinary();
}
checkBinary();
}
private void initialize() throws LocalException {
String osname = System.getProperty("os.name").toLowerCase();
isOSWindows = osname.contains("windows");
String binFileName;
if (isOSWindows) {
binFileName = "BrowserStackLocal.exe";
} else if (osname.contains("mac") || osname.contains("darwin")) {
binFileName = "BrowserStackLocal-darwin-x64";
} else if (osname.contains("linux")) {
String arch = System.getProperty("os.arch");
if (arch.contains("64")) {
if (isAlpine()) {
binFileName = "BrowserStackLocal-alpine";
} else {
binFileName = "BrowserStackLocal-linux-x64";
}
} else {
binFileName = "BrowserStackLocal-linux-ia32";
}
} else {
throw new LocalException("Failed to detect OS type");
}
String sourceURL = BIN_URL;
httpPath = sourceURL + binFileName;
}
private boolean isAlpine() {
String[] cmd = { "/bin/sh", "-c", "grep -w \"NAME\" /etc/os-release" };
boolean flag = false;
try {
Process os = Runtime.getRuntime().exec(cmd);
BufferedReader stdout = new BufferedReader(new InputStreamReader(os.getInputStream()));
flag = stdout.readLine().contains("Alpine");
} finally {
return flag;
}
}
private void checkBinary() throws LocalException{
boolean binaryWorking = validateBinary();
if(!binaryWorking){
File binary_file = new File(binaryPath);
if (binary_file.exists()) {
binary_file.delete();
}
getBinary();
if(!validateBinary()){
throw new LocalException("BrowserStackLocal binary is corrupt");
}
}
}
private boolean validateBinary() throws LocalException{
Process process;
try {
process = new ProcessBuilder(binaryPath,"--version").start();
BufferedReader stdoutbr = new BufferedReader(new InputStreamReader(process.getInputStream()));
String stdout="",line="";
while ((line = stdoutbr.readLine()) != null) {
stdout += line;
}
process.waitFor();
boolean validBinary = Pattern.matches("BrowserStack Local version \\d+\\.\\d+", stdout);
return validBinary;
}catch(IOException ex){
throw new LocalException(ex.toString());
}
catch(InterruptedException ex){
throw new LocalException(ex.toString());
}
}
private void getBinaryOnPath(String path) throws LocalException {
binaryPath = path;
if (!new File(binaryPath).exists()) {
downloadBinary(binaryPath, true);
}
}
private void getBinary() throws LocalException {
String destParentDir = getAvailableDirectory();
binaryPath = destParentDir + "/BrowserStackLocal";
if (isOSWindows) {
binaryPath += ".exe";
}
if (!new File(binaryPath).exists()) {
downloadBinary(destParentDir, false);
}
}
private String getAvailableDirectory() throws LocalException {
int i = 0;
while (i < orderedPaths.length) {
String path = orderedPaths[i];
if (makePath(path))
return path;
else
i++;
}
throw new LocalException("Error trying to download BrowserStackLocal binary");
}
private boolean makePath(String path) {
try {
if (!new File(path).exists())
new File(path).mkdirs();
return true;
} catch (Exception e) {
return false;
}
}
private void downloadBinary(String destParentDir, Boolean custom) throws LocalException {
try {
String source = destParentDir;
if (!custom) {
if (!new File(destParentDir).exists())
new File(destParentDir).mkdirs();
source = destParentDir + "/BrowserStackLocal";
if (isOSWindows) {
source += ".exe";
}
}
URL url = new URL(httpPath);
File f = new File(source);
newCopyToFile(url, f);
changePermissions(binaryPath);
} catch (Exception e) {
throw new LocalException("Error trying to download BrowserStackLocal binary: " + e.getMessage());
}
}
private void changePermissions(String path) {
File f = new File(path);
f.setExecutable(true, true);
f.setReadable(true, true);
f.setWritable(true, true);
}
public String getBinaryPath() {
return binaryPath;
}
private static void newCopyToFile(URL url, File f) throws IOException {
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "browserstack-local-java/" + Local.getPackageVersion());
conn.setRequestProperty("Accept-Encoding", "gzip, *");
String contentEncoding = conn.getContentEncoding();
if (contentEncoding == null || !contentEncoding.toLowerCase().contains("gzip")) {
FileUtils.copyToFile(conn.getInputStream(), f);
return;
}
try (InputStream stream = new GZIPInputStream(conn.getInputStream())) {
if (System.getenv().containsKey("BROWSERSTACK_LOCAL_DEBUG_GZIP")) {
System.out.println("using gzip in " + conn.getRequestProperty("User-Agent"));
}
FileUtils.copyToFile(stream, f);
} catch (ZipException e) {
FileUtils.copyURLToFile(url, f);
}
}
}