pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

facts.files.File does not properly handle the ~ (relative) directory in Ubuntu/Debian

Open stone-w4tch3r opened this issue 1 year ago • 1 comments

Describe the bug

Addressing file via ~/file notation resolves to wrong path using facts.files.File, operations.files.file and so on

To Reproduce

  1. Spin up Ubuntu/Debian vm
  2. Create file test.txt in ~ directory
  3. Run main.py:
from pyinfra.facts import files
from pyinfra import host

relative_file = host.get_fact(files.File, '~/test.txt')
absolute_file = host.get_fact(files.File, '/home/ubuntu/test.txt')

print()
print()
print('+++\n' + str(relative_file) + '\n+++')
print('+++\n' + str(absolute_file) + '\n+++')
print()
  1. See output:
(...)
+++
None
+++
+++
{'user': 'ubuntu', 'group': 'ubuntu', 'mode': 664, 'atime': datetime.datetime(2023, 11, 6, 8, 5, 43), 'mtime': datetime.datetime(2023, 11, 6, 8, 5, 43), 'ctime': datetime.datetime(2023, 11, 6, 8, 5, 43), 'size': 0}
+++
(...)
  • Target system information Ubuntu 20.04.6 LTS (GNU/Linux 5.4.0-164-generic x86_64) Debian GNU/Linux 12 (bookworm) Linux 6.1.0-13-amd64 x86-64

Expected behavior

Explicit warning/error about using of ~ in path, or support for this notation

Meta

  • Include output of pyinfra --support.
pyinfra --support
--> Support information:

    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-6.2.0-36-generic-x86_64-with-glibc2.37
      Release: 6.2.0-36-generic
      Machine: x86_64
    pyinfra: v2.8
    Executable: /home/user/Projects/Test/venv/bin/pyinfra
    Python: 3.11.4 (CPython, GCC 12.3.0)

  • How was pyinfra installed (source/pip)? pip
  • Include pyinfra-debug.log (if one was created) none
  • Consider including output with -vv and --debug. nothing special in verbose output

stone-w4tch3r avatar Nov 06 '23 08:11 stone-w4tch3r

Yeah this should work, it's a gotcha currently that it doesn't just work as one would expect.

Fizzadar avatar Nov 25 '23 19:11 Fizzadar

I believe this is caused by the fact that ~ is not expanded when wrapped inside quotes (does not matter if single or double quotes).

It might be a viable solution to check if the path starts with ~ and if it does, only quote the part after the tilde.

I'll try to create a PR later.

@Fizzadar Just saw that you assigned yourself, are you fine with me giving this a try?

simonhammes avatar Aug 12 '24 16:08 simonhammes