Merge pull request #1334 from hapifhir/gg-202206-npm-install

Gg 202206 npm install
This commit is contained in:
Grahame Grieve 2023-07-01 08:21:55 +10:00 committed by GitHub
commit b8f34be136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 140 additions and 52 deletions

View File

@ -258,8 +258,11 @@ public class FTPClient {
private void attemptUpload(String source, String resolvedPath) throws IOException {
final long startTime = System.nanoTime();
FileInputStream localStream = new FileInputStream(source);
clientImpl.storeFile(resolvedPath, localStream);
localStream.close();
try {
clientImpl.storeFile(resolvedPath, localStream);
} finally {
localStream.close();
}
this.storeFileTimeNanos += System.nanoTime() - startTime;
}

View File

@ -90,11 +90,16 @@ public class TextFile {
public static void stringToFile(String content, File file) throws IOException {
OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);
sw.flush();
sw.close();
FileOutputStream fs = new FileOutputStream(file);
try {
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);
sw.flush();
sw.close();
} finally {
fs.close();
}
}
public static void stringToStream(String content, OutputStream stream, boolean bom) throws IOException {
@ -124,7 +129,8 @@ public class TextFile {
}
public static void stringToFile(String content, File file, boolean bom) throws IOException {
OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
FileOutputStream fs = new FileOutputStream(file);
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
if (bom)
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
sw.write(content);

View File

@ -628,6 +628,15 @@ public class Utilities {
return PathBuilder.getPathBuilder().buildPath(args);
}
public static String path(File f, String... args) throws IOException {
String[] a = new String[args.length+1];
a[0] = f.getAbsolutePath();
for (int i = 0; i < args.length; i++) {
a[i+1] = args[i];
}
return PathBuilder.getPathBuilder().buildPath(a);
}
/**
* Composes a path string using by concatenating the passed arguments.
*

View File

@ -91,11 +91,11 @@ public class XsltUtilities {
return res.getOutputStream().toString();
}
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws TransformerException, IOException {
saxonTransform(xsltDir, source, xslt, dest, alt, null);
}
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map<String, String> params) throws FileNotFoundException, TransformerException {
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map<String, String> params) throws TransformerException, IOException {
TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
@ -108,12 +108,19 @@ public class XsltUtilities {
}
t.setURIResolver(new MyURIResolver(xsltDir, alt));
StreamSource src = new StreamSource(new FileInputStream(source));
StreamResult res = new StreamResult(new FileOutputStream(dest));
t.transform(src, res);
FileInputStream fso = new FileInputStream(source);
FileOutputStream fsr = new FileOutputStream(dest);
try {
StreamSource src = new StreamSource(fso);
StreamResult res = new StreamResult(fsr);
t.transform(src, res);
} finally {
fso.close();
fsr.close();
}
}
public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws TransformerException, IOException {
TransformerFactory f = TransformerFactory.newInstance();
StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
@ -121,9 +128,16 @@ public class XsltUtilities {
Transformer t = f.newTransformer(xsrc);
t.setURIResolver(new MyURIResolver(xsltDir, alt));
StreamSource src = new StreamSource(new FileInputStream(source));
StreamResult res = new StreamResult(new FileOutputStream(dest));
t.transform(src, res);
FileInputStream fss = new FileInputStream(source);
FileOutputStream fsr = new FileOutputStream(dest);
try {
StreamSource src = new StreamSource(fss);
StreamResult res = new StreamResult(fsr);
t.transform(src, res);
} finally {
fss.close();
fsr.close();
}
}

View File

@ -49,7 +49,12 @@ public class JsonLangFileProducer extends LanguageFileProducer {
@Override
public void finish() throws IOException {
JsonParser.compose(json, new FileOutputStream(getFileName(id, baseLang)));
FileOutputStream fs = new FileOutputStream(getFileName(id, baseLang));
try {
JsonParser.compose(json, fs);
} finally {
fs.close();
}
}
}

View File

@ -109,7 +109,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
public static final String PACKAGE_VERSION_REGEX_OPT = "^[A-Za-z][A-Za-z0-9\\_\\-]*(\\.[A-Za-z0-9\\_\\-]+)+(\\#[A-Za-z0-9\\-\\_]+(\\.[A-Za-z0-9\\-\\_]+)*)?$";
private static final Logger ourLog = LoggerFactory.getLogger(FilesystemPackageCacheManager.class);
private static final String CACHE_VERSION = "3"; // second version - see wiki page
private String cacheFolder;
private File cacheFolder;
private boolean progress = true;
private List<NpmPackage> temporaryPackages = new ArrayList<>();
private boolean buildLoaded = false;
@ -134,7 +134,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
* @throws IOException
*/
public FilesystemPackageCacheManager(String customFolder) throws IOException {
this.cacheFolder = customFolder;
this.cacheFolder = new File(customFolder);
init(FilesystemPackageCacheMode.CUSTOM);
}
@ -144,27 +144,33 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
switch (mode) {
case SYSTEM:
cacheFolder = Utilities.path("var", "lib", ".fhir", "packages");
cacheFolder = new File(Utilities.path("var", "lib", ".fhir", "packages"));
break;
case USER:
cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "packages");
cacheFolder = new File(Utilities.path(System.getProperty("user.home"), ".fhir", "packages"));
break;
case TESTING:
cacheFolder = Utilities.path("[tmp]", ".fhir", "packages");
cacheFolder = new File(Utilities.path("[tmp]", ".fhir", "packages"));
break;
case CUSTOM:
if (!new File(cacheFolder).exists()) {
if (!cacheFolder.exists()) {
throw new FHIRException("The folder ''"+cacheFolder+"' could not be found");
}
default:
break;
}
if (!(new File(cacheFolder).exists()))
Utilities.createDirectory(cacheFolder);
if (!(cacheFolder.exists()))
Utilities.createDirectory(cacheFolder.getAbsolutePath());
if (!(new File(Utilities.path(cacheFolder, "packages.ini")).exists()))
TextFile.stringToFile("[cache]\r\nversion=" + CACHE_VERSION + "\r\n\r\n[urls]\r\n\r\n[local]\r\n\r\n", Utilities.path(cacheFolder, "packages.ini"), false);
createIniFile();
for (File f : cacheFolder.listFiles()) {
if (f.isDirectory() && Utilities.isValidUUID(f.getName())) {
Utilities.clearDirectory(f.getAbsolutePath());
f.delete();
}
}
}
public boolean isMinimalMemory() {
@ -187,14 +193,19 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
if (files != null) {
for (File f : files) {
if (f.getName().endsWith(".tgz")) {
temporaryPackages.add(NpmPackage.fromPackage(new FileInputStream(f)));
FileInputStream fs = new FileInputStream(f);
try {
temporaryPackages.add(NpmPackage.fromPackage(fs));
} finally {
fs.close();
}
}
}
}
}
public String getFolder() {
return cacheFolder;
return cacheFolder.getAbsolutePath();
}
private NpmPackage loadPackageInfo(String path) throws IOException {
@ -203,7 +214,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
private void clearCache() throws IOException {
for (File f : new File(cacheFolder).listFiles()) {
for (File f : cacheFolder.listFiles()) {
if (f.isDirectory()) {
new CacheLock(f.getName()).doWithLock(() -> {
Utilities.clearDirectory(f.getAbsolutePath());
@ -303,7 +314,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
public String getLatestVersionFromCache(String id) throws IOException {
for (String f : Utilities.reverseSorted(new File(cacheFolder).list())) {
for (String f : Utilities.reverseSorted(cacheFolder.list())) {
File cf = new File(Utilities.path(cacheFolder, f));
if (cf.isDirectory()) {
if (f.startsWith(id + "#")) {
@ -391,7 +402,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
String foundPackage = null;
String foundVersion = null;
for (String f : Utilities.reverseSorted(new File(cacheFolder).list())) {
for (String f : Utilities.reverseSorted(cacheFolder.list())) {
File cf = new File(Utilities.path(cacheFolder, f));
if (cf.isDirectory()) {
if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
@ -860,7 +871,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
public List<String> listPackages() {
List<String> res = new ArrayList<>();
for (File f : new File(cacheFolder).listFiles()) {
for (File f : cacheFolder.listFiles()) {
if (f.isDirectory() && f.getName().contains("#")) {
res.add(f.getName());
}
@ -1016,7 +1027,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
}
for (String f : Utilities.sorted(new File(cacheFolder).list())) {
for (String f : Utilities.sorted(cacheFolder.list())) {
if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
return true;
}

View File

@ -447,8 +447,7 @@ public class NpmPackage {
}
public static NpmPackage extractFromTgz(InputStream tgz, String desc, String tempDir, boolean minimal) throws IOException {
String dest = Utilities.path(tempDir, "package");
Utilities.createDirectory(dest);
Utilities.createDirectory(tempDir);
int size = 0;
@ -463,21 +462,18 @@ public class NpmPackage {
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
String n = entry.getName();
if (n.startsWith("package/")) {
n = n.substring(8);
}
if (n.contains("..")) {
throw new RuntimeException("Entry with an illegal name: " + n);
}
if (entry.isDirectory()) {
if (!Utilities.noString(n)) {
String dir = n.substring(0, n.length()-1);
Utilities.createDirectory(Utilities.path(dest, dir));
Utilities.createDirectory(Utilities.path(tempDir, dir));
}
} else {
int count;
byte data[] = new byte[BUFFER_SIZE];
String filename = Utilities.path(dest, n);
String filename = Utilities.path(tempDir, n);
String folder = Utilities.getDirectoryForFile(filename);
Utilities.createDirectory(folder);
FileOutputStream fos = new FileOutputStream(filename);

View File

@ -37,7 +37,13 @@ public class PackageHacker {
if (!f.exists())
throw new Error("Unable to find "+f.getAbsolutePath());
NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
NpmPackage pck = null;
FileInputStream fs = new FileInputStream(f);
try {
pck = NpmPackage.fromPackage(fs);
} finally {
fs.close();
}
System.out.println("Altering Package "+f.getAbsolutePath());
System.out.println(nice(pck.getNpm()));
@ -51,7 +57,12 @@ public class PackageHacker {
int r = System.in.read();
if (r == 'y') {
f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
pck.save(new FileOutputStream(f));
FileOutputStream fso = new FileOutputStream(f);
try {
pck.save(fso);
} finally {
fso.close();
}
}
}

View File

@ -68,7 +68,6 @@ public class PackageScanner {
}
}
}
System.out.println("!");
}
}

View File

@ -30,7 +30,12 @@ public class ResourceRenamer {
String rt = r.asString("resourceType");
String id = r.asString("id");
String nn = Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json");
JsonParser.compose(r, new FileOutputStream(nn), true);
FileOutputStream fs = new FileOutputStream(nn);
try {
JsonParser.compose(r, fs, true);
} finally {
fs.close();
}
}
}

View File

@ -1008,9 +1008,13 @@ public class HierarchicalTableGenerator extends TranslatingUtilities {
}
newFile.createNewFile();
FileOutputStream stream = new FileOutputStream(file);
genImage(indents, hasChildren, lineColor, stream);
if (outputTracker!=null)
outputTracker.add(file);
try {
genImage(indents, hasChildren, lineColor, stream);
if (outputTracker!=null)
outputTracker.add(file);
} finally {
stream.close();
}
}
return b.toString();
}

View File

@ -117,7 +117,12 @@ public class XLSXmlNormaliser {
if (!hasComment)
root.appendChild(xml.createComment("canonicalized"));
try {
saveXml(new FileOutputStream(dest));
FileOutputStream fs = new FileOutputStream(dest);
try {
saveXml(fs);
} finally {
fs.close();
}
String s = TextFile.fileToString(dest);
s = s.replaceAll("\r\n","\n");
s = replaceSignificantEoln(s);

View File

@ -55,6 +55,7 @@ import javax.xml.transform.stream.StreamResult;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@ -458,14 +459,24 @@ public class XMLUtil {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new FileInputStream(filename));
FileInputStream fs = new FileInputStream(filename);
try {
return builder.parse(fs);
} finally {
fs.close();
}
}
public static Document parseFileToDom(String filename, boolean ns) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(ns);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new FileInputStream(filename));
FileInputStream fs = new FileInputStream(filename);
try {
return builder.parse(fs);
} finally {
fs.close();
}
}
public static Element getLastChild(Element e) {

View File

@ -27,7 +27,12 @@ public class XmlEscaper {
}
public static void convert(String source, String target) throws IOException {
convertAndClose(new FileInputStream(source), new FileOutputStream(target));
FileOutputStream fs = new FileOutputStream(target);
try {
convertAndClose(new FileInputStream(source), fs);
} finally {
fs.close();
}
}
public static void convertAndClose(InputStream source, OutputStream target) throws IOException {

View File

@ -74,7 +74,11 @@ public class XmlGenerator {
public void generate(Element element, File file) throws IOException, FHIRException {
OutputStream stream = new FileOutputStream(file);
generate(element, stream);
try {
generate(element, stream);
} finally {
stream.close();
}
}
public void generate(Element element, OutputStream stream) throws IOException, FHIRException {