I created a new directory with pyinfra using the relative path new_dir
from pyinfra.operations import files, server
files.directory("new_dir")
Now I would like to get the absolute path of the new directory. I tried
result = server.shell("pwd", _chdir="new_dir")
print(result.stdout)
But that only raises an exception:
RuntimeError: Cannot evaluate operation result before execution
How can I get the absolute path of the directory pyinfra created for me?
I created a new directory with pyinfra using the relative path new_dir
from pyinfra.operations import files, server
files.directory("new_dir")
Now I would like to get the absolute path of the new directory. I tried
result = server.shell("pwd", _chdir="new_dir")
print(result.stdout)
But that only raises an exception:
RuntimeError: Cannot evaluate operation result before execution
How can I get the absolute path of the directory pyinfra created for me?
Share Improve this question asked Jan 19 at 21:01 asmaierasmaier 11.8k11 gold badges85 silver badges106 bronze badges 1- Check if the Operation Output section in the documentation helps. – Man made of meat Commented Jan 20 at 0:40
1 Answer
Reset to default 0So as @user202311 suggested I checked the documentation and it suggested to try a nested operation. So
from pyinfra.operations import files, server, python
DIR_NAME = "new_dir"
files.directory(DIR_NAME)
python.call(lambda: print(server.shell("pwd", _chdir=DIR_NAME).stdout))
will in fact print out the absolute path of the created directory.
However I was not able to extract the value out of the callback function, so that I can use it in the following steps. The only way to do what I wanted was to guess the absolute path differently like
from pyinfra import host
from pyinfra.operations import files, server
home = host.get_fact(server.Home, "")
DIR_NAME = "new_dir"
files.directory(DIR_NAME)
path = home + "/" + DIR_NAME
print(path)
But this will only work if your working directory was in fact the home directory.