See also
Check if a path exists, and is a symbolic link.
Get the mode (permissions) of a file or directory.
Returns a string such as '0755', representing permissions as an octal number.
Upload a template file.
Context manager to watch for changes to the contents of some files.
The filenames argument can be either a string (single filename) or a list (multiple filenames).
You can read the changed attribute at the end of the block to check if the contents of any of the watched files has changed.
You can also provide a callback that will be called at the end of the block if the contents of any of the watched files has changed.
Example using an explicit check:
from fabric.contrib.files import comment, uncomment
from fabtools.files import watch
from fabtools.services import restart
# Edit configuration file
with watch('/etc/daemon.conf') as config:
uncomment('/etc/daemon.conf', 'someoption')
comment('/etc/daemon.conf', 'otheroption')
# Restart daemon if needed
if config.changed:
restart('daemon')
Same example using a callback:
from functools import partial
from fabric.contrib.files import comment, uncomment
from fabtools.files import watch
from fabtools.services import restart
with watch('/etc/daemon.conf', callback=partial(restart, 'daemon')):
uncomment('/etc/daemon.conf', 'someoption')
comment('/etc/daemon.conf', 'otheroption')