Skip to content

Commit

Permalink
[JENKINS-26941] Update to non-deprecated artifact archiving mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
tokou committed Mar 28, 2017
1 parent 8423c82 commit 209e84a
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/main/java/hudson/plugins/android_emulator/AndroidEmulator.java
Expand Up @@ -27,7 +27,9 @@
import hudson.util.ForkOutputStream;
import hudson.util.FormValidation;
import hudson.util.NullStream;
import jenkins.model.ArtifactManager;
import jenkins.security.MasterToSlaveCallable;
import jenkins.util.VirtualFile;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -44,12 +46,12 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -417,7 +419,7 @@ else if (!emulatorAlreadyExists || emuConfig.shouldWipeData() || snapshotState =
}

// Start dumping logcat to temporary file
final File artifactsDir = build.getArtifactsDir();
final ArtifactManager artifactManager = build.getArtifactManager();
final FilePath logcatFile = build.getWorkspace().createTextTempFile("logcat_", ".log", "", false);
final OutputStream logcatStream = logcatFile.write();
final String logcatArgs = String.format("-s %s logcat -v time", emu.serial());
Expand Down Expand Up @@ -487,7 +489,7 @@ else if (!emulatorAlreadyExists || emuConfig.shouldWipeData() || snapshotState =
boolean restarted = emu.sendCommand("avd start");
if (!restarted) {
log(logger, Messages.EMULATOR_RESUME_FAILED());
cleanUp(emuConfig, emu, logWriter, logcatFile, logcatStream, artifactsDir);
cleanUp(emuConfig, emu, logWriter, logcatFile, logcatStream, artifactManager, launcher, listener);
}
} else {
log(logger, Messages.SNAPSHOT_CREATION_FAILED());
Expand Down Expand Up @@ -531,7 +533,7 @@ public void buildEnvVars(Map<String, String> env) {
@SuppressWarnings("rawtypes")
public boolean tearDown(AbstractBuild build, BuildListener listener)
throws IOException, InterruptedException {
cleanUp(emuConfig, emu, logWriter, logcatFile, logcatStream, artifactsDir);
cleanUp(emuConfig, emu, logWriter, logcatFile, logcatStream, artifactManager, launcher, listener);

return true;
}
Expand Down Expand Up @@ -567,7 +569,7 @@ synchronized static void log(final PrintStream logger, String message, boolean i
* @param emu The emulator context
*/
private void cleanUp(EmulatorConfig emulatorConfig, AndroidEmulatorContext emu) throws IOException, InterruptedException {
cleanUp(emulatorConfig, emu, null, null, null, null);
cleanUp(emulatorConfig, emu, null, null, null, null, null, null);
}

/**
Expand All @@ -577,10 +579,14 @@ private void cleanUp(EmulatorConfig emulatorConfig, AndroidEmulatorContext emu)
* @param logcatProcess The adb logcat process.
* @param logcatFile The file the logcat output is being written to.
* @param logcatStream The stream the logcat output is being written to.
* @param artifactsDir The directory where build artifacts should go.
* @param artifactManager The artifact manager. Used to archive the logcatFile.
* @param launcher a launcher used by artifactManager to archive the logcatFile.
* @param listener a listener used by artifactManager to archive the logcatFile.
*/
private void cleanUp(EmulatorConfig emulatorConfig, AndroidEmulatorContext emu, Proc logcatProcess,
FilePath logcatFile, OutputStream logcatStream, File artifactsDir) throws IOException, InterruptedException {
private void cleanUp(EmulatorConfig emulatorConfig, AndroidEmulatorContext emu,
Proc logcatProcess, FilePath logcatFile, OutputStream logcatStream,
ArtifactManager artifactManager, Launcher launcher, BuildListener listener)
throws IOException, InterruptedException {
// FIXME: Sometimes on Windows neither the emulator.exe nor the adb.exe processes die.
// Launcher.kill(EnvVars) does not appear to help either.
// This is (a) inconsistent; (b) very annoying.
Expand Down Expand Up @@ -616,7 +622,12 @@ private void cleanUp(EmulatorConfig emulatorConfig, AndroidEmulatorContext emu,
// Archive the logs
if (logcatFile.length() != 0) {
log(emu.logger(), Messages.ARCHIVING_LOG());
logcatFile.copyTo(new FilePath(artifactsDir).child("logcat.txt"));
if (launcher != null && listener != null) {
final FilePath workspace = logcatFile.getParent();
final String path = logcatFile.getName();
final Map<String, String> artifacts = Collections.singletonMap(path, path);
artifactManager.archive(workspace, launcher, listener, artifacts);
}
}
logcatFile.delete();
}
Expand Down

0 comments on commit 209e84a

Please sign in to comment.