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.umask(use_sudo=False)[source]

Get the user’s umask.

Returns a string such as '0002', representing the user’s umask as an octal number.

If use_sudo is True, this function returns root’s umask.

fabtools.files.upload_template(filename, destination, context=None, use_jinja=False, template_dir=None, use_sudo=False, backup=True, mirror_local_mode=False, mode=None, mkdir=False, chown=False, user=None)[source]

Upload a template file.

This is a wrapper around fabric.contrib.files.upload_template() that adds some extra parameters.

If mkdir is True, then the remote directory will be created, as the current user or as user if specified.

If chown is True, then it will ensure that the current user (or user if specified) is the owner of the remote 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')
fabtools.files.uncommented_lines(filename, use_sudo=False)[source]

Get the lines of a remote file, ignoring empty or commented ones

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

Return the time of last modification of path. The return value is a number giving the number of seconds since the epoch

Same as os.path.getmtime()

fabtools.files.copy(source, destination, recursive=False, use_sudo=False)[source]

Copy a file or directory

fabtools.files.move(source, destination, use_sudo=False)[source]

Move a file or directory

Create a symbolic link to a file or directory

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

Remove a file or directory