In fabric, the cd
context manager works like
with cd("dir"):
run("command")
and the command will be run after changing to the dir
directory. This works fine, but the issue is that it uses a global state. For example, suppose I have a helper function, which needs to use cd:
def helper():
with cd("foo"):
run("some command")
If I call helper
from another function like
def main_function():
helper()
...
it works fine. But if I do something like
def main_function():
with cd("bar"):
helper()
it breaks, because the run("come command")
from helper is now run from bar/foo
instead of just foo
.
Any tips on how to get around this? I tried using absolute paths in cd, but that didn't work. What I really want is for the cd
context to only extend to the function scope.