Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/pyinfra/facts/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,16 @@ def command(self, path, marker=None, begin=None, end=None):
QuoteString(f"{MISSING}{path}"),
)

# The markers are passed to awk as arguments and matched as plain text with index(),
# so regex characters in them (``(``, ``.``, ``*``, ``/``...) are not special.
cmd = StringCommand(
f"awk '/{end}/{{ f=0}} f; /{start}/{{ f=1}} ' ",
'awk \'BEGIN { s=ARGV[2]; e=ARGV[3]; ARGV[2]=""; ARGV[3]="" } '
"index($0, e) { f=0 } f; index($0, s) { f=1 }' ",
QuoteString(path),
" ",
QuoteString(start),
" ",
QuoteString(end),
" || ",
backstop,
_separator="",
Expand Down
27 changes: 22 additions & 5 deletions src/pyinfra/operations/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,8 @@ def block(
+ escape_regex_characters: whether to escape regex characters from the matching line
+ try_prevent_shell_expansion: deprecated and ignored; ``content`` is always written
literally (no shell expansion) and is safely shell-quoted
+ marker: the base string used to mark the text. Default is ``# {mark} PYINFRA BLOCK``
+ marker: the base string used to mark the text. Default is ``# {mark} PYINFRA BLOCK``.
It is matched as plain text, not as a regex.
+ begin: the value for ``{mark}`` in the marker before the content. Default is ``BEGIN``
+ end: the value for ``{mark}`` in the marker after the content. Default is ``END``
+ preserve_meta: whether to restore the file's owner, group and mode after rewriting it.
Expand Down Expand Up @@ -2283,15 +2284,19 @@ def block(
if (len(current) != len(content)) or (
not all(lines[0] == lines[1] for lines in zip(content, current, strict=True))
): # marked_block found but text is different
# markers are passed as arguments and matched as plain text with index()
prog = (
'awk \'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=""}}'
f"/{mark_1}/ {{print; print x; f=0}} /{mark_2}/ {{print; f=1; next}} f'"
"awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; "
'ARGV[2]=""; ARGV[3]=""; ARGV[4]=""} '
"index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f'"
)
cmd = StringCommand(
out_prep,
prog,
q_path,
QuoteString(block_content),
QuoteString(mark_1),
QuoteString(mark_2),
'> "$OUT"',
real_out,
)
Expand All @@ -2308,8 +2313,20 @@ def block(
elif current == []:
host.noop("no remove required: markers not found")
else:
cmd = StringCommand(f"awk '/{mark_1}/,/{mark_2}/ {{next}} 1'")
yield StringCommand(out_prep, cmd, q_path, "> $OUT", real_out)
# markers are passed as arguments and matched as plain text with index()
prog = (
'awk \'BEGIN {s=ARGV[2]; e=ARGV[3]; ARGV[2]=""; ARGV[3]=""} '
"index($0, s) {r=1} r {if (index($0, e)) r=0; next} 1'"
)
yield StringCommand(
out_prep,
prog,
q_path,
QuoteString(mark_1),
QuoteString(mark_2),
"> $OUT",
real_out,
)


_TAR_FORMATS = {
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Block/defaults.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/tmp/foo",
"command": "awk '/# END PYINFRA BLOCK/{ f=0} f; /# BEGIN PYINFRA BLOCK/{ f=1} ' /tmp/foo || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )",
"command": "awk 'BEGIN { s=ARGV[2]; e=ARGV[3]; ARGV[2]=\"\"; ARGV[3]=\"\" } index($0, e) { f=0 } f; index($0, s) { f=1 }' /tmp/foo '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )",
"output": [
"aaa",
"bbb"
Expand Down
8 changes: 8 additions & 0 deletions tests/facts/files.Block/marker_with_regex_characters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
arg:
path: /tmp/foo
marker: "# {mark} PYINFRA (block)"
command: "awk 'BEGIN { s=ARGV[2]; e=ARGV[3]; ARGV[2]=\"\"; ARGV[3]=\"\" } index($0, e) { f=0 } f; index($0, s) { f=1 }' /tmp/foo '# BEGIN PYINFRA (block)' '# END PYINFRA (block)' || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )"
output:
- aaa
fact:
- aaa
8 changes: 8 additions & 0 deletions tests/facts/files.Block/marker_with_slash_and_quote.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
arg:
path: /tmp/foo
marker: "// {mark} it's /etc/app"
command: "awk 'BEGIN { s=ARGV[2]; e=ARGV[3]; ARGV[2]=\"\"; ARGV[3]=\"\" } index($0, e) { f=0 } f; index($0, s) { f=1 }' /tmp/foo '// BEGIN it'\"'\"'s /etc/app' '// END it'\"'\"'s /etc/app' || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )"
output:
- aaa
fact:
- aaa
2 changes: 1 addition & 1 deletion tests/facts/files.Block/no_markers_found.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/tmp/foo",
"command": "awk '/# END PYINFRA BLOCK/{ f=0} f; /# BEGIN PYINFRA BLOCK/{ f=1} ' /tmp/foo || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )",
"command": "awk 'BEGIN { s=ARGV[2]; e=ARGV[3]; ARGV[2]=\"\"; ARGV[3]=\"\" } index($0, e) { f=0 } f; index($0, s) { f=1 }' /tmp/foo '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )",
"output": [
"__pyinfra_exists_/tmp/foo"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Block/not_found.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/tmp/foo",
"command": "awk '/# END PYINFRA BLOCK/{ f=0} f; /# BEGIN PYINFRA BLOCK/{ f=1} ' /tmp/foo || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )",
"command": "awk 'BEGIN { s=ARGV[2]; e=ARGV[3]; ARGV[2]=\"\"; ARGV[3]=\"\" } index($0, e) { f=0 } f; index($0, s) { f=1 }' /tmp/foo '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' || (find /tmp/foo -type f > /dev/null && echo __pyinfra_exists_/tmp/foo || echo __pyinfra_missing_/tmp/foo )",
"output": [
"__pyinfra_missing_/tmp/foo"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
}
},
"commands": [
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=\"\"}}/# BEGIN PYINFRA BLOCK/ {print; print x; f=0} /# END PYINFRA BLOCK/ {print; f=1; next} f' /home/someone/something 'should be this' > \"$OUT\" && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; ARGV[2]=\"\"; ARGV[3]=\"\"; ARGV[4]=\"\"} index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f' /home/someone/something 'should be this' '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > \"$OUT\" && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
}
},
"commands": [
"cp /home/someone/something /home/someone/something.a-timestamp && OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=\"\"}}/# BEGIN PYINFRA BLOCK/ {print; print x; f=0} /# END PYINFRA BLOCK/ {print; f=1; next} f' /home/someone/something 'should be this' > \"$OUT\" && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
"cp /home/someone/something /home/someone/something.a-timestamp && OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; ARGV[2]=\"\"; ARGV[3]=\"\"; ARGV[4]=\"\"} index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f' /home/someone/something 'should be this' '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > \"$OUT\" && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
}
},
"commands": [
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=\"\"}}/# BEGIN PYINFRA BLOCK/ {print; print x; f=0} /# END PYINFRA BLOCK/ {print; f=1; next} f' /home/someone/something 'should be this\nand this\nand even this' > \"$OUT\" && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; ARGV[2]=\"\"; ARGV[3]=\"\"; ARGV[4]=\"\"} index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f' /home/someone/something 'should be this\nand this\nand even this' '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > \"$OUT\" && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ facts:
- previously was something else
commands:
- |-
cp /home/someone/something /home/someone/something.a-timestamp && OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && awk 'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=""}}/# BEGIN PYINFRA BLOCK/ {print; print x; f=0} /# END PYINFRA BLOCK/ {print; f=1; next} f' /home/someone/something 'should be this' > "$OUT" && mv "$OUT" /home/someone/something
cp /home/someone/something /home/someone/something.a-timestamp && OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; ARGV[2]=""; ARGV[3]=""; ARGV[4]=""} index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f' /home/someone/something 'should be this' '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > "$OUT" && mv "$OUT" /home/someone/something
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ facts:
- previously was something else
commands:
- |-
OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && awk 'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=""}}/# BEGIN PYINFRA BLOCK/ {print; print x; f=0} /# END PYINFRA BLOCK/ {print; f=1; next} f' /home/someone/something 'should be this' > "$OUT" && mv "$OUT" /home/someone/something
OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; ARGV[2]=""; ARGV[3]=""; ARGV[4]=""} index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f' /home/someone/something 'should be this' '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > "$OUT" && mv "$OUT" /home/someone/something
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require_platform:
- Darwin
- Linux
args:
- /home/someone/something
kwargs:
present: false
marker: "# {mark} PYINFRA (block)"
preserve_meta: false
facts:
files.Block:
begin=None, end=None, marker=# {mark} PYINFRA (block), path=/home/someone/something:
- existing content
commands:
- |-
OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && awk 'BEGIN {s=ARGV[2]; e=ARGV[3]; ARGV[2]=""; ARGV[3]=""} index($0, s) {r=1} r {if (index($0, e)) r=0; next} 1' /home/someone/something '# BEGIN PYINFRA (block)' '# END PYINFRA (block)' > $OUT && mv "$OUT" /home/someone/something
4 changes: 2 additions & 2 deletions tests/operations/files.block/remove_but_content_not_none.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
}
},
"commands": [
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk '/# BEGIN PYINFRA BLOCK/,/# END PYINFRA BLOCK/ {next} 1' /home/someone/something > $OUT && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {s=ARGV[2]; e=ARGV[3]; ARGV[2]=\"\"; ARGV[3]=\"\"} index($0, s) {r=1} r {if (index($0, e)) r=0; next} 1' /home/someone/something '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > $OUT && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
]
}
}
4 changes: 2 additions & 2 deletions tests/operations/files.block/remove_existing_block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
}
},
"commands": [
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk '/# BEGIN PYINFRA BLOCK/,/# END PYINFRA BLOCK/ {next} 1' /home/someone/something > $OUT && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
"OUT=\"$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)\" && MODE=\"$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)\" && OWNER=\"$(stat -c \"%u:%g\" /home/someone/something 2>/dev/null || stat -f \"%u:%g\" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))\" && awk 'BEGIN {s=ARGV[2]; e=ARGV[3]; ARGV[2]=\"\"; ARGV[3]=\"\"} index($0, s) {r=1} r {if (index($0, e)) r=0; next} 1' /home/someone/something '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > $OUT && mv \"$OUT\" /home/someone/something && chown \"$OWNER\" /home/someone/something && chmod \"$MODE\" /home/someone/something"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require_platform:
- Darwin
- Linux
args:
- /home/someone/something
kwargs:
content: should be this
marker: "# {mark} PYINFRA (block)"
preserve_meta: false
facts:
files.Block:
begin=None, end=None, marker=# {mark} PYINFRA (block), path=/home/someone/something:
- previously was something else
commands:
- |-
OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; ARGV[2]=""; ARGV[3]=""; ARGV[4]=""} index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f' /home/someone/something 'should be this' '# BEGIN PYINFRA (block)' '# END PYINFRA (block)' > "$OUT" && mv "$OUT" /home/someone/something
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ facts:
- previously was something else
commands:
- |-
OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && MODE="$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)" && OWNER="$(stat -c "%u:%g" /home/someone/something 2>/dev/null || stat -f "%u:%g" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))" && awk 'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=""}}/# BEGIN PYINFRA BLOCK/ {print; print x; f=0} /# END PYINFRA BLOCK/ {print; f=1; next} f' /home/someone/something 'alias l="lsd -la"
OUT="$(TMPDIR=_tempdir_ mktemp -t pyinfra.XXXXXX)" && MODE="$(stat -c %a /home/someone/something 2>/dev/null || stat -f %Lp /home/someone/something 2>/dev/null)" && OWNER="$(stat -c "%u:%g" /home/someone/something 2>/dev/null || stat -f "%u:%g" /home/someone/something 2>/dev/null || echo $(id -un):$(id -gn))" && awk 'BEGIN {f=1; x=ARGV[2]; s=ARGV[3]; e=ARGV[4]; ARGV[2]=""; ARGV[3]=""; ARGV[4]=""} index($0, s) {print; print x; f=0} index($0, e) {print; f=1; next} f' /home/someone/something 'alias l="lsd -la"
export _ZO_ECHO='"'"'1'"'"'
eval "$(zoxide init --cmd cd bash)"' > "$OUT" && mv "$OUT" /home/someone/something && chown "$OWNER" /home/someone/something && chmod "$MODE" /home/someone/something
eval "$(zoxide init --cmd cd bash)"' '# BEGIN PYINFRA BLOCK' '# END PYINFRA BLOCK' > "$OUT" && mv "$OUT" /home/someone/something && chown "$OWNER" /home/someone/something && chmod "$MODE" /home/someone/something
Loading