Jenkins Pipeline - Active Choice Reactive Parameter - groovy

The following script reads all file names from a directory and displays them to the user as parameters (without extension)
import groovy.io.FileType
def list = []
def dir = new File("/var/lib/jenkins/workspace/grookins/folderx")
dir.eachFileRecurse (FileType.FILES) { file ->
list.add(file.getName().split("\\.", 2)[0])
}
return list
It works fine if I paste the code into the Groovy Script area in the UI right here
Now if I paste the script into a Jenkinsfile,
for some reason, the backslashes are turning into a simple backslash in the UI after running the script from scm
and the parameters are gone
Can someone help me to find the reason for this behavior?

By try and error i found out that the regex in the split function is interpreted differently in the UI than in the Jenkinsfile. In the Jenkinsfile 4 backslashes are required to escape the dot in the UI.
This is how the entry in the Jenkinsfile looks like:
list.add(file.getName().split("\\\\.", 2)[0])

Related

Snakemake: Parameter as wildcard used in parallel script runs

I'm fairly new to snakemake and inherited a kind of huge worflow that consists in a sequence of 17 rules that run in serial.
Each rule takes outputs from the previous rules and uses them to run a python script. Everything has worked great so far except that now I'm trying to improve the worflow since some of the rules can be run in parallel.
A rough example of what I'm trying to achieve, my understanding is that wildcards should allow me to solve this.
grid = [ 10 , 20 ]
rule all:
input:
expand("path/to/C/{grid}/file_C" ,grid = grid)
rule process_A:
input:
path_A = "path/to/A/file_A"
path_B = "path/to/B/{grid}/file_B" # A rule further in the worflow could need a file from a previous rule saved with this structure
params:
grid = lambda wc: wc.get(grid)
output:
path_C = "path/to/C/{grid}/file_C"
script:
"script_A.py"
And inside the script I retrieve the grid size parameter:
grid = snakemake.params.grid
In the end the whole rule process_A should be rerun with grid = 10 and with grid = 20 and save each result to a folder whose path depends on grid also.
I know there are several things wrong with this, but I can't seem to find were to start from to figure this out. The error I'm getting now is:
name 'params' is not defined
Any help as to where to start from?
It would be useful to post the error stack trace of name 'params' is not defined to know exactly what is causing it. For now...
And inside the script I retrieve the grid size parameter:
grid = snakemake.params.grid
I suspect you are mixing the script directive with the shell directive. Probably you want something like:
rule process_A:
input: ...
output: ...
params: ...
script:
"script_A.py"
inside script_A.py snakemake will replace snakemake.params.grid with the actual param value.
Alternatively, write a standalone python script that parses command line arguments and you execute like any other program using the shell directive. (I tend to prefer this solution as it makes things more explicit and easier to debug but it also means more boiler-plate code to write a standalone script).

In Freeplane Scripts, how to deal with special characters in node links to operating system commands

I'm trying to create a link to a shell script in a Node in Freeplane.
When I monitor the text in the node node.link.text I get execute_:script.sh%20/home/dir/file.ext
Ok, It works, but when my path has special characters link spaces or ( ), it isn't able to open. I already tried to use URLEncoder from java.net:
filePath='/home/user/Books/Author Name/File (231)/Book - Author.pdf'
urlEncoder = new URLEncoder()
def urlEncode(String s) {
urlEncoder.encode(s).replace("+", "%20");
}
fileLink = 'execute:_docreader.sh%20-p%20' + page + '%20' + urlEncode(filePath)
createdNode.link.setText(fileLink)
But I couldn't execute any command with files as arguments whose path have special characters. I even tried to put ' or " in the path manually but it didn't work too.
If filePath are without special characters, like /home/user/Books/AuthorName/file.pdf it works fine.
I look into Freeplane wiki and Freeplane API, looked into the examples but havent found any clue about that.

Return a String from a Windows Batch file

I want to find the target branch when a pull request is submitted on GitHub, in my Jenkins pipeline. To achieve this I am doing the following:
I am invoking a windows batch file from my Jenkinsfile, which in turn invokes a nodejs script. This script internally invokes GitHub APIs to get the target branch which is to be set on some variable in Jenkinsfile(code snippet given below):
Jenkinsfile
env.TARGET_BRANCH = bat "GetTargetBranchFromGit.bat ${env.BRANCH_NAME}"
BatchFile:
node getTargetBranchForPR.js %1
But unfortunately, the variable env.TARGET_BRANCH is not getting set to the target branch even though the nodejs script gets the right value. I am in fact not able to return the value from the batch file. Could someone please help me here?
#npocmaka mention is the right way: How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?
Accodring to Jenkins' documentation.
returnStdout (optional) If checked, standard output from the task is
returned as the step value as a String, rather than being printed to
the build log. (Standard error, if any, will still be printed to the
log.) You will often want to call .trim() on the result to strip off a
trailing newline.
So your code should look like
env.TARGET_BRANCH = bat( script: "GetTargetBranchFromGit.bat ${env.BRANCH_NAME}",
returnStdout: true
).trim()
If you get back more than expected you probably need to parse it.

How to run a string from an input file as python code?

I am creating something along the likes of a text adventure game. I have a .yaml file that is my input. This file looks something like this
node_type:
action
title:
Do some stuff
info:
This does some stuff and things
script:
'print("hello world")
print(ret_val)
foo.bar(True)
ret_val = (foo.bar() == True)
if (thing):
print(thing)
print(ret_val)
'
My end goal is to have my python program run the script portion of the yaml file exactly as if it had been copy pasted into the main code. (I know there are about ten bazillion security reasons I should not be running user input like this, but I am the only one writing these nodes, and the only one using this program so I'm mostly just ignoring this fact...)
Currently my attempt goes like this: I load my yaml file as a dict using pyyaml
node = yaml.safe_load(file.yaml)
Then I'm trying to use exec to run my code and hitting a lot of problems, I can't run if statements, I simply get a syntax error, and I can't get any sort of return value from my code. I've tried this as a work around:
def main()
ret_val = "test";
thing = exec(node['script'], globals(),locals())
print(ret_val)
which when run with the above .yaml file prints
>> hello world
>> test
>> True
>> test
for some reason not actually modifying any of my main variables even though I fed them to exec.
Is there any way for me to work around these issues or is there an all together better way to be doing this?
One way of doing this would be to parse the code out and save it to a .py file, from which it can be imported dynamically, for example by importlib.
You might want to encapsulate parsed code into a function, which you can then easily call to invoke your action. Also, it would make sense to specify some default imports there.

String Expansion with Variables

I have a config file, that contains strings used within my scripts. The config file is read via a C# class using this Syntax:
$final = $PropMgr.GetValue($section, $property)
$final = $ExecutionContext.InvokeCommand.ExpandString($final)
This returns the string from the config file. Afterwards the returned string gets expanded because it may contain objects that need expansion
Everything works fine but for one thing. One of my strings in the config file looks like this:
Found $($scripts.Length) scripts to execute in the config file
$scripts is array, so the expanded string should look like this:
Found 6 scripts to execute in the config file
But using my code avove I receive an exception:
Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an object."
If I debug the script and execute this code in a commandline:
$ExecutionContext.InvokeCommand.ExpandString("Found $($scripts.Length) scripts to execute in the config file")
Everything is fine, but using the $final variable instead of the string itself, I receive the exception again.
What do I have to do to achieve what I was looking for?

Resources