powershell
napt.powershell
PowerShell string quoting, file encoding, and the build-host script runner.
Values written into PowerShell source (recipe fields, installer metadata, file paths) can be vendor-controlled, and a value that closes its string early runs as code on the endpoint or the build host.
Scripts NAPT runs on the build host to read installer metadata hand their results back through a UTF-8 file rather than stdout; see run_powershell_lines for why.
PowerShell treats typographic quotes as string delimiters too: U+2018 to U+201B close a single-quoted string, U+201C to U+201E a double-quoted one. Both quoting functions escape the full sets. A value that lands outside a string, such as an app name in a comment line, needs a different guard: a line break ends the comment and the rest of the value runs as code, so strip_control_characters removes line breaks and the other control characters first.
run_powershell_lines
Runs a PowerShell script that hands its results back through a file.
PowerShell writes captured stdout in the console's OEM code page (cp437
on English Windows), which Python would decode as the locale code page
(cp1252), mangling every non-ASCII character of a product name or an
icon name. Changing the console's code page from inside the script
would fix that but leaks into the user's terminal for the rest of the
session. So the script writes its values as UTF-8 with
[System.IO.File]::WriteAllLines to the file named by the environment
variable out_var, and this function reads them back. The path
travels in the environment, keeping it out of the script text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script
|
str
|
The PowerShell source. It writes its results to
|
required |
out_var
|
str
|
Name of the environment variable that carries the output file's path. |
required |
timeout
|
int
|
Seconds to wait for the script. |
required |
what
|
str
|
What the script does, for messages ( |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The lines of the output file. |
Raises:
| Type | Description |
|---|---|
PackagingError
|
If the script exits non-zero (the message carries its stderr), times out, or PowerShell cannot be launched. |
Source code in napt/powershell.py
ps_single_quote
Formats a value as a single-quoted PowerShell string literal.
Single-quoted strings are verbatim: no variable expansion, no subexpressions, no backtick escapes. The only characters with meaning are the quote delimiters, which are escaped by doubling. Prefer this form whenever the surrounding PowerShell allows it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw text to embed. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The literal including its surrounding quotes. |
Source code in napt/powershell.py
ps_escape_double_quoted
Escapes a value for use inside a double-quoted PowerShell string.
Backticks, dollar signs, and every double-quote delimiter are prefixed
with a backtick so the value reads as literal text instead of closing
the string, expanding a variable, or running a $(...) subexpression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw text destined for the inside of a double-quoted string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The escaped text, without surrounding quotes. |
Example
Escape an app name for a template placeholder in double quotes:
Source code in napt/powershell.py
strip_control_characters
Removes line breaks and other control characters from a value.
Quoting keeps a value safe inside a string, but an app name is also written into a comment line and a script filename, where a line break ends the comment (the remainder runs as code) or makes the filename invalid. Each run of control characters becomes a single space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw text, typically an app name from installer metadata or a recipe. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with every run of control characters replaced by a space |
str
|
and surrounding whitespace trimmed. |
Example
Clean a display name read from an installer manifest: