add saving packages

This commit is contained in:
Grahame Grieve 2019-11-05 14:53:48 +11:00
parent d582e41c03
commit be76704609
4 changed files with 179 additions and 7 deletions

View File

@ -0,0 +1,52 @@
package org.hl7.fhir.convertors.misc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map.Entry;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.cache.NpmPackage;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class XMLPackageConvertor {
public static void main(String[] args) throws FileNotFoundException, IOException {
new XMLPackageConvertor().process(new File("C:\\web\\hl7.org\\fhir"));
}
private void process(File folder) throws FileNotFoundException, IOException {
for (File f : folder.listFiles()) {
if (f.isDirectory()) {
process(f);
} else {
if (f.getName().endsWith(".tgz")) {
System.out.println("Package "+f.getAbsolutePath());
NpmPackage p = NpmPackage.fromPackage(new FileInputStream(f));
if (p.getNpm().has("dependencies")) {
JsonObject dep = p.getNpm().getAsJsonObject("dependencies");
if (dep.entrySet().isEmpty()) {
System.out.println(" Dependencies: none");
} else {
System.out.println(" Dependencies:");
for (Entry<String, JsonElement> e : dep.entrySet()) {
System.out.println(" "+e.getKey()+": "+e.getValue().getAsString());
}
}
} else {
System.out.println(" Dependencies: n/a");
}
}
}
}
}
}

View File

@ -29,6 +29,8 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -40,7 +42,9 @@ import java.util.zip.ZipInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.exceptions.FHIRException;
@ -51,6 +55,8 @@ import org.hl7.fhir.utilities.cache.PackageCacheManager.PackageEntry;
import org.hl7.fhir.utilities.cache.PackageGenerator.PackageType;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -463,4 +469,57 @@ import com.google.gson.JsonObject;
FileUtils.copyDirectory(new File(path), new File(dir));
}
public void save(OutputStream stream) throws IOException {
TarArchiveOutputStream tar;
ByteArrayOutputStream OutputStream;
BufferedOutputStream bufferedOutputStream;
GzipCompressorOutputStream gzipOutputStream;
OutputStream = new ByteArrayOutputStream();
bufferedOutputStream = new BufferedOutputStream(OutputStream);
gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
tar = new TarArchiveOutputStream(gzipOutputStream);
NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
indexer.start();
for (String s : content.keySet()) {
byte[] b = content.get(s);
if (s.startsWith("package/")) {
indexer.seeFile(tail(s), b);
}
if (!s.endsWith(".index.json") && !s.equals("package/package.json")) {
TarArchiveEntry entry = new TarArchiveEntry(s);
entry.setSize(b.length);
tar.putArchiveEntry(entry);
tar.write(b);
tar.closeArchiveEntry();
}
}
byte[] cnt = new GsonBuilder().setPrettyPrinting().create().toJson(npm).getBytes(Charset.forName("UTF-8"));
TarArchiveEntry entry = new TarArchiveEntry("package/package.json");
entry.setSize(cnt.length);
tar.putArchiveEntry(entry);
tar.write(cnt);
tar.closeArchiveEntry();
cnt = indexer.build().getBytes(Charset.forName("UTF-8"));
entry = new TarArchiveEntry("package/.index.json");
entry.setSize(cnt.length);
tar.putArchiveEntry(entry);
tar.write(cnt);
tar.closeArchiveEntry();
tar.finish();
tar.close();
gzipOutputStream.close();
bufferedOutputStream.close();
OutputStream.close();
byte[] b = OutputStream.toByteArray();
stream.write(b);
}
private String tail(String s) {
return s.substring(s.lastIndexOf("/")+1);
}
}

View File

@ -529,19 +529,14 @@ public class PackageCacheManager {
int i = 0;
int c = 0;
int size = 0;
for (Entry<String, byte[]> e : npm.getContent().entrySet()) {
// if (e.getValue() == null) {
// thorw
// File f = new File(Utilities.path(packRoot, e.name));
// if (!f.mkdir())
// throw new IOException("Unable to create directory '%s', during extraction of archive contents: "+ f.getAbsolutePath());
// } else {
String fn = Utilities.path(packRoot, e.getKey());
String dir = Utilities.getDirectoryForFile(fn);
if (!(new File(dir).exists()))
Utilities.createDirectory(dir);
TextFile.bytesToFile(e.getValue(), fn);
// }
size = size + e.getValue().length;
i++;
if (progress && i % 50 == 0) {
c++;
@ -563,6 +558,7 @@ public class PackageCacheManager {
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.setTimeStampFormat("yyyyMMddhhmmss");
ini.setTimestampProperty("packages", id+"#"+version, Timestamp.from(Instant.now()), null);
ini.setIntegerProperty("package-sizes", id+"#"+version, size, null);
ini.save();
if (progress)
System.out.println(" done.");

View File

@ -0,0 +1,65 @@
package org.hl7.fhir.utilities.cache;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import org.hl7.fhir.utilities.Utilities;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
/**
* intenral use only - set the file name to edit in main(), and fill out the edit routine
*
* @author grahame
*
*/
public class PackageHacker {
public static void main(String[] args) throws FileNotFoundException, IOException {
}
private void edit(String name) throws FileNotFoundException, IOException {
File f = new File(name);
if (!f.exists())
throw new Error("Unable to find "+f.getAbsolutePath());
NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
System.out.println("Altering Package "+f.getAbsolutePath());
System.out.println(nice(pck.getNpm()));
change(pck.getNpm(), pck.getContent());
System.out.println("Revised Package");
System.out.println("=======================");
System.out.println(nice(pck.getNpm()));
System.out.println("=======================");
System.out.print("save? y/n: ");
int r = System.in.read();
if (r == 'y') {
f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
pck.save(new FileOutputStream(f));
}
}
private String nice(JsonObject json) {
return new GsonBuilder().setPrettyPrinting().create().toJson(json);
}
private void change(JsonObject npm, Map<String, byte[]> content) {
JsonArray fhirVersions = new JsonArray();
fhirVersions.add("1.9.0");
npm.add("fhirVersions", fhirVersions);
JsonObject dependencies = new JsonObject();
dependencies.addProperty("hl7.fhir.r3.core", "3.0.2");
npm.remove("dependencies");
// npm.add("dependencies", dependencies);
}
}