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
|
/*
* Copyright (c) 2014-2016 Eike Stepper (Loehne, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Stepper
*/
public final class ArtifactRepositoryAdjuster
{
private static final String DOWNLOAD_PREFIX = "/home/data/httpd/download.eclipse.org/";
private static final Pattern REPOSITORY_PATTERN = Pattern.compile("\\s*<repository.*?>\\s*");
private static final Pattern FEATURE_PATTERN = Pattern.compile(
"\\s*<artifact\\s+classifier\\s*=\\s*['\"]org.eclipse.update.feature['\"]\\s+id\\s*=\\s*['\"](org\\.eclipse\\.oomph.*?)['\"]\\s+version\\s*=\\s*['\"]([^'\"]*)['\"].*?>\\s*");
private ArtifactRepositoryAdjuster()
{
}
public static void main(String[] args) throws Exception
{
File repositoryFolder = new File(args[0]).getCanonicalFile();
File repositoryFolderFinal = new File(args[1]).getCanonicalFile();
String repositoryName = args[2];
String buildType = args[3];
File input = new File(repositoryFolder, "artifacts.xml");
File output = new File(repositoryFolder, "artifacts.out");
Version greatestVersion = getGreatestFeatureVersion(input);
System.out.println("Adjusting " + input);
BufferedReader reader = new BufferedReader(new FileReader(input));
BufferedWriter writer = new BufferedWriter(new FileWriter(output));
String relativePath = repositoryFolderFinal.getAbsolutePath();
if (relativePath.startsWith(DOWNLOAD_PREFIX))
{
relativePath = relativePath.substring(DOWNLOAD_PREFIX.length());
}
if (relativePath.startsWith("/"))
{
relativePath = relativePath.substring(1);
}
boolean repositoryFound = false;
String line;
while ((line = reader.readLine()) != null)
{
if (!repositoryFound)
{
Matcher matcher = REPOSITORY_PATTERN.matcher(line);
if (matcher.matches())
{
String newLine = line.replaceFirst("name=['\"].*?['\"]", "name='" + repositoryName + "'");
if (!newLine.equals(line))
{
System.out.println(" repository.name = " + repositoryName);
line = newLine;
}
writeLine(writer, line);
Properties properties = new Properties(reader);
boolean mirrored = !"nightly".equals(buildType);
if (mirrored)
{
String mirrorsURL = "http://www.eclipse.org/downloads/download.php?file=/" + relativePath;
System.out.println(" p2.mirrorsURL = " + mirrorsURL);
properties.put("p2.mirrorsURL", mirrorsURL);
}
else
{
properties.remove("p2.mirrorsURL");
}
String statsURI = "https://download.eclipse.org/stats/oomph/feature/" + greatestVersion;
System.out.println(" p2.statsURI = " + statsURI);
properties.put("p2.statsURI", statsURI);
properties.write(writer);
repositoryFound = true;
continue;
}
}
else
{
Matcher matcher = FEATURE_PATTERN.matcher(line);
if (matcher.matches())
{
writeLine(writer, line);
String id = matcher.group(1);
System.out.println(" download.stats = " + id);
Properties properties = new Properties(reader);
properties.put("download.stats", id);
properties.write(writer);
continue;
}
}
writeLine(writer, line);
}
writer.close();
reader.close();
}
private static Version getGreatestFeatureVersion(File input) throws IOException
{
Version greatestVersion = null;
List<String> lines = readLines(input, "UTF-8");
for (String line : lines)
{
Matcher matcher = FEATURE_PATTERN.matcher(line);
if (matcher.matches())
{
Version version = new Version(matcher.group(2));
if (greatestVersion == null || version.isGreaterThan(greatestVersion))
{
greatestVersion = version;
}
}
}
return greatestVersion;
}
private static List<String> readLines(File file, String charsetName) throws IOException
{
List<String> lines = new ArrayList<>();
if (file.exists())
{
InputStream in = null;
Reader reader = null;
BufferedReader bufferedReader = null;
try
{
in = new FileInputStream(file);
reader = charsetName == null ? new InputStreamReader(in) : new InputStreamReader(in, charsetName);
bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
}
}
finally
{
closeSilent(bufferedReader);
closeSilent(reader);
closeSilent(in);
}
}
return lines;
}
private static void closeSilent(Closeable closeable)
{
try
{
if (closeable != null)
{
closeable.close();
}
}
catch (Exception ex)
{
// Ignore.
}
}
private static void writeLine(BufferedWriter writer, String line) throws IOException
{
writer.write(line);
writer.write('\n');
}
/**
* @author Eike Stepper
*/
private static final class Properties extends LinkedHashMap<String, String>
{
private static final long serialVersionUID = 1L;
private static final Pattern PROPERTIES_PATTERN = Pattern.compile("(\\s*)<properties\\s+size\\s*=\\s*['\"]([0-9]+)['\"]\\s*>\\s*");
private static final Pattern PROPERTY_PATTERN = Pattern
.compile("(\\s*)<property\\s+name\\s*=\\s*['\"](.*?)['\"]\\s+value\\s*=\\s*['\"](.*?)['\"]\\s*/>\\s*");
private String propertiesIndent;
private String propertyIndent;
public Properties(BufferedReader reader) throws IOException
{
String propertiesLine = reader.readLine();
Matcher propertiesMatcher = PROPERTIES_PATTERN.matcher(propertiesLine);
if (!propertiesMatcher.matches())
{
throw new IllegalStateException("No properties match: " + propertiesLine);
}
propertiesIndent = propertiesMatcher.group(1);
int size = Integer.parseInt(propertiesMatcher.group(2));
for (int i = 0; i < size; i++)
{
String propertyLine = reader.readLine();
Matcher propertyMatcher = PROPERTY_PATTERN.matcher(propertyLine);
if (!propertyMatcher.matches())
{
throw new IllegalStateException("No property match: " + propertyLine);
}
propertyIndent = propertyMatcher.group(1);
put(propertyMatcher.group(2), propertyMatcher.group(3));
}
}
public void write(BufferedWriter writer) throws IOException
{
writeLine(writer, propertiesIndent + "<properties size='" + size() + "'>");
for (Map.Entry<String, String> entry : entrySet())
{
writeLine(writer, propertyIndent + "<property name='" + entry.getKey() + "' value='" + entry.getValue() + "'/>");
}
}
}
}
|