mirror of https://github.com/apache/jclouds.git
Issue 126: findPid on windows now matches on window title. fixed error where script exit quit the calling shell in windows
git-svn-id: http://jclouds.googlecode.com/svn/trunk@2354 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
e62fe3f6e4
commit
4b24b990be
|
@ -55,8 +55,7 @@ public class Call implements Statement {
|
||||||
public String render(OsFamily family) {
|
public String render(OsFamily family) {
|
||||||
StringBuilder args = new StringBuilder();
|
StringBuilder args = new StringBuilder();
|
||||||
for (String arg : this.args) {
|
for (String arg : this.args) {
|
||||||
args.append(String.format(" \"%s\"", Utils.replaceTokens(arg, ShellToken
|
args.append(" ").append(Utils.replaceTokens(arg, ShellToken.tokenValueMap(family)));
|
||||||
.tokenValueMap(family))));
|
|
||||||
}
|
}
|
||||||
StringBuilder call = new StringBuilder();
|
StringBuilder call = new StringBuilder();
|
||||||
call.append(Utils.replaceTokens(OS_TO_CALL.get(family), ImmutableMap.of("function", function,
|
call.append(Utils.replaceTokens(OS_TO_CALL.get(family), ImmutableMap.of("function", function,
|
||||||
|
|
|
@ -149,7 +149,7 @@ public enum ShellToken {
|
||||||
case END_SCRIPT:
|
case END_SCRIPT:
|
||||||
switch (family) {
|
switch (family) {
|
||||||
case WINDOWS:
|
case WINDOWS:
|
||||||
return "exit 0\r\n";
|
return "exit /b 0\r\n";
|
||||||
case UNIX:
|
case UNIX:
|
||||||
return "exit 0\n";
|
return "exit 0\n";
|
||||||
}
|
}
|
||||||
|
@ -170,7 +170,7 @@ public enum ShellToken {
|
||||||
case EXIT:
|
case EXIT:
|
||||||
switch (family) {
|
switch (family) {
|
||||||
case WINDOWS:
|
case WINDOWS:
|
||||||
return "exit";
|
return "exit /b";
|
||||||
case UNIX:
|
case UNIX:
|
||||||
return "exit";
|
return "exit";
|
||||||
}
|
}
|
||||||
|
|
|
@ -215,8 +215,8 @@ public class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Map<OsFamily, String> OS_TO_ZERO_PATH = ImmutableMap.of(OsFamily.WINDOWS,
|
public static final Map<OsFamily, String> OS_TO_ZERO_PATH = ImmutableMap.of(OsFamily.WINDOWS,
|
||||||
"set PATH=c:\\windows\\;C:\\windows\\system32\r\n", OsFamily.UNIX,
|
"set PATH=c:\\windows\\;C:\\windows\\system32;c:\\windows\\system32\\wbem\r\n",
|
||||||
"export PATH=/usr/ucb/bin:/bin:/usr/bin:/usr/sbin\n");
|
OsFamily.UNIX, "export PATH=/usr/ucb/bin:/bin:/usr/bin:/usr/sbin\n");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return line used to zero out the path of the script such that basic commands such as unix ps
|
* @return line used to zero out the path of the script such that basic commands such as unix ps
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
:abort
|
:abort
|
||||||
echo aborting: %EXCEPTION%
|
echo aborting: %EXCEPTION%
|
||||||
exit 1
|
exit /b 1
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
:findPid
|
:findPid
|
||||||
set FOUND_PID=
|
set FOUND_PID=
|
||||||
SETLOCAL
|
|
||||||
set _pid=
|
|
||||||
set _expression=%1
|
set _expression=%1
|
||||||
shift
|
shift
|
||||||
set FIND_PROCESS=wmic process where (CommandLine like "%_expression%%%") get ProcessId
|
set FIND_PROCESS=TASKLIST /FI "WINDOWTITLE eq %_expression%" /NH
|
||||||
for /f "usebackq skip=1" %%a in (`cmd /c "%FIND_PROCESS% 2>NUL"`) do (
|
FOR /F "usebackq tokens=2 delims= " %%A IN (`cmd /c "%FIND_PROCESS% 2>NUL"`) DO (
|
||||||
if not defined _proc (
|
SET FOUND_PID=%%A
|
||||||
set _pid=%%a
|
)
|
||||||
goto :done
|
if defined FOUND_PID (
|
||||||
)
|
exit /b 0
|
||||||
|
) else (
|
||||||
|
set EXCEPTION=%_expression% not found
|
||||||
|
exit /b 1
|
||||||
)
|
)
|
||||||
:done
|
|
||||||
ENDLOCAL&SET FOUND_PID=%_pid%
|
|
||||||
exit /b 0
|
|
||||||
|
|
|
@ -45,12 +45,12 @@ public class CallTest {
|
||||||
|
|
||||||
public void testCallArgsUNIX() {
|
public void testCallArgsUNIX() {
|
||||||
Call call = new Call("help", "me", "rhonda");
|
Call call = new Call("help", "me", "rhonda");
|
||||||
assertEquals(call.render(OsFamily.UNIX), "help \"me\" \"rhonda\" || return 1\n");
|
assertEquals(call.render(OsFamily.UNIX), "help me rhonda || return 1\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCallArgsWINDOWS() {
|
public void testCallArgsWINDOWS() {
|
||||||
Call call = new Call("help", "me", "rhonda");
|
Call call = new Call("help", "me", "rhonda");
|
||||||
assertEquals(call.render(OsFamily.WINDOWS),
|
assertEquals(call.render(OsFamily.WINDOWS),
|
||||||
"call :help \"me\" \"rhonda\"\r\nif errorlevel 1 goto abort\r\n");
|
"call :help me rhonda\r\nif errorlevel 1 goto abort\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,11 +55,11 @@ public class ShellTokenTest {
|
||||||
public void testTokenValueMapWindows() {
|
public void testTokenValueMapWindows() {
|
||||||
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "\\")
|
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "\\")
|
||||||
.put("ps", ";").put("lf", "\r\n").put("sh", "cmd").put("source", "@call").put("rem",
|
.put("ps", ";").put("lf", "\r\n").put("sh", "cmd").put("source", "@call").put("rem",
|
||||||
"@rem").put("args", "%*").put("varl", "%").put("exit", "exit").put("varr",
|
"@rem").put("args", "%*").put("varl", "%").put("exit", "exit /b").put("varr",
|
||||||
"%").put("libraryPathVariable", "PATH").put("return", "exit /b").put("vq",
|
"%").put("libraryPathVariable", "PATH").put("return", "exit /b").put("vq",
|
||||||
"").put("beginFunctions", "GOTO FUNCTION_END\r\n").put("endFunctions",
|
"").put("beginFunctions", "GOTO FUNCTION_END\r\n").put("endFunctions",
|
||||||
":FUNCTION_END\r\n").put("beginScript", "@echo off\r\n").put("endScript",
|
":FUNCTION_END\r\n").put("beginScript", "@echo off\r\n").put("endScript",
|
||||||
"exit 0\r\n").put("fncl", ":").put("fncr", "\r\n").put("fnce",
|
"exit /b 0\r\n").put("fncl", ":").put("fncr", "\r\n").put("fnce",
|
||||||
" exit /b 0\r\n").put("export", "set").build();
|
" exit /b 0\r\n").put("export", "set").build();
|
||||||
|
|
||||||
assertEquals(ShellToken.tokenValueMap(OsFamily.WINDOWS), expected);
|
assertEquals(ShellToken.tokenValueMap(OsFamily.WINDOWS), expected);
|
||||||
|
|
|
@ -23,6 +23,6 @@ function findPid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export PATH=/usr/ucb/bin:/bin:/usr/bin:/usr/sbin
|
export PATH=/usr/ucb/bin:/bin:/usr/bin:/usr/sbin
|
||||||
findPid "$@" || exit 1
|
findPid $@ || exit 1
|
||||||
echo $FOUND_PID
|
echo $FOUND_PID
|
||||||
exit 0
|
exit 0
|
||||||
|
|
|
@ -5,26 +5,24 @@ set PATH=
|
||||||
GOTO FUNCTION_END
|
GOTO FUNCTION_END
|
||||||
:abort
|
:abort
|
||||||
echo aborting: %EXCEPTION%
|
echo aborting: %EXCEPTION%
|
||||||
exit 1
|
exit /b 1
|
||||||
:findPid
|
:findPid
|
||||||
set FOUND_PID=
|
set FOUND_PID=
|
||||||
SETLOCAL
|
|
||||||
set _pid=
|
|
||||||
set _expression=%1
|
set _expression=%1
|
||||||
shift
|
shift
|
||||||
set FIND_PROCESS=wmic process where (CommandLine like "%_expression%%%") get ProcessId
|
set FIND_PROCESS=TASKLIST /FI "WINDOWTITLE eq %_expression%" /NH
|
||||||
for /f "usebackq skip=1" %%a in (`cmd /c "%FIND_PROCESS% 2>NUL"`) do (
|
FOR /F "usebackq tokens=2 delims= " %%A IN (`cmd /c "%FIND_PROCESS% 2>NUL"`) DO (
|
||||||
if not defined _proc (
|
SET FOUND_PID=%%A
|
||||||
set _pid=%%a
|
)
|
||||||
goto :done
|
if defined FOUND_PID (
|
||||||
)
|
exit /b 0
|
||||||
|
) else (
|
||||||
|
set EXCEPTION=%_expression% not found
|
||||||
|
exit /b 1
|
||||||
)
|
)
|
||||||
:done
|
|
||||||
ENDLOCAL&SET FOUND_PID=%_pid%
|
|
||||||
exit /b 0
|
|
||||||
:FUNCTION_END
|
:FUNCTION_END
|
||||||
set PATH=c:\windows\;C:\windows\system32
|
set PATH=c:\windows\;C:\windows\system32;c:\windows\system32\wbem
|
||||||
call :findPid "%*"
|
call :findPid %*
|
||||||
if errorlevel 1 goto abort
|
if errorlevel 1 goto abort
|
||||||
echo %FOUND_PID%
|
echo %FOUND_PID%
|
||||||
exit 0
|
exit /b 0
|
||||||
|
|
|
@ -5,12 +5,12 @@ set PATH=
|
||||||
GOTO FUNCTION_END
|
GOTO FUNCTION_END
|
||||||
:abort
|
:abort
|
||||||
echo aborting: %EXCEPTION%
|
echo aborting: %EXCEPTION%
|
||||||
exit 1
|
exit /b 1
|
||||||
:default
|
:default
|
||||||
set JAVA_HOME=/apps/jdk1.6
|
set JAVA_HOME=/apps/jdk1.6
|
||||||
exit /b 0
|
exit /b 0
|
||||||
:FUNCTION_END
|
:FUNCTION_END
|
||||||
set PATH=c:\windows\;C:\windows\system32
|
set PATH=c:\windows\;C:\windows\system32;c:\windows\system32\wbem
|
||||||
goto CASE%1
|
goto CASE%1
|
||||||
:CASE_start
|
:CASE_start
|
||||||
echo started
|
echo started
|
||||||
|
@ -19,4 +19,4 @@ goto CASE%1
|
||||||
echo stopped
|
echo stopped
|
||||||
GOTO END_SWITCH
|
GOTO END_SWITCH
|
||||||
:END_SWITCH
|
:END_SWITCH
|
||||||
exit 0
|
exit /b 0
|
||||||
|
|
Loading…
Reference in New Issue