| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.build.doc; |
| 7 |
|
| 8 |
import java.io.ByteArrayInputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.StringReader; |
| 11 |
import java.nio.charset.StandardCharsets; |
| 12 |
import java.nio.file.FileVisitResult; |
| 13 |
import java.nio.file.Files; |
| 14 |
import java.nio.file.Path; |
| 15 |
import java.nio.file.Paths; |
| 16 |
import java.nio.file.SimpleFileVisitor; |
| 17 |
import java.nio.file.attribute.BasicFileAttributes; |
| 18 |
import java.sql.Connection; |
| 19 |
import java.sql.DriverManager; |
| 20 |
import java.sql.ResultSet; |
| 21 |
import java.sql.Statement; |
| 22 |
import java.util.zip.CRC32; |
| 23 |
import java.util.zip.Deflater; |
| 24 |
import java.util.zip.ZipEntry; |
| 25 |
import java.util.zip.ZipOutputStream; |
| 26 |
|
| 27 |
import org.h2.dev.ftp.FtpClient; |
| 28 |
import org.h2.engine.Constants; |
| 29 |
import org.h2.store.fs.FileUtils; |
| 30 |
import org.h2.test.utils.OutputCatcher; |
| 31 |
import org.h2.util.ScriptReader; |
| 32 |
import org.h2.util.StringUtils; |
| 33 |
|
| 34 |
/** |
| 35 |
* Upload the code coverage result to the H2 web site. |
| 36 |
*/ |
| 37 |
public class UploadBuild { |
| 38 |
|
| 39 |
/** |
| 40 |
* This method is called when executing this application from the command |
| 41 |
* line. |
| 42 |
* |
| 43 |
* @param args the command line parameters |
| 44 |
*/ |
| 45 |
public static void main(String... args) throws Exception { |
| 46 |
System.exit(0); |
| 47 |
System.setProperty("h2.socketConnectTimeout", "30000"); |
| 48 |
String password = System.getProperty("h2.ftpPassword"); |
| 49 |
if (password == null) { |
| 50 |
return; |
| 51 |
} |
| 52 |
FtpClient ftp = FtpClient.open("h2database.com"); |
| 53 |
ftp.login("h2database", password); |
| 54 |
ftp.changeWorkingDirectory("/httpdocs"); |
| 55 |
Path coverageFile = Paths.get("coverage/index.html"); |
| 56 |
boolean coverage = Files.exists(coverageFile); |
| 57 |
boolean coverageFailed; |
| 58 |
if (coverage) { |
| 59 |
String index = new String(Files.readAllBytes(coverageFile), StandardCharsets.ISO_8859_1); |
| 60 |
coverageFailed = index.contains("CLASS=\"h\""); |
| 61 |
while (true) { |
| 62 |
int idx = index.indexOf("<A HREF=\""); |
| 63 |
if (idx < 0) { |
| 64 |
break; |
| 65 |
} |
| 66 |
int end = index.indexOf('>', idx) + 1; |
| 67 |
index = index.substring(0, idx) + index.substring(end); |
| 68 |
idx = index.indexOf("</A>"); |
| 69 |
index = index.substring(0, idx) + index.substring(idx + "</A>".length()); |
| 70 |
} |
| 71 |
index = StringUtils.replaceAll(index, "[all", ""); |
| 72 |
index = StringUtils.replaceAll(index, "classes]", ""); |
| 73 |
Files.write(Paths.get("coverage/overview.html"), index.getBytes(StandardCharsets.ISO_8859_1)); |
| 74 |
Files.createDirectories(Paths.get("details")); |
| 75 |
zip("details/coverage_files.zip", "coverage", true); |
| 76 |
zip("coverage.zip", "details", false); |
| 77 |
FileUtils.delete("coverage.txt"); |
| 78 |
FileUtils.delete("details/coverage_files.zip"); |
| 79 |
FileUtils.delete("details"); |
| 80 |
if (ftp.exists("/httpdocs", "coverage")) { |
| 81 |
ftp.removeDirectoryRecursive("/httpdocs/coverage"); |
| 82 |
} |
| 83 |
ftp.makeDirectory("/httpdocs/coverage"); |
| 84 |
} else { |
| 85 |
coverageFailed = true; |
| 86 |
} |
| 87 |
String testOutput; |
| 88 |
boolean error; |
| 89 |
Path testOutputFile = Paths.get("docs/html/testOutput.html"); |
| 90 |
if (Files.exists(testOutputFile)) { |
| 91 |
testOutput = new String(Files.readAllBytes(testOutputFile), StandardCharsets.UTF_8); |
| 92 |
error = testOutput.contains(OutputCatcher.START_ERROR); |
| 93 |
} else { |
| 94 |
Path logFile = Paths.get("log.txt"); |
| 95 |
if (Files.exists(logFile)) { |
| 96 |
testOutput = new String(Files.readAllBytes(logFile)); |
| 97 |
testOutput = testOutput.replaceAll("\n", "<br />"); |
| 98 |
error = true; |
| 99 |
} else { |
| 100 |
testOutput = "No log.txt"; |
| 101 |
error = true; |
| 102 |
} |
| 103 |
} |
| 104 |
if (!ftp.exists("/httpdocs", "automated")) { |
| 105 |
ftp.makeDirectory("/httpdocs/automated"); |
| 106 |
} |
| 107 |
String buildSql; |
| 108 |
if (ftp.exists("/httpdocs/automated", "history.sql")) { |
| 109 |
buildSql = new String(ftp.retrieve("/httpdocs/automated/history.sql")); |
| 110 |
} else { |
| 111 |
buildSql = "create table item(id identity, title varchar, " + |
| 112 |
"issued timestamp, desc varchar);\n"; |
| 113 |
} |
| 114 |
String ts = new java.sql.Timestamp(System.currentTimeMillis()).toString(); |
| 115 |
String now = ts.substring(0, 16); |
| 116 |
int idx = testOutput.indexOf("Statements per second: "); |
| 117 |
if (idx >= 0) { |
| 118 |
int end = testOutput.indexOf("<br />", idx); |
| 119 |
if (end >= 0) { |
| 120 |
String result = testOutput.substring(idx + |
| 121 |
"Statements per second: ".length(), end); |
| 122 |
now += " " + result + " op/s"; |
| 123 |
} |
| 124 |
} |
| 125 |
String sql = "insert into item(title, issued, desc) values('Build " + |
| 126 |
now + |
| 127 |
(error ? " FAILED" : "") + |
| 128 |
(coverageFailed ? " COVERAGE" : "") + |
| 129 |
"', '" + ts + |
| 130 |
"', '<a href=\"https://h2database.com/" + |
| 131 |
"html/testOutput.html\">Output</a>" + |
| 132 |
" - <a href=\"https://h2database.com/" + |
| 133 |
"coverage/overview.html\">Coverage</a>" + |
| 134 |
" - <a href=\"https://h2database.com/" + |
| 135 |
"automated/h2-latest.jar\">Jar</a>');\n"; |
| 136 |
buildSql += sql; |
| 137 |
Connection conn; |
| 138 |
try { |
| 139 |
Class.forName("org.h2.Driver"); |
| 140 |
conn = DriverManager.getConnection("jdbc:h2:mem:"); |
| 141 |
} catch (Exception e) { |
| 142 |
Class.forName("org.h2.upgrade.v1_1.Driver"); |
| 143 |
conn = DriverManager.getConnection("jdbc:h2v1_1:mem:"); |
| 144 |
} |
| 145 |
conn.createStatement().execute(buildSql); |
| 146 |
String newsfeed = new String(Files.readAllBytes(Paths.get("src/tools/org/h2/build/doc/buildNewsfeed.sql")), |
| 147 |
StandardCharsets.UTF_8); |
| 148 |
ScriptReader r = new ScriptReader(new StringReader(newsfeed)); |
| 149 |
Statement stat = conn.createStatement(); |
| 150 |
ResultSet rs = null; |
| 151 |
while (true) { |
| 152 |
String s = r.readStatement(); |
| 153 |
if (s == null) { |
| 154 |
break; |
| 155 |
} |
| 156 |
if (stat.execute(s)) { |
| 157 |
rs = stat.getResultSet(); |
| 158 |
} |
| 159 |
} |
| 160 |
rs.next(); |
| 161 |
String content = rs.getString("content"); |
| 162 |
conn.close(); |
| 163 |
ftp.store("/httpdocs/automated/history.sql", |
| 164 |
new ByteArrayInputStream(buildSql.getBytes())); |
| 165 |
ftp.store("/httpdocs/automated/news.xml", |
| 166 |
new ByteArrayInputStream(content.getBytes())); |
| 167 |
ftp.store("/httpdocs/html/testOutput.html", |
| 168 |
new ByteArrayInputStream(testOutput.getBytes())); |
| 169 |
String jarFileName = "bin/h2-" + Constants.VERSION + ".jar"; |
| 170 |
if (FileUtils.exists(jarFileName)) { |
| 171 |
ftp.store("/httpdocs/automated/h2-latest.jar", |
| 172 |
Files.newInputStream(Paths.get(jarFileName))); |
| 173 |
} |
| 174 |
if (coverage) { |
| 175 |
ftp.store("/httpdocs/coverage/overview.html", |
| 176 |
Files.newInputStream(Paths.get("coverage/overview.html"))); |
| 177 |
ftp.store("/httpdocs/coverage/coverage.zip", |
| 178 |
Files.newInputStream(Paths.get("coverage.zip"))); |
| 179 |
FileUtils.delete("coverage.zip"); |
| 180 |
} |
| 181 |
String mavenRepoDir = System.getProperty("user.home") + "/.m2/repository/"; |
| 182 |
boolean mavenSnapshot = Files.exists(Paths.get(mavenRepoDir + |
| 183 |
"com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar")); |
| 184 |
if (mavenSnapshot) { |
| 185 |
if (!ftp.exists("/httpdocs", "m2-repo")) { |
| 186 |
ftp.makeDirectory("/httpdocs/m2-repo"); |
| 187 |
} |
| 188 |
if (!ftp.exists("/httpdocs/m2-repo", "com")) { |
| 189 |
ftp.makeDirectory("/httpdocs/m2-repo/com"); |
| 190 |
} |
| 191 |
if (!ftp.exists("/httpdocs/m2-repo/com", "h2database")) { |
| 192 |
ftp.makeDirectory("/httpdocs/m2-repo/com/h2database"); |
| 193 |
} |
| 194 |
if (!ftp.exists("/httpdocs/m2-repo/com/h2database", "h2")) { |
| 195 |
ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2"); |
| 196 |
} |
| 197 |
if (!ftp.exists("/httpdocs/m2-repo/com/h2database/h2", "1.0-SNAPSHOT")) { |
| 198 |
ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2/1.0-SNAPSHOT"); |
| 199 |
} |
| 200 |
if (!ftp.exists("/httpdocs/m2-repo/com/h2database", "h2-mvstore")) { |
| 201 |
ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2-mvstore"); |
| 202 |
} |
| 203 |
if (!ftp.exists("/httpdocs/m2-repo/com/h2database/h2-mvstore", "1.0-SNAPSHOT")) { |
| 204 |
ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2-mvstore/1.0-SNAPSHOT"); |
| 205 |
} |
| 206 |
ftp.store("/httpdocs/m2-repo/com/h2database/h2" + |
| 207 |
"/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.pom", |
| 208 |
Files.newInputStream(Paths.get(mavenRepoDir + |
| 209 |
"com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.pom"))); |
| 210 |
ftp.store("/httpdocs/m2-repo/com/h2database/h2" + |
| 211 |
"/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar", |
| 212 |
Files.newInputStream(Paths.get(mavenRepoDir + |
| 213 |
"com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar"))); |
| 214 |
ftp.store("/httpdocs/m2-repo/com/h2database/h2-mvstore" + |
| 215 |
"/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.pom", |
| 216 |
Files.newInputStream(Paths.get(mavenRepoDir + |
| 217 |
"com/h2database/h2-mvstore/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.pom"))); |
| 218 |
ftp.store("/httpdocs/m2-repo/com/h2database/h2-mvstore" + |
| 219 |
"/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.jar", |
| 220 |
Files.newInputStream(Paths.get(mavenRepoDir + |
| 221 |
"com/h2database/h2-mvstore/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.jar"))); |
| 222 |
} |
| 223 |
ftp.close(); |
| 224 |
} |
| 225 |
|
| 226 |
private static void zip(String destFile, String directory, boolean storeOnly) throws IOException { |
| 227 |
ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(Paths.get(destFile))); |
| 228 |
if (storeOnly) { |
| 229 |
zipOut.setMethod(ZipOutputStream.STORED); |
| 230 |
} |
| 231 |
zipOut.setLevel(Deflater.BEST_COMPRESSION); |
| 232 |
Path base = Paths.get(directory); |
| 233 |
Files.walkFileTree(base, new SimpleFileVisitor<Path>() { |
| 234 |
@Override |
| 235 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 236 |
byte[] data = Files.readAllBytes(file); |
| 237 |
ZipEntry entry = new ZipEntry(base.relativize(file).toString().replace('\\', '/')); |
| 238 |
CRC32 crc = new CRC32(); |
| 239 |
crc.update(data); |
| 240 |
entry.setSize(data.length); |
| 241 |
entry.setCrc(crc.getValue()); |
| 242 |
zipOut.putNextEntry(entry); |
| 243 |
zipOut.write(data); |
| 244 |
zipOut.closeEntry(); |
| 245 |
return FileVisitResult.CONTINUE; |
| 246 |
} |
| 247 |
}); |
| 248 |
zipOut.finish(); |
| 249 |
zipOut.close(); |
| 250 |
} |
| 251 |
|
| 252 |
} |