Why does Groovy execute() hang? - scala

I have the following Groovy script:
#!/opt/groovy-1.8.6/bin/groovy
final env = null // []
final command = ["./setter-for-catan.scala"]
final process = command.execute(env, null)
println (['echo', '********************** 0'].execute(env, null).text)
final stdout = process.inputStream
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout))
while ((line = reader.readLine()) != null) {
System.out.println ("Stdout: " + line);
}
and the following Scala script:
#!/bin/bash
export SCALA_HOME=/opt/scala-2.10.1
echo '********************* 1' "$0" "$#"
${SCALA_HOME}/bin/scala -version 2>&1
exec ${SCALA_HOME}/bin/scala "$0" "$#" 2>&1
!#
println("******************* 2")
Calling the Groovy script outputs:
********************** 0
Stdout: ********************* 1 ./setter-for-catan.scala
Stdout: Scala code runner version 2.10.1 -- Copyright 2002-2013, LAMP/EPFL
Stdout: ******************* 2
If env is defined as [], the Groovy script hangs with the following output:
********************** 0
Stdout: ********************* 1 ./setter-for-catan.scala
Stdout: Scala code runner version 2.10.1 -- Copyright 2002-2013, LAMP/EPFL
What's going on and what needs to be done so that execute() doesn't hang when env is an Array?

JAVA_HOME isn't being inherited by the Scala script so it needs to be defined.
One way to do it would be in the Scala script:
#!/bin/bash
export JAVA_HOME=/Library/Java/Home
export SCALA_HOME=/opt/scala-2.10.1
echo '********************* 1' "$0" "$#"
${SCALA_HOME}/bin/scala -version 2>&1
exec ${SCALA_HOME}/bin/scala "$0" "$#" 2>&1
!#
Another way would be to do it in the Groovy script:
final env = ['JAVA_HOME=/Library/Java/Home']

Related

How can I set makefile variables (CC, CFLAGS etc) to terminal or cmd using powershell scripts?

I want to write platform independent Makefile's environment variable setup script which can set Makefile's environment variable like CC, CFLAGS, LDFLAGS, LD etc. This is my make file. I want to run it on Window or Linux as per the user need. So instead of setting CC, CFLAGS, LDFLAGS, LD every time, I want to write a script which can set the variable for user depending on which platform they are using.
LDFLAG=-L..\..\test\lib
LIBS=-ltestlibs
INCLUDES = ..\..\test\inc
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
EXECUTABLE = targetImage.exe
.PHONY: clean
all: myprog
myprog: $(OBJS)
$(CC) $(OBJS) $(LDFLAG) $(LIBS) -o $(EXECUTABLE)
$(OBJS): $(SRCS) $(INCLUDES)
$(CC) $(CFLAGS) -I$(INCLUDES) -c $(SRCS)
clean:
$(RM) $(OBJS) $(EXECUTABLE)
I know the .sh script but it can be run on CYGWIN or MINGW.
#!/bin/sh
echo "Finding the current OS type"
echo
osType="$(uname -s)"
#osType=$1
case "${osType}" in
"CYGWIN")
{
echo "Running on CYGWIN."
CURRENT_OS=CYGWIN
export CC=""
} ;;
Linux*)
{
echo "Running on Linux."
CURRENT_OS=Linux
export CC="-g -Wall"
} ;;
"MINGW")
{
echo "Running on MINGW."
CURRENT_OS=MINGW
} ;;
"Thor96")
{
source /opt/fsl-imx-xwayland/4.14-sumo/environment-setup-aarch64-poky-linux
CURRENT_OS=THOR96
};;
*)
{
echo "Unsupported OS:${osType}, exiting"
exit
} ;;
esac
echo ${CURRENT_OS}
But it can run on linux only. So how can I achieve the same using powershell .ps1 scripts ? So same script can work on any platform.
I have drafted this .ps1 file for reference but need to update it as I am not sure it's correct or not. Please guide me with proper solution. I am not able to find the proper solution for this.
Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
$Env:CC = "gcc"
}
Function RunOn-Linux
{
Write-Host 'The Script is Running on a Linux Machine'
$Env:CC = "gcc"
}
Function RunOn-Mac
{
Write-Host 'The Script is Running on a Mac'
}
Function RunOn-Other
{
Write-Host 'The Script is Running on a Other'
$Env:CC = "aarch64-poky-linux-gcc"
}
If ($IsWindows)
{RunOn-Windows}
elseif ($IsLinux)
{RunOn-Linux}
elseif ($IsMacOS)
{RunOn-Mac}
else
{RunOn-Other}
I don't have a Windows box to do tests but if yours has GNU make available there is a chance that you can do what you want from inside your Makefile:
# List supported OS (as returned by uname -s, case sensitive)
SUPPORTED_OS := CYGWIN Linux MINGW Darwin
# Get OS
OS := $(shell uname -s)
# Check if OS is supported
ifneq ($(filter-out $(SUPPORTED_OS),$(OS)),)
$(error Unsupported OS: $(OS))
endif
# Define all OS-dependent make variables as OS_VARIABLE
# Linux:
Linux_CC := gcc
Linux_CFLAGS := -g -Wall
Linux_LDFLAGS := ...
# CIGWIN:
CYGWIN_CC := ...
CYGWIN_CFLAGS := ...
CYGWIN_LDFLAGS := ...
...
# Assign make variables
CC := $($(OS)_CC)
CFLAGS := $($(OS)_CFLAGS)
LDFLAGS := $($(OS)_LDFLAGS)

