From 3d8c6065c59297a1ef93370343d2408ad59fca3c Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Mon, 9 Jul 2012 15:30:35 +0400 Subject: [PATCH] Android module: stop ddms process for unix systems --- .../jetbrains/jet/compiler/OutputUtils.java | 15 ++++++- .../jet/compiler/emulator/Emulator.java | 41 ++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java b/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java index 470f8e7b79c..761f9d9d9ce 100644 --- a/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java +++ b/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java @@ -18,6 +18,7 @@ package org.jetbrains.jet.compiler; import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.openapi.util.SystemInfo; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.compiler.run.RunUtils; import org.jetbrains.jet.compiler.run.result.RunResult; @@ -31,6 +32,7 @@ import java.util.regex.Pattern; public class OutputUtils { private final static Pattern EMULATOR_PATTERN = Pattern.compile("emulator-([0-9])*"); + private final static Pattern EMULATOR_PROCESS_PATTERN = Pattern.compile("\\w*[\\s]+([0-9]*) .* java .* emulator .*"); public static boolean isResultOk(String output) { return !(output.contains("BUILD FAILED") || output.contains("Build failed")); @@ -49,7 +51,7 @@ public class OutputUtils { commandLineForListOfDevices.addParameter("devices"); RunResult runResult = RunUtils.execute(commandLineForListOfDevices); checkResult(runResult); - + Matcher matcher = EMULATOR_PATTERN.matcher(runResult.getOutput()); while (matcher.find()) { System.out.println("Stopping redundant emulator..."); @@ -73,6 +75,17 @@ public class OutputUtils { } checkResult(RunUtils.execute(commandLineForListOfDevices)); } + + @Nullable + public static String getPidFromPsCommand(String output) { + if (!output.isEmpty()) { + Matcher matcher = EMULATOR_PROCESS_PATTERN.matcher(output); + if (matcher.find()) { + return matcher.group(1); + } + } + return null; + } private OutputUtils() { } diff --git a/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java b/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java index 139e2dd8913..4119d06928f 100644 --- a/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java +++ b/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java @@ -28,7 +28,7 @@ import org.jetbrains.jet.compiler.run.result.RunResult; */ public class Emulator { - + private final PathManager pathManager; public Emulator(PathManager pathManager) { @@ -69,7 +69,7 @@ public class Emulator { commandLine.addParameter("wait-for-device"); return commandLine; } - + private GeneralCommandLine getStopCommandForAdb() { GeneralCommandLine commandLine = new GeneralCommandLine(); String adbCmdName = SystemInfo.isWindows ? "adb.exe" : "adb"; @@ -117,5 +117,42 @@ public class Emulator { OutputUtils.checkResult(RunUtils.execute(getStopCommand())); System.out.println("Stopping adb..."); OutputUtils.checkResult(RunUtils.execute(getStopCommandForAdb())); + if (SystemInfo.isUnix) { + finishProcess("emulator-arm"); + finishProcess("adb"); + stopDdmsProcess(); + } + } + + //Only for Unix + private void stopDdmsProcess() { + GeneralCommandLine listOfEmulatorProcess = new GeneralCommandLine(); + listOfEmulatorProcess.setExePath("sh"); + listOfEmulatorProcess.addParameter("-c"); + listOfEmulatorProcess.addParameter("ps aux | grep emulator"); + RunResult runResult = RunUtils.execute(listOfEmulatorProcess); + OutputUtils.checkResult(runResult); + String pidFromPsCommand = OutputUtils.getPidFromPsCommand(runResult.getOutput()); + if (pidFromPsCommand != null) { + GeneralCommandLine killCommand = new GeneralCommandLine(); + killCommand.setExePath("kill"); + killCommand.addParameter(pidFromPsCommand); + OutputUtils.checkResult(RunUtils.execute(killCommand)); + } + } + + //Only for Unix + private void finishProcess(String processName) { + GeneralCommandLine pidOfProcess = new GeneralCommandLine(); + pidOfProcess.setExePath("pidof"); + pidOfProcess.addParameter(processName); + RunResult runResult = RunUtils.execute(pidOfProcess); + String pid = runResult.getOutput().substring(("pidof " + processName).length()); + if (pid.length() > 1) { + GeneralCommandLine killCommand = new GeneralCommandLine(); + killCommand.setExePath("kill"); + killCommand.addParameter(pid); + OutputUtils.checkResult(RunUtils.execute(killCommand)); + } } }