asmaier
asmaier

Reputation: 11776

How can I get the absolute path for a directory created with pyinfra?

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?

Upvotes: 0

Views: 33

Answers (1)

asmaier
asmaier

Reputation: 11776

So 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.

Upvotes: 0

Related Questions