shell "No such file or directory" on variable execution result

some basic function with goal to extract xml var by xpath:
function get_xml_value_from_config_dir {
local src_root=$1
local xpath_expr="//$2/text()"
local path_to_local="$src_root/app/etc/local.xml"
if [ ! -f $path_to_local ]; then echo "Config file not found: $path_to_local"; exit; fi;
echo $("$xmllint --nocdata --xpath '$xpath_expr' $path_to_local")
}
## and then
src_usr=$(get_xml_value_from_config_dir $src_dir username)
gives me
line 34: /usr/bin/xmllint --nocdata --xpath '//username/text()' /tmp/bin/app/etc/local.xml: No such file or directory
why? ( /usr/bin/xmllint exist as well as /tmp/bin/app/etc/local.xml )
It's telling you it can't find the file or directory named
/usr/bin/xmllint --nocdata --xpath '//username/text()' /tmp/bin/app/etc/local.xml
which indeed is unlikely to exist on your system.
Replace
echo $("$xmllint --nocdata --xpath '$xpath_expr' $path_to_local")
with
echo $($xmllint --nocdata --xpath "$xpath_expr" $path_to_local)
Incidentally, that will put all xmllint output on a single line; to avoid that, just use
xmllint --nocdata --xpath "$xpath_expr" $path_to_local

Getting error in hack/update-all.sh script

