fabtools.require.files

Files and directories

This module provides high-level tools for managing files and directories.

See also

fabtools.files

fabtools.require.files.directory(path, use_sudo=False, owner='', group='', mode='')[source]

Require a directory to exist.

from fabtools import require

require.directory('/tmp/mydir', owner='alice', use_sudo=True)

Note

This function can be accessed directly from the fabtools.require module for convenience.

fabtools.require.files.directories(path_list, use_sudo=False, owner='', group='', mode='')[source]

Require a list of directories to exist.

from fabtools import require
dirs=[
    '/tmp/mydir',
    '/tmp/mydear',
    '/tmp/my/dir'
]
require.directories(dirs, owner='alice', mode='750')

Note

This function can be accessed directly from the fabtools.require module for convenience.

fabtools.require.files.file(path=None, contents=None, source=None, url=None, md5=None, use_sudo=False, owner=None, group='', mode=None, verify_remote=True, temp_dir='/tmp')[source]

Require a file to exist and have specific contents and properties.

You can provide either:

  • contents: the required contents of the file:

    from fabtools import require
    
    require.file('/tmp/hello.txt', contents='Hello, world')
    
  • source: the local path of a file to upload:

    from fabtools import require
    
    require.file('/tmp/hello.txt', source='files/hello.txt')
    
  • url: the URL of a file to download (path is then optional):

    from fabric.api import cd
    from fabtools import require
    
    with cd('tmp'):
        require.file(url='http://example.com/files/hello.txt')
    

If verify_remote is True (the default), then an MD5 comparison will be used to check whether the remote file is the same as the source. If this is False, the file will be assumed to be the same if it is present. This is useful for very large files, where generating an MD5 sum may take a while.

When providing either the contents or the source parameter, Fabric’s put function will be used to upload the file to the remote host. When use_sudo is True, the file will first be uploaded to a temporary directory, then moved to its final location. The default temporary directory is /tmp, but can be overridden with the temp_dir parameter. If temp_dir is an empty string, then the user’s home directory will be used.

If use_sudo is True, then the remote file will be owned by root, and its mode will reflect root’s default umask. The optional owner, group and mode parameters can be used to override these properties.

Note

This function can be accessed directly from the fabtools.require module for convenience.

fabtools.require.files.template_file(path=None, template_contents=None, template_source=None, context=None, **kwargs)[source]

Require a file whose contents is defined by a template.

fabtools.require.files.temporary_directory(template=None)[source]

Require a temporary directory.

The directory is created using the mktemp command. It will be created in /tmp, unless the TMPDIR environment variable is set to another location.

from fabtools.require.files import temporary_directory

tmp_dir = temporary_directory()

You can choose a specific location and name template for the temporary directory:

from fabtools.require.files import temporary_directory

tmp_dir = temporary_directory('/var/tmp/temp.XXXXXX')

You can also call this function as a context manager. In this case, the directory and its contents will be automatically deleted when exiting the block:

from pipes import quote
from posixpath import join

from fabtools.require.files import temporary_directory

with temporary_directory() as tmp_dir:
    path = join(tmp_dir, 'foo')
    run('touch %s' % quote(path))