TeamCity allows build parameters to be defined, and then used in build scripts.
Eg define a parameter output_dir which is set to doc\release_notes. TeamCity documentation says to use it in build scripts with % escapes, eg %output_dir%.
But, how can such parameters be safely used in Python build scripts without being messed up by various characters?
Eg if the Python build script has
output_dir = "%output_dir%"
Then various characters in the build parameter can cause problems.
\will be treated as an escape character, sodoc\release_notesbecomes two lines,docandelease_notesseparated by a CR character\r.- A trailing
\will "escape" the closing quote, so forrelease_notes\the Python interpreter will generate a syntax error, saying the string"release_notes\"is unterminated. - A double-quote character will prematurely terminate the string, so
9" nailswill be seen by Python as the string"9"followed by a syntax error.
Using a Python "raw string literal", eg
output_dir = r"%output_dir%"
solves (1) but not (2) or (3).
Using a Python "raw string multi-line literal", eg
output_dir = r"""%output_dir%"""
solves (1), but not (2), and solves (3) except if the " character is at the end of the string.
How can TeamCity build parameters be safely used in Python build scripts without being messed up by various string escape character issues?