allow suppression of npm management logging

This commit is contained in:
Grahame Grieve 2022-05-11 06:58:46 +10:00
parent 4b1b99a61c
commit 9c20a28710
3 changed files with 41 additions and 12 deletions

View File

@ -18,6 +18,7 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
private static final Logger ourLog = LoggerFactory.getLogger(BasePackageCacheManager.class); private static final Logger ourLog = LoggerFactory.getLogger(BasePackageCacheManager.class);
private List<String> myPackageServers = new ArrayList<>(); private List<String> myPackageServers = new ArrayList<>();
private Function<String, PackageClient> myClientFactory = address -> new CachingPackageClient(address); private Function<String, PackageClient> myClientFactory = address -> new CachingPackageClient(address);
protected boolean silent;
/** /**
* Constructor * Constructor
@ -77,10 +78,12 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
String url = packageClient.url(id, version); String url = packageClient.url(id, version);
return new InputStreamWithSrc(stream, url, version); return new InputStreamWithSrc(stream, url, version);
} catch (IOException e) { } catch (IOException e) {
if (!silent) {
ourLog.info("Failed to resolve package {}#{} from server: {} ({})", id, version, nextPackageServer, e.getMessage()); ourLog.info("Failed to resolve package {}#{} from server: {} ({})", id, version, nextPackageServer, e.getMessage());
} }
} }
} }
}
return null; return null;
} }
@ -177,4 +180,9 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
return loadPackage(idAndVer, null); return loadPackage(idAndVer, null);
} }
public void setSilent(boolean silent) {
this.silent = silent;
}
} }

View File

@ -344,15 +344,15 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
public NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException { public NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException {
checkValidVersionString(version, id); checkValidVersionString(version, id);
if (progress) { if (progress) {
System.out.println("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache"); log("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache");
System.out.print(" Fetching:"); log(" Fetching:");
} }
NpmPackage npm = NpmPackage.fromPackage(packageTgzInputStream, sourceDesc, true); NpmPackage npm = NpmPackage.fromPackage(packageTgzInputStream, sourceDesc, true);
if (progress) { if (progress) {
System.out.println(); log("");
System.out.print(" Installing: "); logn(" Installing: ");
} }
if (!suppressErrors && npm.name() == null || id == null || !id.equalsIgnoreCase(npm.name())) { if (!suppressErrors && npm.name() == null || id == null || !id.equalsIgnoreCase(npm.name())) {
@ -374,7 +374,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
try { try {
Utilities.clearDirectory(packRoot); Utilities.clearDirectory(packRoot);
} catch (Throwable t) { } catch (Throwable t) {
System.out.println("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later"); log("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later");
} }
int i = 0; int i = 0;
@ -392,10 +392,10 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
i++; i++;
if (progress && i % 50 == 0) { if (progress && i % 50 == 0) {
c++; c++;
System.out.print("."); logn(".");
if (c == 120) { if (c == 120) {
System.out.println(""); log("");
System.out.print(" "); logn(" ");
c = 2; c = 2;
} }
} }
@ -409,7 +409,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
ini.setIntegerProperty("package-sizes", id + "#" + v, size, null); ini.setIntegerProperty("package-sizes", id + "#" + v, size, null);
ini.save(); ini.save();
if (progress) if (progress)
System.out.println(" done."); log(" done.");
} }
pck = loadPackageInfo(packRoot); 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")) || !v.equals(JSONUtil.str(npm.getNpm(), "version"))) {
@ -428,7 +428,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} catch (Exception e) { } catch (Exception e) {
try { try {
// don't leave a half extracted package behind // don't leave a half extracted package behind
System.out.println("Clean up package " + packRoot + " because installation failed: " + e.getMessage()); log("Clean up package " + packRoot + " because installation failed: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
Utilities.clearDirectory(packRoot); Utilities.clearDirectory(packRoot);
new File(packRoot).delete(); new File(packRoot).delete();
@ -441,6 +441,18 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}); });
} }
private void log(String s) {
if (!silent) {
System.out.println(s);
}
}
private void logn(String s) {
if (!silent) {
System.out.print(s);
}
}
@Override @Override
public String getPackageUrl(String packageId) throws IOException { public String getPackageUrl(String packageId) throws IOException {
String result = super.getPackageUrl(packageId); String result = super.getPackageUrl(packageId);
@ -646,7 +658,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
try { try {
loadFromBuildServer(); loadFromBuildServer();
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error connecting to build server - running without build (" + e.getMessage() + ")"); log("Error connecting to build server - running without build (" + e.getMessage() + ")");
e.printStackTrace(); e.printStackTrace();
} }
return false; return false;

View File

@ -63,8 +63,10 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.json.JSONUtil; import org.hl7.fhir.utilities.json.JSONUtil;
import org.hl7.fhir.utilities.json.JsonTrackingParser; import org.hl7.fhir.utilities.json.JsonTrackingParser;
import org.hl7.fhir.utilities.npm.NpmPackage.ITransformingLoader; import org.hl7.fhir.utilities.npm.NpmPackage.ITransformingLoader;
@ -1160,5 +1162,12 @@ public class NpmPackage {
} }
} }
public static NpmPackage fromUrl(String source) throws IOException {
SimpleHTTPClient fetcher = new SimpleHTTPClient();
HTTPResult res = fetcher.get(source+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return fromPackage(new ByteArrayInputStream(res.getContent()));
}
} }