I have been trying to run ./hack/update-all.sh script and I am getting this error while updating codegen:
$ ./hack/update-all.sh
Running in the silent mode, run with -v if you want to see script logs.
Running in short-circuit mode; run with -a to force all scripts to run.
Updating generated-protobuf
Updating codegen
# runtime
/usr/local/go/src/runtime/os2_linux_generic.go:12: _SS_DISABLE redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:8
/usr/local/go/src/runtime/os2_linux_generic.go:13: _NSIG redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:9
/usr/local/go/src/runtime/os2_linux_generic.go:14: _SI_USER redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:10
/usr/local/go/src/runtime/os2_linux_generic.go:15: _SIG_BLOCK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:11
/usr/local/go/src/runtime/os2_linux_generic.go:16: _SIG_UNBLOCK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:12
/usr/local/go/src/runtime/os2_linux_generic.go:17: _SIG_SETMASK redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:13
/usr/local/go/src/runtime/os2_linux_generic.go:18: _RLIMIT_AS redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:14
/usr/local/go/src/runtime/os2_linux_generic.go:24: sigset redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:20
/usr/local/go/src/runtime/os2_linux_generic.go:26: rlimit redeclared in this block
previous declaration at /usr/local/go/src/runtime/os2_linux.go:22
/usr/local/go/src/runtime/panic1.go:11: paniclk redeclared in this block
previous declaration at /usr/local/go/src/runtime/panic.go:552
/usr/local/go/src/runtime/panic1.go:11: too many errors
!!! Error in /home/peeyush/work/kubernetes/hack/lib/golang.sh:435
'go install "${goflags[#]:+${goflags[#]}}" -ldflags "${goldflags}" "${nonstatics[#]:+${nonstatics[#]}}"' exited with status 2
Call stack:
1: /home/peeyush/work/kubernetes/hack/lib/golang.sh:435 kube::golang::build_binaries_for_platform(...)
2: /home/peeyush/work/kubernetes/hack/lib/golang.sh:574 kube::golang::build_binaries(...)
3: /home/peeyush/work/kubernetes/hack/build-go.sh:26 main(...)
Exiting with status 1
!!! Error in /home/peeyush/work/kubernetes/hack/lib/golang.sh:494
'( kube::golang::setup_env; echo "Go version: $(go version)"; local host_platform; host_platform=$(kube::golang::host_platform); local goflags goldflags; eval "goflags=(${KUBE_GOFLAGS:-})"; goldflags="${KUBE_GOLDFLAGS:-} $(kube::version::ldflags)"; local use_go_build; local -a targets=(); local arg; for arg in "$#";
do
if [[ "${arg}" == "--use_go_build" ]]; then
use_go_build=true;
else
if [[ "${arg}" == -* ]]; then
goflags+=("${arg}");
else
targets+=("${arg}");
fi;
fi;
done; if [[ ${#targets[#]} -eq 0 ]]; then
targets=("${KUBE_ALL_TARGETS[#]}");
fi; local -a platforms=("${KUBE_BUILD_PLATFORMS[#]:+${KUBE_BUILD_PLATFORMS[#]}}"); if [[ ${#platforms[#]} -eq 0 ]]; then
platforms=("${host_platform}");
fi; local binaries; binaries=($(kube::golang::binaries_from_targets "${targets[#]}")); local parallel=false; if [[ ${#platforms[#]} -gt 1 ]]; then
local gigs; gigs=$(kube::golang::get_physmem); if [[ ${gigs} -ge ${KUBE_PARALLEL_BUILD_MEMORY} ]]; then
kube::log::status "Multiple platforms requested and available ${gigs}G >= threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in parallel"; parallel=true;
else
kube::log::status "Multiple platforms requested, but available ${gigs}G < threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in serial"; parallel=false;
fi;
fi; if [[ "${parallel}" == "true" ]]; then
kube::log::status "Building go targets for ${platforms[#]} in parallel (output will appear in a burst when complete):" "${targets[#]}"; local platform; for platform in "${platforms[#]}";
do
( kube::golang::set_platform_envs "${platform}"; kube::log::status "${platform}: go build started"; kube::golang::build_binaries_for_platform ${platform} ${use_go_build:-}; kube::log::status "${platform}: go build finished" ) &> "/tmp//${platform//\//_}.build" &
done; local fails=0; for job in $(jobs -p);
do
wait ${job} || let "fails+=1";
done; for platform in "${platforms[#]}";
do
cat "/tmp//${platform//\//_}.build";
done; exit ${fails};
else
for platform in "${platforms[#]}";
do
kube::log::status "Building go targets for ${platform}:" "${targets[#]}"; kube::golang::set_platform_envs "${platform}"; kube::golang::build_binaries_for_platform ${platform} ${use_go_build:-};
done;
fi )' exited with status 1
Call stack:
1: /home/peeyush/work/kubernetes/hack/lib/golang.sh:494 kube::golang::build_binaries(...)
2: /home/peeyush/work/kubernetes/hack/build-go.sh:26 main(...)
Exiting with status 1
!!! Error in ./hack/../hack/update-codegen.sh:32
'"${KUBE_ROOT}/hack/build-go.sh" ${BUILD_TARGETS[*]}' exited with status 1
Call stack:
1: ./hack/../hack/update-codegen.sh:32 main(...)
Exiting with status 1
Updating codegen FAILED
Any idea what could be the reason behind this? Or how to resolve this issue?
Looks like an environment issue. Cleared everything, cloned the repo afresh and everything is working fine.

Running a process as sudo in Scala

I am trying to run a process as sudo in scala. I have written this code
val l : Seq[String] = Seq("echo", "SecretXYZ!", "|", "sudo", "-S", "-u", "web", "spark-submit", "--class",
"com.abhi.Foo", "--master", "yarn-cluster", "Foo-assembly-1.0.jar", DateTimeFormat.forPattern(pattern).print(date), ">",
"fn_output.txt", "2>", "fn_error.txt")
l.!
println("completed...")
but when I run this, it doesn't run the process. it just prints
SecretXYZ! | sudo -S -u web spark-submit --class com.abhi.Foo --master yarn-cluster Foo-assembly-1.0.jar 2015-03-19 > fn_output.txt 2> fn_error.txt
completed...
As Ɓukasz has pointed out, the "right" answer is to build the pipeline yourself with sys.process.
The lazy answer is to explicitly wrap everything in a call to bash -c ...:
val miniScript: Seq[String] = Seq(
"echo", "SecretXYZ!",
"|", "sudo", "-S", "-u", "web",
"spark-submit", "--class", "com.abhi.Foo", "--master", "yarn-cluster",
"Foo-assembly-1.0.jar", DateTimeFormat.forPattern(pattern).print(date),
">", "fn_output.txt", "2>", "fn_error.txt")
val cmd: Seq[String] = Seq("bash", "-c", miniScript.mkString(" "))
cmd.!
Be careful with escaping, though -- your password in this version would need single-quotes around it, for example -- and really, if you want this code to be robust, you should really do it with sys.process so you know exactly what's happening.

How to execute a .bat file from psake?

I am trying in a powershell psake script to execute a .bat file. Is this possible? Or do I have to do a workaround?
Try the following:
task CallBatch {
exec {cmd.exe /c "path\to\my\testscript.bat"}
}
It is not necessary to wrap the call to cmd.exe in PSake's exec {} function, but if you do it, the build fails if the batch returns anything but 0.
The task below always lets the build fail:
task Return1FromCmd {
exec {cmd.exe /c "#exit 1"}
}
To execute a .bat (or .cmd) from PowerShell:
foo.bat:
#echo off
echo "foo"
foo.ps1:
. .\foo.bat
#or
.\foo.bat
#or
& .\foo.bat
we can then run the script:
D:\dev> .\foo.ps1
"foo"
This works for me:
properties {
$mybat = 'C:\path\tool.bat'
}
task Test -depends ... {
"Bla bla"
Exec { & $mybat }
}
No need to directly mention cmd.exe -- using & in the Exec script block seems to be enough.