use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method loadPackageFromFile.
private NpmPackage loadPackageFromFile(String id, String folder) throws IOException {
File f = new File(Utilities.path(folder, id));
if (!f.exists()) {
throw new FHIRException("Package '" + id + " not found in folder " + folder);
}
if (!f.isDirectory()) {
throw new FHIRException("File for '" + id + " found in folder " + folder + ", not a folder");
}
File fp = new File(Utilities.path(folder, id, "package", "package.json"));
if (!fp.exists()) {
throw new FHIRException("Package '" + id + " found in folder " + folder + ", but does not contain a package.json file in /package");
}
return NpmPackage.fromFolder(f.getAbsolutePath());
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method addPackageToCache.
/**
* Add an already fetched package to the cache
*/
@Override
public NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException {
checkValidVersionString(version, id);
if (progress) {
log("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache");
log(" Fetching:");
}
NpmPackage npm = NpmPackage.fromPackage(packageTgzInputStream, sourceDesc, true);
if (progress) {
log("");
logn(" Installing: ");
}
if (!suppressErrors && npm.name() == null || id == null || !id.equalsIgnoreCase(npm.name())) {
if (!id.equals("hl7.fhir.r5.core") && !id.equals("hl7.fhir.us.immds")) {
// temporary work around
throw new IOException("Attempt to import a mis-identified package. Expected " + id + ", got " + npm.name());
}
}
if (version == null)
version = npm.version();
String v = version;
return new CacheLock(id + "#" + version).doWithLock(() -> {
NpmPackage pck = null;
String packRoot = Utilities.path(cacheFolder, id + "#" + v);
try {
// ok, now we have a lock on it... check if something created it while we were waiting
if (!new File(packRoot).exists() || Utilities.existsInList(v, "current", "dev")) {
Utilities.createDirectory(packRoot);
try {
Utilities.clearDirectory(packRoot);
} catch (Throwable t) {
log("Unable to clear directory: " + packRoot + ": " + t.getMessage() + " - this may cause problems later");
}
int i = 0;
int c = 0;
int size = 0;
for (Entry<String, NpmPackageFolder> e : npm.getFolders().entrySet()) {
String dir = e.getKey().equals("package") ? Utilities.path(packRoot, "package") : Utilities.path(packRoot, "package", e.getKey());
if (!(new File(dir).exists()))
Utilities.createDirectory(dir);
for (Entry<String, byte[]> fe : e.getValue().getContent().entrySet()) {
String fn = Utilities.path(dir, Utilities.cleanFileName(fe.getKey()));
byte[] cnt = fe.getValue();
TextFile.bytesToFile(cnt, fn);
size = size + cnt.length;
i++;
if (progress && i % 50 == 0) {
c++;
logn(".");
if (c == 120) {
log("");
logn(" ");
c = 2;
}
}
}
}
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.setTimeStampFormat("yyyyMMddhhmmss");
ini.setTimestampProperty("packages", id + "#" + v, Timestamp.from(Instant.now()), null);
ini.setIntegerProperty("package-sizes", id + "#" + v, size, null);
ini.save();
if (progress)
log(" done.");
}
pck = loadPackageInfo(packRoot);
if (!id.equals(JSONUtil.str(npm.getNpm(), "name")) || !v.equals(JSONUtil.str(npm.getNpm(), "version"))) {
if (!id.equals(JSONUtil.str(npm.getNpm(), "name"))) {
npm.getNpm().addProperty("original-name", JSONUtil.str(npm.getNpm(), "name"));
npm.getNpm().remove("name");
npm.getNpm().addProperty("name", id);
}
if (!v.equals(JSONUtil.str(npm.getNpm(), "version"))) {
npm.getNpm().addProperty("original-version", JSONUtil.str(npm.getNpm(), "version"));
npm.getNpm().remove("version");
npm.getNpm().addProperty("version", v);
}
TextFile.stringToFile(new GsonBuilder().setPrettyPrinting().create().toJson(npm.getNpm()), Utilities.path(cacheFolder, id + "#" + v, "package", "package.json"), false);
}
} catch (Exception e) {
try {
// don't leave a half extracted package behind
log("Clean up package " + packRoot + " because installation failed: " + e.getMessage());
e.printStackTrace();
Utilities.clearDirectory(packRoot);
new File(packRoot).delete();
} catch (Exception ei) {
// nothing
}
throw e;
}
return pck;
});
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class NpmPackage method fromFolder.
public static NpmPackage fromFolder(String folder, PackageType defType, String... exemptions) throws IOException {
NpmPackage res = new NpmPackage();
res.loadFiles(folder, new File(folder), exemptions);
if (!res.folders.containsKey("package")) {
res.folders.put("package", res.new NpmPackageFolder("package"));
}
if (!res.folders.get("package").hasFile("package.json") && defType != null) {
TextFile.stringToFile("{ \"type\" : \"" + defType.getCode() + "\"}", Utilities.path(res.folders.get("package").folder.getAbsolutePath(), "package.json"));
}
res.npm = (JsonObject) new com.google.gson.JsonParser().parse(new String(res.folders.get("package").fetchFile("package.json")));
return res;
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class NpmPackage method unPack.
public void unPack(String dir, boolean withAppend) throws IOException {
for (NpmPackageFolder folder : folders.values()) {
String dn = folder.getName();
if (!dn.equals("package") && (dn.startsWith("package/") || dn.startsWith("package\\"))) {
dn = dn.substring(8);
}
if (dn.equals("$root")) {
dn = dir;
} else {
dn = Utilities.path(dir, dn);
}
Utilities.createDirectory(dn);
for (String s : folder.listFiles()) {
String fn = Utilities.path(dn, s);
File f = new File(fn);
if (withAppend && f.getName().startsWith("_append.")) {
String appendFn = Utilities.path(dn, s.substring(8));
if (new File(appendFn).exists())
TextFile.appendBytesToFile(folder.fetchFile(s), appendFn);
else
TextFile.bytesToFile(folder.fetchFile(s), appendFn);
} else
TextFile.bytesToFile(folder.fetchFile(s), fn);
}
// if (path != null)
// FileUtils.copyDirectory(new File(path), new File(dir));
}
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class NpmPackage method readStream.
public void readStream(InputStream tgz, String desc, boolean progress) throws IOException {
GzipCompressorInputStream gzipIn;
try {
gzipIn = new GzipCompressorInputStream(tgz);
} catch (Exception e) {
throw new IOException("Error reading " + (desc == null ? "package" : desc) + ": " + e.getMessage(), e);
}
try (TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
TarArchiveEntry entry;
int i = 0;
int c = 12;
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
i++;
String n = entry.getName();
if (entry.isDirectory()) {
String dir = n.substring(0, n.length() - 1);
if (dir.startsWith("package/")) {
dir = dir.substring(8);
}
folders.put(dir, new NpmPackageFolder(dir));
} else {
int count;
byte[] data = new byte[BUFFER_SIZE];
ByteArrayOutputStream fos = new ByteArrayOutputStream();
try (BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
while ((count = tarIn.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, count);
}
}
fos.close();
loadFile(n, fos.toByteArray());
}
if (progress && i % 50 == 0) {
c++;
System.out.print(".");
if (c == 120) {
System.out.println("");
System.out.print(" ");
c = 2;
}
}
}
}
try {
npm = JsonTrackingParser.parseJson(folders.get("package").fetchFile("package.json"));
} catch (Exception e) {
throw new IOException("Error parsing " + (desc == null ? "" : desc + "#") + "package/package.json: " + e.getMessage(), e);
}
checkIndexed(desc);
}
Aggregations