Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added the ability to suppress verbose logging during unzip step #40

Merged
merged 7 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private SimpleManifest parseFile(String file) throws IOException, InterruptedExc
}
String lcName = path.getName().toLowerCase();
if(lcName.endsWith(".jar") || lcName.endsWith(".war") || lcName.endsWith(".ear")) {
Map<String, String> mf = path.act(new UnZipStepExecution.UnZipFileCallable(listener, ws, "META-INF/MANIFEST.MF", true, "UTF-8"));
Map<String, String> mf = path.act(new UnZipStepExecution.UnZipFileCallable(listener, ws, "META-INF/MANIFEST.MF", true, "UTF-8", false));
String text = mf.get("META-INF/MANIFEST.MF");
if (isBlank(text)) {
throw new FileNotFoundException(path.getRemote() + " does not seem to contain a manifest.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class UnZipStep extends Step {
private String glob;
private boolean test = false;
private boolean read = false;
private boolean quiet = false;

@DataBoundConstructor
public UnZipStep(String zipFile) throws Descriptor.FormException {
Expand Down Expand Up @@ -165,10 +166,10 @@ public boolean isRead() {
public void setRead(boolean read) {
this.read = read;
}

/**
* Get the charset to use when unzipping the zip file. <em>E.g. UTF-8</em>
*
*
* <code>String version = unzip zipFile: 'example.zip', glob: 'version.txt', read: true, charset: UTF-8</code>
*
* @return String specifying the charset, defaults to UTF-8
Expand All @@ -181,7 +182,7 @@ public String getCharset()

/**
* Set the charset to use when unzipping the zip file.
*
*
* <code>String version = unzip zipFile: 'example.zip', glob: 'version.txt', read: true , charset: UTF-8</code>
*
* @param charset
Expand All @@ -193,6 +194,29 @@ public void setCharset(String charset)
this.charset = (charset.trim().isEmpty()) ? "UTF-8" : charset;
}

/**
* Suppress the verbose output that logs every single file that is dealt with.
* <em>E.g.</em>
* <code>unzip zipFile: 'example.zip', glob: 'version.txt', quiet: true</code>
*
* @return if verbose logging should be suppressed
*/
public boolean isQuiet() {
return quiet;
}

/**
* Suppress the verbose output that logs every single file that is dealt with.
* <em>E.g.</em>
* <code>unzip zipFile: 'example.zip', glob: 'version.txt', quiet: true</code>
*
* @param quiet if verbose logging should be suppressed
*/
@DataBoundSetter
public void setQuiet(boolean quiet) {
this.quiet = quiet;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new UnZipStepExecution(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected Object run() throws Exception {
if (!StringUtils.isBlank(step.getDir())) {
destination = ws.child(step.getDir());
}
return source.act(new UnZipFileCallable(listener, destination, step.getGlob(), step.isRead(),step.getCharset()));
return source.act(new UnZipFileCallable(listener, destination, step.getGlob(), step.isRead(),step.getCharset(),step.isQuiet()));
}

private Boolean test() throws IOException, InterruptedException {
Expand All @@ -113,14 +113,16 @@ public static class UnZipFileCallable extends MasterToSlaveFileCallable<Map<Stri
private final FilePath destination;
private final String glob;
private final boolean read;
private final boolean quiet;
private final String charset;

public UnZipFileCallable(TaskListener listener, FilePath destination, String glob, boolean read, String charset) {
public UnZipFileCallable(TaskListener listener, FilePath destination, String glob, boolean read, String charset, boolean quiet) {
this.listener = listener;
this.destination = destination;
this.glob = glob;
this.read = read;
this.charset = charset;
this.quiet = quiet;
}

@Override
Expand All @@ -137,6 +139,7 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
Charset charsetForZip = Charset.forName(charset);
zip = new ZipFile(zipFile, charsetForZip);
Enumeration<? extends ZipEntry> entries = zip.entries();
Integer fileCount = 0;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (doGlob && !matches(entry.getName(), glob)) {
Expand All @@ -148,11 +151,15 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
f.mkdirs();
}
} else {
fileCount++;

if (!read) {
logger.print("Extracting: ");
logger.print(entry.getName());
logger.print(" -> ");
logger.println(f.getRemote());
if (!quiet) {
logger.print("Extracting: ");
logger.print(entry.getName());
logger.print(" -> ");
logger.println(f.getRemote());
}

/*
It is not by all means required to close the input streams of the zip file because they are
Expand All @@ -165,8 +172,10 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
outputStream.flush();
}
} else {
logger.print("Reading: ");
logger.println(entry.getName());
if (!quiet) {
logger.print("Reading: ");
logger.println(entry.getName());
}

try (InputStream is = zip.getInputStream(entry)) {
strMap.put(entry.getName(), IOUtils.toString(is, Charset.defaultCharset()));
Expand All @@ -175,8 +184,18 @@ public Map<String, String> invoke(File zipFile, VirtualChannel channel) throws I
}
}
if (read) {
if (quiet) {
logger.print("Read: ");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be good to have be printed even if quiet is false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can certainly be done. I'll have a new commit with that momentarily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that change the tests I added will always work, even if run with quiet = false, since they were just looking for that. Is there an assertLogNotContains or anything like that I could use to make the test check that "Extracting: hello.txt ->" is not in the log?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Tests have been updated with that checking.

logger.print(fileCount);
logger.println(" files");
}
return strMap;
} else {
if (quiet) {
logger.print("Extracted: ");
logger.print(fileCount);
logger.println(" files");
}
return null;
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ f.entry(field: 'test', title: _('Test the archive')) {
f.entry(field: 'read', title: _('Read the file contents')) {
f.checkbox()
}


f.entry(field: 'quiet', title: _('Suppress logging of each file')) {
f.checkbox()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
~ The MIT License (MIT)
~
~ Copyright (c) 2016 CloudBees Inc.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->
<p>
Suppress the verbose output that logs every single file that is dealt with.
<em>E.g.</em>
<code>
unzip zipFile: 'example.zip', quiet: true
</code>
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void configRoundTrip() throws Exception {
step.setDir("base/");
step.setGlob("**/*.zip");
step.setRead(true);
step.setQuiet(false);
step.setCharset("");

UnZipStep step2 = new StepConfigTester(j).configRoundTrip(step);
Expand Down Expand Up @@ -222,4 +223,51 @@ public void testZipTestingOkayZip() throws Exception {
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

@Test
public void unzipQuiet() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" dir('zipIt') {\n" +
" writeFile file: 'hello.txt', text: 'Hello World!'\n" +
" writeFile file: 'hello.dat', text: 'Hello World!'\n" +
" dir('two') {\n" +
" writeFile file: 'hello.txt', text: 'Hello World2!'\n" +
" }\n" +
" zip zipFile: '../hello.zip'\n" +
" }\n" +
" dir('unzip') {\n" +
" unzip zipFile: '../hello.zip', quiet: true\n" +
" String txt = readFile 'hello.txt'\n" +
" echo \"Reading: ${txt}\"\n" +
" txt = readFile 'two/hello.txt'\n" +
" echo \"Reading: ${txt}\"\n" +
" }\n" +
"}", true));
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("Extracted: 3 files", run);
j.assertLogContains("Reading: Hello World!", run);
j.assertLogContains("Reading: Hello World2!", run);
}

@Test
public void unzipQuietReading() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" dir('zipIt') {\n" +
" writeFile file: 'hello.txt', text: 'Hello World!'\n" +
" writeFile file: 'hello.dat', text: 'Hello World!'\n" +
" zip zipFile: '../hello.zip'\n" +
" }\n" +
" dir('unzip') {\n" +
" def txt = unzip zipFile: '../hello.zip', quiet: true, read: true\n" +
" echo \"Text: ${txt.values().join('\\n')}\"\n" +
" }\n" +
"}", false)); //For some reason the Sandbox forbids invoking Map.values?
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("Read: 2 files", run);
j.assertLogContains("Text: Hello World!", run);
}
}