Repository navigation
Description
Describe the bug
The files.line operation backup option does not work as documented ("If you pass a string value this will be used as the extension of the backed up file"). The backup file name can not be set successfully, instead, backup file names always use the timestamp.
To Reproduce
Backup works as documented with backup=True:
$ pyinfra --version
pyinfra: v3.10.0
$ mkdir /var/tmp/pyinfra-test/
$ cd /var/tmp/pyinfra-test/
$ echo "A nice line" >| foobar.txt
$ pyinfra @local files.line path=$PWD/foobar.txt line="^An even nicer line$" present=true backup=true -y
--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
[@local] Connected
--> Preparing operation func...
[@local] Ready: line
--> Skipping change detection
--> Beginning operation run...
--> Starting operation: files.line (path=/var/tmp/pyinfra-test/foobar.txt, line=^An even nicer line$, present=True, backup=True)
[@local] Success
--> Results:
Operation Hosts Success Error No Change
files.line (path=/var/tmp/pyinfra-test/foobar.txt, line=^An even nicer line$, present=True, backup=True) 1 1 - -
Grand total 1 1 - -
--> Disconnecting from hosts...
$ ls -ltr
total 136
-rw-r--r--. 1 holger holger 12 Oct 28 13:15 foobar.txt.2610281315
-rw-r--r--. 1 holger holger 31 Oct 28 13:15 foobar.txt
$ cat foobar.txt
A nice line
An even nicer line
...whereas when trying to use an explicit backup filename:
$ rm -f foobar.txt*
$ echo "A nice line" >| foobar.txt
$ pyinfra @local files.line path=$PWD/foobar.txt line="^An even nicer line$" present=true backup=$PWD/foobar.txt.bak -y -vvv
--> Loading config...
--> Loading inventory...
--> Connecting to hosts...
[@local] Connected
--> Preparing operation func...
[@local] Ready: line
--> Skipping change detection
--> Beginning operation run...
--> Starting operation: files.line (path=/var/tmp/pyinfra-test/foobar.txt, line=^An even nicer line$, present=True, backup=/var/tmp/pyinfra-test/foobar.txt.bak)
[@local] >>> sh -c 'grep -e '"'"'^An even nicer line$'"'"' /var/tmp/pyinfra-test/foobar.txt 2> /dev/null || ( find /var/tmp/pyinfra-test/foobar.txt -type f > /dev/null && echo __pyinfra_exists_/var/tmp/pyinfra-test/foobar.txt || true )'
[@local] __pyinfra_exists_/var/tmp/pyinfra-test/foobar.txt
[@local] Loaded fact files.FindInFile (extended_regex=False, interpolate_variables=False, path=/var/tmp/pyinfra-test/foobar.txt, pattern=^An even nicer line$)
[@local] >>> sh -c 'cp /var/tmp/pyinfra-test/foobar.txt /var/tmp/pyinfra-test/foobar.txt.2610281317 && echo '"'"'An even nicer line'"'"' >> /var/tmp/pyinfra-test/foobar.txt'
[@local] Success
--> Results:
Operation Hosts Success Error No Change
files.line (path=/var/tmp/pyinfra-test/foobar.txt, line=^An even nicer line$, present=True, backup=/var/tmp/pyinfra-test/foobar.txt.bak) 1 1 - -
Grand total 1 1 - -
--> Disconnecting from hosts...
$ ls -ltr
total 8
-rw-r--r--. 1 holger holger 12 Oct 28 13:17 foobar.txt.2610281317
-rw-r--r--. 1 holger holger 31 Oct 28 13:17 foobar.txt
The backup file name is not applied but the timestamp gets still used.
Looks to me as this is the culprit:
pyinfra/src/pyinfra/operations/files.py
Lines 459 to 468 in 89cb5f0
| if backup: | |
| backup_filename = f"{path}.{get_timestamp()}" | |
| echo_command = StringCommand( | |
| make_formatted_string_command( | |
| "cp {0} {1} && ", | |
| QuoteString(path), | |
| QuoteString(backup_filename), | |
| ), | |
| echo_command, | |
| ) |
The backup option is unconditionally handled as a truthy value, no special handling for a string argument.
Expected behavior
An explicit (fixed) backup file name can be set, as documented.
Meta
$ pyinfra --support
If you are having issues with pyinfra or wish to make feature requests, please
check out the GitHub issues at https://github.com/Fizzadar/pyinfra/issues .
When adding an issue, be sure to include the following:
System: Linux
Platform: Linux-5.14.0-687.42.1.el9_8.x86_64-x86_64-with-glibc2.34
Release: 5.14.0-687.42.1.el9_8.x86_64
Machine: x86_64
pyinfra: v3.10.0
click: v8.5.0
distro: v1.9.0
gevent: v26.8.0
jinja2: v3.1.6
packaging: v26.3
paramiko: v4.0.0
pydantic: v2.13.5
python-dateutil: v2.9.0.post0
typeguard: v4.6.0
types-paramiko: v4.0.0.20260518
typing-extensions: v4.16.0
Executable: /ae/data/pydev/hjoukl/nats/nats-lzone-infra/.venv/bin/pyinfra
Python: 3.14.7 (CPython, GCC 11.5.0 20240719 (Red Hat 11.5.0-14))
pyinfra is great, keep up the good work!
Best regards,
Holger
Activity