How to retain backslash in apache ant replaceregexp task? - ant

I have a property in application.properties as below
application.generalsettings.environmentname.blacklist=value_to_replace
And I have a task in build.xml as below
<replaceregexp file="application.properties" match="value_to_replace" replace="\"" byline="true"/>
However it giving me output like below:
application.generalsettings.environmentname.blacklist="
Instead of
application.generalsettings.environmentname.blacklist=\"
So is there any flag in replaceregexp to retain backslash as shown in above output ?

(I get XML parsing errors with your task as-is.)
I think this should work for you:
replace="\\\\""
At the moment your backslash is escaping the double quote after it.
You need to escape the backslash "twice", once so the XML can be parsed correctly, the second time so the substitution works, hence the 4 successive slashes above. " is the XML entity for a double quote.
I'm not sure why you XML parser is happy to parse "\"", seems very forgiving!

Related

Are backslashes supported in Groovy comments?

I experienced a weird Jenkins error while trying to run a groovy-scripted pipeline that contained backslashes inside comments.
Here is the offending code:
// The script below depends on:
// * The presence of a recent nuget.exe in c:\tools (v5.7)
// * The presence of the xxx plugin installed for user foo\username
I'm aware the backslash is used in many languages to start an escape sequence, but I had never before experienced backslashes being an issue inside comments.
I wonder if this backslash in comments behavior is due to Groovy itself or rather a bug in the way Jenkins interprets it...
Edit: I solved my issue by replacing the offending \ with \\ but then noticed I had forgotten to double the backslash in c:\tools, and still Jenkins did not complain. It seems Jenkins (or Groovy) tried to interpret the \u in foo\username as introducing an hex character code... but was okay with \t for it probably was interpreted as a tab!

Revit Ironpython Shell - Parsing a list of filenames with a number after a backslash in the path

I want to read in a list of files (inc path) from either a spreadsheet or a text file for some downstream processing. The list has been generated as a log from another process and the path includes a 2 digit year folder followed by a project number folder as follows:
\\servername\projects\19\1901001\project files\filetobeprocessed.abc
The problem is as soon as the above string is read in, it is interpreted as
\\servername\\projects\x019\x01901001\\project files\x0ciletobeprocessed.abc
Which then means that I cannot use the path to access the file.
Assigning the path string to a variable, I have tried:
thePath = repr(pathreadfromfile)
After assigning the path string I have tried fixing the string using
thePath.replace('\x0','\\')
thePath.replace('\\x0','\\')
thePath.replace(r'\x0','\\')
Nothing seems to fix the path so that it can be used to open the file.
I can't find anything in either python or Ironpython that suggests a fix for this programatically. I know that you can fix this is the path is known within the code by using r'' to use raw text to create the path.
Any help appreciated
Obviously, the backslash \ is interpreted as an escape character.
For a really simple solution, hopefully the simplest, I would suggest using forward slash / for all your path separators instead of backslash.
If you really need the backslash somewhere further down the road, you can replace them back again.

Properly escape json to command line call in Ruby or Rails

I'd like to call a command line utility from a ruby script like so:
#!/env/ruby
json = {"key" => "value that \"has\" quotes"}.to_json
`aws events put-targets --cli-input-json #{json}`
Such that the resultant call should look like:
aws events put-targets --cli-input-json "{\"key\": \"value that \"has\" quotes\"}"
However, the result in this string interpolation results in a clean looking json structure without the quotes escaped and so results in error at the command line. Eg.
aws events put-targets --cli-input-json {"key": "value that \"has\" quotes"}
I need all the quotes properly escaped so that a string to the command line can be parsed as proper json.
I've tried doing string manipulation to manually escape quotes with things like:
json.gsub(/\"/,'\"')
But that doesn't work either.
This seems like it's harder than it should be. How can I render a properly escaped json string to the command line call?
I do have a rails environment that I can run this through if there are utilities that ActiveSupport provides that would facilitate this.
In this case it is simpler and more effective to make a system call without a shell. If you use the multi-argument form of Kernel#system to invoke the external command directly:
system('aws', 'events', 'puts-targets', '--cli-input-json', json)
No shell, no quoting or escaping problems with json.
If you need to do more complicated things (such as capture output or errors), look into the various methods in Open3.
If you absolutely must go through a shell there's always Shellwords.shellescape. But really, when you use the shell, you're:
Building a command line with a bunch of Ruby string operations.
Invoking a shell.
Letting the shell parse the command line (i.e. undo everything you did in (1)).
Letting the shell invoke the command with your arguments.
Why not go straight to (4) yourself?
Thanks to #mu-is-too-short, I came across Shellwords which is a neat utility. This didn't solve the problem however, but led me to search for "shell escape json" which in turn led me to: Best way to escape and unescape strings in Ruby?
So:
json = {"key" => "value that \"has\" quotes"}.to_json.dump
Properly gets the escaped string that bash will understand. Tada.
UPDATE: Don't use this in production code. You're better off following #mu-is-too-short's advice in the comments or using a higher level library.

Jenkins text parameter rebuild

I'm using the text parameter to get multi line parameters, and write them to file.
If I use the rebuild, the text parameter is loaded as a single line string (where newline is removed).
Does anyone have an idea on how to fix this? I guess the rebuild plugin is the problem...
The multi-line text parameter seems to be rather buggy. One workaround you may consider is to substitute newlines with some custom escape system and then convert the escape sequences back to newlines inside your build.
A more advanced solution would be to modify the plugin itself to convert the escape sequences into newlines and use that modified plugin in your Jenkins. I've done that sort of thing for Claim Plugin to display failed matrix jobs which it did not do on its own. If you decide to take this route I can walk you through the main steps.
I have just enhanced the plugin to add TextParameterValue.jelly
That works fine, since text and textarea are not that different except new lines just use StringParameterValue.jelly as template and use <f:textarea name="value" value="${it.value}" /> instead of <f:textbox name="value" value="${it.value}" />

why ant print time with a slash?

I have this date format:
<entry key="buildDate" type="date" value="now" pattern="MM-dd-yyyy HH:mm:ss"/>
But ant gives me this result (in a key-value properties file):
buildDate=01-13-2012 14\:19\:59
Why ant add those slash in? because it is in a properties file?
Yes. The colons have special meaning in a Java properties file (they can act as key-value separators), so Ant needs to escape them with backslashes.
Ant doesn't add those \, it's Java.
See api docs for Properties, especially the store(Writer writer, String comments) method:
The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.

Resources