Search in sources :

Example 1 with NpmPackageFolder

use of org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder 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;
    });
}
Also used : IniFile(org.hl7.fhir.utilities.IniFile) GsonBuilder(com.google.gson.GsonBuilder) IOException(java.io.IOException) NpmPackageFolder(org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) RandomAccessFile(java.io.RandomAccessFile) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 2 with NpmPackageFolder

use of org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder 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;
}
Also used : File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 3 with NpmPackageFolder

use of org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder in project org.hl7.fhir.core by hapifhir.

the class NpmPackage method loadAllFiles.

public void loadAllFiles(ITransformingLoader loader) throws IOException {
    for (String folder : folders.keySet()) {
        NpmPackageFolder pf = folders.get(folder);
        String p = folder.contains("$") ? path : Utilities.path(path, folder);
        for (File f : new File(p).listFiles()) {
            if (!f.isDirectory() && !isInternalExemptFile(f)) {
                pf.getContent().put(f.getName(), loader.load(f));
            }
        }
    }
}
Also used : File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 4 with NpmPackageFolder

use of org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder 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));
    }
}
Also used : File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 5 with NpmPackageFolder

use of org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder 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);
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

File (java.io.File)8 TextFile (org.hl7.fhir.utilities.TextFile)8 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 ParseException (java.text.ParseException)5 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 GsonBuilder (com.google.gson.GsonBuilder)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)1 IniFile (org.hl7.fhir.utilities.IniFile)1 NpmPackageFolder (org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder)1