forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContributionInstaller.java
312 lines (263 loc) · 12.5 KB
/
ContributionInstaller.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* This file is part of Arduino.
*
* Copyright 2014 Arduino LLC (http://www.arduino.cc/)
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
package cc.arduino.contributions.packages;
import cc.arduino.Constants;
import cc.arduino.contributions.DownloadableContribution;
import cc.arduino.contributions.DownloadableContributionsDownloader;
import cc.arduino.contributions.ProgressListener;
import cc.arduino.contributions.SignatureVerifier;
import cc.arduino.filters.FileExecutablePredicate;
import cc.arduino.utils.ArchiveExtractor;
import cc.arduino.utils.MultiStepProgress;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import processing.app.BaseNoGui;
import processing.app.I18n;
import processing.app.Platform;
import processing.app.PreferencesData;
import processing.app.helpers.FileUtils;
import processing.app.helpers.filefilters.OnlyDirs;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import static processing.app.I18n.format;
import static processing.app.I18n.tr;
public class ContributionInstaller {
private static Logger log = LogManager.getLogger(ContributionInstaller.class);
private final Platform platform;
private final SignatureVerifier signatureVerifier;
public ContributionInstaller(Platform platform, SignatureVerifier signatureVerifier) {
this.platform = platform;
this.signatureVerifier = signatureVerifier;
}
public synchronized List<String> install(ContributedPlatform contributedPlatform, ProgressListener progressListener) throws Exception {
List<String> errors = new LinkedList<>();
if (contributedPlatform.isInstalled()) {
throw new Exception("Platform is already installed!");
}
// Do not download already installed tools
List<ContributedTool> tools = new ArrayList<>();
for (ContributedTool tool : contributedPlatform.getResolvedTools()) {
DownloadableContribution downloadable = tool.getDownloadableContribution(platform);
if (downloadable == null) {
throw new Exception(format(tr("Tool {0} is not available for your operating system."), tool.getName()));
}
// Download the tool if it's not installed or it's a built-in tool
if (!tool.isInstalled() || tool.isBuiltIn()) {
tools.add(tool);
}
}
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.indexer.getStagingFolder());
// Calculate progress increases
MultiStepProgress progress = new MultiStepProgress((tools.size() + 1) * 2);
// Download all
try {
// Download platform
downloader.download(contributedPlatform, progress, tr("Downloading boards definitions."), progressListener, false);
progress.stepDone();
// Download tools
int i = 1;
for (ContributedTool tool : tools) {
String msg = format(tr("Downloading tools ({0}/{1})."), i, tools.size());
i++;
downloader.download(tool.getDownloadableContribution(platform), progress, msg, progressListener, false);
progress.stepDone();
}
} catch (InterruptedException e) {
// Download interrupted... just exit
return errors;
}
ContributedPackage pack = contributedPlatform.getParentPackage();
File packageFolder = new File(BaseNoGui.indexer.getPackagesFolder(), pack.getName());
// TODO: Extract to temporary folders and move to the final destination only
// once everything is successfully unpacked. If the operation fails remove
// all the temporary folders and abort installation.
List<Map.Entry<ContributedToolReference, ContributedTool>> resolvedToolReferences = contributedPlatform
.getResolvedToolReferences().entrySet().stream()
.filter((entry) -> !entry.getValue().isInstalled()
|| entry.getValue().isBuiltIn())
.collect(Collectors.toList());
int i = 1;
for (Map.Entry<ContributedToolReference, ContributedTool> entry : resolvedToolReferences) {
progress.setStatus(format(tr("Installing tools ({0}/{1})..."), i, resolvedToolReferences.size()));
progressListener.onProgress(progress);
i++;
ContributedTool tool = entry.getValue();
Path destFolder = Paths.get(BaseNoGui.indexer.getPackagesFolder().getAbsolutePath(), entry.getKey().getPackager(), "tools", tool.getName(), tool.getVersion());
Files.createDirectories(destFolder);
DownloadableContribution toolContrib = tool.getDownloadableContribution(platform);
assert toolContrib.getDownloadedFile() != null;
new ArchiveExtractor(platform).extract(toolContrib.getDownloadedFile(), destFolder.toFile(), 1);
try {
findAndExecutePostInstallScriptIfAny(destFolder.toFile(), contributedPlatform.getParentPackage().isTrusted(), PreferencesData.areInsecurePackagesAllowed());
} catch (IOException e) {
errors.add(tr("Error running post install script"));
}
tool.setInstalled(true);
tool.setInstalledFolder(destFolder.toFile());
progress.stepDone();
}
// Unpack platform on the correct location
progress.setStatus(tr("Installing boards..."));
progressListener.onProgress(progress);
File platformFolder = new File(packageFolder, "hardware" + File.separator + contributedPlatform.getArchitecture());
File destFolder = new File(platformFolder, contributedPlatform.getParsedVersion());
Files.createDirectories(destFolder.toPath());
new ArchiveExtractor(platform).extract(contributedPlatform.getDownloadedFile(), destFolder, 1);
contributedPlatform.setInstalled(true);
contributedPlatform.setInstalledFolder(destFolder);
try {
findAndExecutePostInstallScriptIfAny(destFolder, contributedPlatform.getParentPackage().isTrusted(), PreferencesData.areInsecurePackagesAllowed());
} catch (IOException e) {
e.printStackTrace();
errors.add(tr("Error running post install script"));
}
progress.stepDone();
progress.setStatus(tr("Installation completed!"));
progressListener.onProgress(progress);
return errors;
}
private void findAndExecutePostInstallScriptIfAny(File folder, boolean trusted, boolean trustAll) throws IOException {
Collection<File> scripts = platform.postInstallScripts(folder).stream().filter(new FileExecutablePredicate()).collect(Collectors.toList());
if (scripts.isEmpty()) {
String[] subfolders = folder.list(new OnlyDirs());
if (subfolders.length != 1) {
return;
}
findAndExecutePostInstallScriptIfAny(new File(folder, subfolders[0]), trusted, trustAll);
return;
}
executeScripts(folder, scripts, trusted, trustAll);
}
private void findAndExecutePreUninstallScriptIfAny(File folder, boolean trusted, boolean trustAll) throws IOException {
Collection<File> scripts = platform.preUninstallScripts(folder).stream().filter(new FileExecutablePredicate()).collect(Collectors.toList());
if (scripts.isEmpty()) {
String[] subfolders = folder.list(new OnlyDirs());
if (subfolders.length != 1) {
return;
}
findAndExecutePreUninstallScriptIfAny(new File(folder, subfolders[0]), trusted, trustAll);
return;
}
executeScripts(folder, scripts, trusted, trustAll);
}
private void executeScripts(File folder, Collection<File> postInstallScripts, boolean trusted, boolean trustAll) throws IOException {
File script = postInstallScripts.iterator().next();
if (!trusted && !trustAll) {
System.err.println(I18n.format(tr("Warning: non trusted contribution, skipping script execution ({0})"), script));
return;
}
if (trustAll) {
System.err.println(I18n.format(tr("Warning: forced untrusted script execution ({0})"), script));
}
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
Executor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(stdout, stderr));
executor.setWorkingDirectory(folder);
executor.setExitValues(null);
int exitValue = executor.execute(new CommandLine(script));
executor.setExitValues(new int[0]);
System.out.write(stdout.toByteArray());
System.err.write(stderr.toByteArray());
if (executor.isFailure(exitValue)) {
throw new IOException();
}
}
public synchronized List<String> remove(ContributedPlatform contributedPlatform) {
if (contributedPlatform == null || contributedPlatform.isBuiltIn()) {
return new LinkedList<>();
}
List<String> errors = new LinkedList<>();
try {
findAndExecutePreUninstallScriptIfAny(contributedPlatform.getInstalledFolder(), contributedPlatform.getParentPackage().isTrusted(), PreferencesData.areInsecurePackagesAllowed());
} catch (IOException e) {
errors.add(tr("Error running post install script"));
}
// Check if the tools are no longer needed
for (ContributedTool tool : contributedPlatform.getResolvedTools()) {
// Do not remove used tools
if (BaseNoGui.indexer.isContributedToolUsed(contributedPlatform, tool))
continue;
// Do not remove built-in tools
if (tool.isBuiltIn())
continue;
// Ok, delete the tool
File destFolder = tool.getInstalledFolder();
FileUtils.recursiveDelete(destFolder);
tool.setInstalled(false);
tool.setInstalledFolder(null);
// We removed the version folder (.../tools/TOOL_NAME/VERSION)
// now try to remove the containing TOOL_NAME folder
// (and silently fail if another version of the tool is installed)
try {
Files.delete(destFolder.getParentFile().toPath());
} catch (Exception e) {
// ignore
log.info("The directory is not empty there is another version installed. directory {}",
destFolder.getParentFile().toPath(), e);
}
}
FileUtils.recursiveDelete(contributedPlatform.getInstalledFolder());
contributedPlatform.setInstalled(false);
contributedPlatform.setInstalledFolder(null);
return errors;
}
public synchronized void updateIndex(ProgressListener progressListener) {
MultiStepProgress progress = new MultiStepProgress(1);
final DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.indexer.getStagingFolder());
final Set<String> packageIndexURLs = new HashSet<>(
PreferencesData.getCollection(Constants.PREF_BOARDS_MANAGER_ADDITIONAL_URLS)
);
packageIndexURLs.add(Constants.PACKAGE_INDEX_URL);
for (String packageIndexURLString : packageIndexURLs) {
try {
// Extract the file name from the URL
final URL packageIndexURL = new URL(packageIndexURLString);
log.info("Start download and signature check of={}", packageIndexURLs);
downloader.downloadIndexAndSignature(progress, packageIndexURL, progressListener, signatureVerifier);
} catch (Exception e) {
log.error(e.getMessage(), e);
System.err.println(e.getMessage());
}
}
progress.stepDone();
log.info("Downloaded package index URL={}", packageIndexURLs);
}
}