628
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.environment;
|
|
19 |
|
|
20 |
import java.io.ByteArrayOutputStream;
|
|
21 |
import java.io.File;
|
|
22 |
import java.io.FileFilter;
|
|
23 |
import java.io.FileInputStream;
|
|
24 |
import java.io.IOException;
|
|
25 |
import java.io.InputStream;
|
|
26 |
import java.io.InputStreamReader;
|
|
27 |
import java.security.MessageDigest;
|
|
28 |
import java.security.NoSuchAlgorithmException;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.HashMap;
|
|
31 |
import java.util.Iterator;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Map;
|
|
34 |
import java.util.regex.Matcher;
|
|
35 |
import java.util.regex.Pattern;
|
|
36 |
|
|
37 |
import org.apache.tools.ant.Project;
|
|
38 |
|
|
39 |
import com.nokia.helium.environment.ant.types.ExecutableInfo;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Represents the environment of the computer in terms of executables, etc. Typically scans for
|
|
43 |
* executables that have been run during the build.
|
|
44 |
*/
|
|
45 |
public class Environment {
|
|
46 |
private static final String[] WINDOWS_EXE_EXTENSIONS = { ".exe", ".bat", ".cmd" };
|
|
47 |
private static final String STDERR_OUTPUT = "stderr";
|
|
48 |
private static final String[] DEFAULT_EXECUTABLES = { "java", "ant" };
|
|
49 |
|
|
50 |
private Project project;
|
|
51 |
/** List of known executables. */
|
|
52 |
private List<Executable> executables = new ArrayList<Executable>();
|
|
53 |
/** List of meta information about executables. */
|
|
54 |
private Map<String, ExecutableInfo> defs = new HashMap<String, ExecutableInfo>();
|
|
55 |
|
|
56 |
public Environment(Project project) {
|
|
57 |
this.project = project;
|
|
58 |
}
|
|
59 |
|
|
60 |
public void setExecutableDefs(List<ExecutableInfo> executableDefs) {
|
|
61 |
for (Iterator<ExecutableInfo> iterator = executableDefs.iterator(); iterator.hasNext();) {
|
|
62 |
ExecutableInfo execDef = (ExecutableInfo) iterator.next();
|
|
63 |
defs.put(execDef.getName(), execDef);
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
public List<Executable> getExecutables() {
|
|
68 |
return executables;
|
|
69 |
}
|
|
70 |
|
|
71 |
public void scan(List<String> execCalls) throws IOException {
|
|
72 |
addDefaultExecutables();
|
|
73 |
parseExecLog(execCalls);
|
|
74 |
addExecsWithInfo();
|
|
75 |
populateExecutables(executables);
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Adds default executables to the list that must have been run because Ant is running.
|
|
80 |
*/
|
|
81 |
private void addDefaultExecutables() {
|
|
82 |
|
|
83 |
for (int i = 0; i < DEFAULT_EXECUTABLES.length; i++) {
|
|
84 |
Executable exe = new Executable(DEFAULT_EXECUTABLES[i]);
|
|
85 |
exe.setExecuted(true);
|
|
86 |
executables.add(exe);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Parses a log of calls to executables in CSV format.
|
|
92 |
*
|
|
93 |
* @param execLog
|
|
94 |
* @throws IOException
|
|
95 |
*/
|
|
96 |
private void parseExecLog(List<String> execCalls) throws IOException {
|
|
97 |
for (Iterator<String> iterator = execCalls.iterator(); iterator.hasNext();) {
|
|
98 |
String execCall = (String) iterator.next();
|
|
99 |
|
|
100 |
File execFile = new File(execCall);
|
|
101 |
String name = execFile.getName();
|
|
102 |
String path = null;
|
|
103 |
// See if the full path is available in the exec call
|
|
104 |
if (execFile.getParentFile() != null) {
|
|
105 |
path = execFile.getCanonicalPath();
|
|
106 |
}
|
|
107 |
Executable runExec = new Executable(name);
|
|
108 |
runExec.setPath(path);
|
|
109 |
runExec.setExecuted(true);
|
|
110 |
if (!executables.contains(runExec)) {
|
|
111 |
executables.add(runExec);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Adds executables to the list based on the meta-information given in the configuration.
|
|
118 |
*/
|
|
119 |
private void addExecsWithInfo() {
|
|
120 |
for (Iterator<String> iterator = defs.keySet().iterator(); iterator.hasNext();) {
|
|
121 |
String execName = (String) iterator.next();
|
|
122 |
Executable exec = new Executable(execName);
|
|
123 |
if (!executables.contains(exec)) {
|
|
124 |
executables.add(exec);
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
private void populateExecutables(List<Executable> executables) throws IOException {
|
|
130 |
for (Iterator<Executable> iterator = executables.iterator(); iterator.hasNext();) {
|
|
131 |
Executable exec = (Executable) iterator.next();
|
|
132 |
project.log("Checking executable: " + exec.toString(), Project.MSG_INFO);
|
|
133 |
File executableFile = findExecutableFile(exec);
|
|
134 |
|
|
135 |
if (executableFile != null) {
|
|
136 |
if (!findVersion(exec)) {
|
|
137 |
calculateAdditionalVersioningInfo(exec);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
else {
|
|
141 |
project.log("Cannot find path for executable: " + exec.toString(), Project.MSG_DEBUG);
|
|
142 |
}
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Finds an executable file based on the executable definition.
|
|
148 |
*
|
|
149 |
* @param exec An executable definition.
|
|
150 |
* @return The executable file.
|
|
151 |
* @throws IOException
|
|
152 |
*/
|
|
153 |
private File findExecutableFile(final Executable exec) throws IOException {
|
|
154 |
File executableFile = null;
|
|
155 |
|
|
156 |
// See if the executable has a full path
|
|
157 |
String path = exec.getPath();
|
|
158 |
if (path != null) {
|
|
159 |
File file = new File(path);
|
|
160 |
executableFile = file;
|
|
161 |
exec.setPath(executableFile.getCanonicalPath());
|
|
162 |
}
|
|
163 |
// Or search the PATH
|
|
164 |
else {
|
|
165 |
String[] pathDirs = getPaths();
|
|
166 |
// Filter object to match executable filenames
|
|
167 |
FileFilter filter = new FileFilter() {
|
|
168 |
public boolean accept(File file) {
|
|
169 |
if (isFileExecutable(file)) {
|
|
170 |
// Find the first file in the directory that has the same start of the name
|
|
171 |
String exeNameNoExt = exec.getNameNoExt();
|
|
172 |
|
|
173 |
String name = file.getName();
|
|
174 |
if (name.contains(".")) {
|
|
175 |
name = name.substring(0, name.indexOf("."));
|
|
176 |
}
|
|
177 |
|
|
178 |
if (name.equals(exeNameNoExt) || name.startsWith(exeNameNoExt + ".")) {
|
|
179 |
return true;
|
|
180 |
}
|
|
181 |
}
|
|
182 |
return false;
|
|
183 |
}
|
|
184 |
};
|
|
185 |
for (int i = 0; i < pathDirs.length; i++) {
|
|
186 |
File pathDir = new File(pathDirs[i]);
|
|
187 |
File[] executableFiles = pathDir.listFiles(filter);
|
|
188 |
if (executableFiles != null && executableFiles.length > 0) {
|
|
189 |
executableFile = executableFiles[0];
|
|
190 |
exec.setPath(executableFile.getCanonicalPath());
|
|
191 |
}
|
|
192 |
}
|
|
193 |
}
|
|
194 |
return executableFile;
|
|
195 |
}
|
|
196 |
|
|
197 |
private boolean isFileExecutable(File file) {
|
|
198 |
if (file.canExecute()) {
|
|
199 |
String os = System.getProperty("os.name").toLowerCase();
|
|
200 |
if (os.contains("windows") || os.contains("win32")) {
|
|
201 |
for (int i = 0; i < WINDOWS_EXE_EXTENSIONS.length; i++) {
|
|
202 |
if (file.getName().endsWith(WINDOWS_EXE_EXTENSIONS[i])) {
|
|
203 |
return true;
|
|
204 |
}
|
|
205 |
}
|
|
206 |
}
|
|
207 |
else {
|
|
208 |
return true;
|
|
209 |
}
|
|
210 |
}
|
|
211 |
return false;
|
|
212 |
}
|
|
213 |
|
|
214 |
private String[] getPaths() {
|
|
215 |
String pathEnvVar = System.getenv("Path");
|
|
216 |
String[] pathDirs = null;
|
|
217 |
if (pathEnvVar == null) {
|
|
218 |
pathEnvVar = System.getenv("PATH");
|
|
219 |
}
|
|
220 |
if (pathEnvVar != null) {
|
|
221 |
pathDirs = pathEnvVar.split(File.pathSeparator);
|
|
222 |
}
|
|
223 |
else {
|
|
224 |
pathDirs = new String[0];
|
|
225 |
}
|
|
226 |
return pathDirs;
|
|
227 |
}
|
|
228 |
|
|
229 |
private boolean findVersion(Executable exec) throws IOException {
|
|
230 |
// Get the executable additional data for this execution
|
|
231 |
ExecutableInfo def = defs.get(exec.getNameNoExt());
|
|
232 |
if (def != null && def.getVersionArgs() != null) {
|
|
233 |
String path = exec.getPath();
|
|
234 |
if (path == null) {
|
|
235 |
path = "";
|
|
236 |
}
|
|
237 |
String[] versionArgs = def.getVersionArgs().split(" ");
|
|
238 |
String[] commands = new String[versionArgs.length + 1];
|
|
239 |
commands[0] = path;
|
|
240 |
for (int i = 0; i < versionArgs.length; i++) {
|
|
241 |
commands[i + 1] = versionArgs[i].trim();
|
|
242 |
}
|
|
243 |
Process commandProcess = Runtime.getRuntime().exec(commands);
|
|
244 |
|
|
245 |
String output = def.getOutput();
|
|
246 |
StringBuilder text = new StringBuilder();
|
|
247 |
int dataRead = 0;
|
|
248 |
char[] chars = new char[1000];
|
|
249 |
if (output == null || !output.equals(STDERR_OUTPUT)) {
|
|
250 |
InputStream in = commandProcess.getInputStream();
|
|
251 |
InputStreamReader textIn = new InputStreamReader(in);
|
|
252 |
while (dataRead != -1) {
|
|
253 |
dataRead = textIn.read(chars, 0, chars.length);
|
|
254 |
if (dataRead != -1) {
|
|
255 |
text.append(chars, 0, dataRead);
|
|
256 |
}
|
|
257 |
}
|
|
258 |
}
|
|
259 |
InputStream err = commandProcess.getErrorStream();
|
|
260 |
InputStreamReader textErr = new InputStreamReader(err);
|
|
261 |
dataRead = 0;
|
|
262 |
while (dataRead != -1) {
|
|
263 |
dataRead = textErr.read(chars, 0, chars.length);
|
|
264 |
if (dataRead != -1) {
|
|
265 |
text.append(chars, 0, dataRead);
|
|
266 |
}
|
|
267 |
}
|
|
268 |
String versionText = text.toString();
|
|
269 |
if (def.getVersionRegex() != null) {
|
|
270 |
Pattern versionPattern = Pattern.compile(def.getVersionRegex());
|
|
271 |
Matcher versionMatch = versionPattern.matcher(versionText);
|
|
272 |
if (versionMatch.find()) {
|
|
273 |
String version = versionMatch.group(1);
|
|
274 |
exec.setVersion(version);
|
|
275 |
return true;
|
|
276 |
}
|
|
277 |
}
|
|
278 |
}
|
|
279 |
return false;
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* Calculate extra versioning info for the executable file. Used if cannot find a version value.
|
|
284 |
*
|
|
285 |
* @param exec The executable.
|
|
286 |
* @throws IOException If file cannot be read.
|
|
287 |
*/
|
|
288 |
private void calculateAdditionalVersioningInfo(Executable exec) throws IOException {
|
|
289 |
calculateHash(exec);
|
|
290 |
File file = new File(exec.getPath());
|
|
291 |
exec.setLastModified(file.lastModified());
|
|
292 |
exec.setLength(file.length());
|
|
293 |
}
|
|
294 |
|
|
295 |
/**
|
|
296 |
* Calculate a hash value for the executable file.
|
|
297 |
*
|
|
298 |
* @param exec The executable.
|
|
299 |
* @throws IOException If file cannot be read.
|
|
300 |
*/
|
|
301 |
private void calculateHash(Executable exec) throws IOException {
|
|
302 |
FileInputStream in = new FileInputStream(exec.getPath());
|
|
303 |
byte[] bytes = new byte[1000];
|
|
304 |
ByteArrayOutputStream fileBytes = new ByteArrayOutputStream();
|
|
305 |
int bytesRead = 0;
|
|
306 |
bytesRead = in.read(bytes);
|
|
307 |
while (bytesRead != -1) {
|
|
308 |
fileBytes.write(bytes, 0, bytesRead);
|
|
309 |
bytesRead = in.read(bytes);
|
|
310 |
}
|
|
311 |
MessageDigest digest;
|
|
312 |
String hash = "";
|
|
313 |
try {
|
|
314 |
digest = MessageDigest.getInstance("MD5");
|
|
315 |
digest.update(fileBytes.toByteArray());
|
|
316 |
byte[] hashBytes = digest.digest();
|
|
317 |
StringBuilder builder = new StringBuilder();
|
|
318 |
for (int i = 0; i < hashBytes.length; i++) {
|
|
319 |
builder.append(Byte.toString(hashBytes[i]));
|
|
320 |
}
|
|
321 |
hash = new String(builder.toString());
|
|
322 |
exec.setHash(hash);
|
|
323 |
|
|
324 |
}
|
|
325 |
catch (NoSuchAlgorithmException e) {
|
|
326 |
// Not expected to occur
|
|
327 |
e.printStackTrace();
|
|
328 |
}
|
|
329 |
}
|
|
330 |
}
|