Pyinfra: sed_replace function in operations/utils calls sed with args in wrong order

Created on 21 Jul 2020  路  9Comments  路  Source: Fizzadar/pyinfra

Describe the bug
files.replace doesn't work correctly because of sed arguments

To Reproduce
any files.replace example

Expected behavior
files.replace should work as advertised...

* To FIX: change order of replace and line on line 58 of operations/util/files.py

Meta

  • Include output of pyinfra --support.
System: Darwin
  Platform: Darwin-19.6.0-x86_64-i386-64bit
  Release: 19.6.0
  Machine: x86_64
pyinfra: v1.0.4
Executable: /Users/x/.pyenv/versions/3.7.7/envs/infra-3.7.7/bin/pyinfra
Python: 3.7.7 (CPython, Clang 11.0.3 (clang-1103.0.32.29))
  • How was pyinfra installed (source/pip)?
  • Include pyinfra-debug.log (if one was created)
  • Consider including output with -vv and --debug.
Bug

All 9 comments

@cpbrown could you include an example operation call here? I can't see the issue:

echo -e "yes\nyes\nno" > testfile
$ cat testfile
yes
yes
no
$ pyinfra @local files.replace testfile no yes
--> Loading config...
--> Loading inventory...

--> Connecting to hosts...
    [@local] Connected

--> Preparing operation...

--> Proposed changes:
    Groups: @local
    [@local]   Operations: 1   Commands: 1

--> Beginning operation run...
--> Starting operation: Files/Replace ('testfile', 'no', 'yes')
    [@local] Success

--> Results:
    Groups: @local
    [@local]   Successful: 1   Errors: 0   Commands: 1/1
$ cat testfile
yes
yes
yes

When I run the example as you did it also works for me. If I run it from a script:

files.line(name='msg', path='testfile', replace='bb',line='zz')

it does not work... I may be doing something wrong, but I don't think so

This is indeed strange, would you mind posting the output of running that with -v --debug?

[pyinfra_cli.main] Checking potential directory:
[pyinfra_cli.main] Checking potential directory:

--> Loading config...
--> Loading inventory...
[pyinfra_cli.inventory] Creating fake inventory...

--> Connecting to hosts...
[@local] Connected
[pyinfra.api.state] Activating host: @local

--> Preparing operations...
Loading: basher.py
[pyinfra.api.operation] Adding operation, {'Ensure line is in .bashrc'}, called @ basher.py:13, opLines=(0, 13), opHash=45b2789839d2d9f1244b3aac0658286a724a20c0
[pyinfra.api.facts] Getting fact: find_in_file (ensure_hosts: (@local,))
[pyinfra.api.connectors.local] --> Running command on localhost: sh -c 'grep -e '"'"'^.zz.$'"'"' foo 2> /dev/null || (find foo -type f > /dev/null && echo "__pyinfra_exists_foo")'
[pyinfra.api.connectors.util] --> Waiting for exit status...
[pyinfra.api.connectors.util] --> Command exit status: 0
Loaded fact find_in_file: ('foo', '^.zz.$')
[pyinfra.api.facts] Getting fact: find_in_file (ensure_hosts: (@local,))
[pyinfra.api.connectors.local] --> Running command on localhost: sh -c 'grep -e '"'"'^.bb.$'"'"' foo 2> /dev/null || (find foo -type f > /dev/null && echo "__pyinfra_exists_foo")'
[pyinfra.api.connectors.util] --> Waiting for exit status...
[pyinfra.api.connectors.util] --> Command exit status: 0
Loaded fact find_in_file: ('foo', '^.bb.$')
[@local] Ready: basher.py

--> Proposed changes:
Groups: @local
[@local] Operations: 1 Commands: 0

--> Beginning operation run...
--> Starting operation: Ensure line is in .bashrc
[pyinfra.api.operations] Starting operation Ensure line is in .bashrc on @local
[@local] No changes

--> Results:
Groups: @local
[@local] Successful: 1 Errors: 0 Commands: 0/0

Not sure if it matters, I'm actually calling files.line rather than files.replace

So sorry for the confusion on my part... there is no bug. When using file.lines, replace is the value you want to replace what matched, not the line you are replacing...

No worries @cpbrown, I'm going to leave this open to review the documentation and make sure this is more clear!

Github seems to have to have no private message capability so I'll leave a comment here... Just wanted to thank you for the effort you have put into this project. I'm coming to it as an ansible 'refugee' and I love what you've done. No more YAML! I plan to keep using it and may be able to contribute if you find my code worthy...

Thank you for the kind words @cpbrown, much appreciated!

Was this page helpful?
0 / 5 - 0 ratings