fabtools.files

Files and directories

fabtools.files.is_file(path, use_sudo=False)[source]

Check if a path exists, and is a file.

fabtools.files.is_dir(path, use_sudo=False)[source]

Check if a path exists, and is a directory.

Check if a path exists, and is a symbolic link.

fabtools.files.owner(path, use_sudo=False)[source]

Get the owner name of a file or directory.

fabtools.files.group(path, use_sudo=False)[source]

Get the group name of a file or directory.

fabtools.files.mode(path, use_sudo=False)[source]

Get the mode (permissions) of a file or directory.

Returns a string such as '0755', representing permissions as an octal number.

fabtools.files.upload_template(filename, template, context=None, use_sudo=False, user='root', mkdir=False, chown=False)[source]

Upload a template file.

fabtools.files.md5sum(filename, use_sudo=False)[source]

Compute the MD5 sum of a file.

class fabtools.files.watch(filenames, callback=None, use_sudo=False)[source]

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')

Project Versions

Table Of Contents

Previous topic

fabtools.deb

Next topic

fabtools.group

This Page