Project Tools¶
Useful non-core functionality, e.g. functions composing multiple operations.
-
fabric.contrib.project.rsync_project(remote_dir, local_dir=None, exclude=(), delete=False, extra_opts='', ssh_opts='', capture=False, upload=True, default_opts='-pthrvz')¶ Synchronize a remote directory with the current project directory via rsync.
Where
upload_project()makes use ofsftpto copy one’s entire project every time it is invoked,rsync_project()uses thersynccommand-line utility, which only transfers files newer than those on the remote end.rsync_project()is thus a simple wrapper aroundrsync; for details on howrsyncworks, please see its manpage.rsyncmust be installed on both your local and remote systems in order for this operation to work correctly.This function makes use of Fabric’s
local()operation, and returns the output of that function call; thus it will return the stdout, if any, of the resultantrsynccall.rsync_project()uses the current Fabric connection parameters (user, host, port) by default, adding them to rsync’s ssh options (then mixing inssh_opts, if given – see below.)rsync_project()takes the following parameters:remote_dir: the only required parameter, this is the path to the directory on the remote server. Due to howrsyncis implemented, the exact behavior depends on the value oflocal_dir:- If
local_dirends with a trailing slash, the files will be dropped inside ofremote_dir. E.g.rsync_project("/home/username/project/", "foldername/")will drop the contents offoldernameinside of/home/username/project. - If
local_dirdoes not end with a trailing slash (and this includes the default scenario, whenlocal_diris not specified),remote_diris effectively the “parent” directory, and a new directory named afterlocal_dirwill be created inside of it. Sorsync_project("/home/username", "foldername")would create a new directory/home/username/foldername(if needed) and place the files there.
- If
local_dir: by default,rsync_projectuses your current working directory as the source directory. This may be overridden by specifyinglocal_dir, which is a string passed verbatim torsync, and thus may be a single directory ("my_directory") or multiple directories ("dir1 dir2"). See thersyncdocumentation for details.exclude: optional, may be a single string, or an iterable of strings, and is used to pass one or more--excludeoptions torsync.delete: a boolean controlling whetherrsync’s--deleteoption is used. If True, instructsrsyncto remove remote files that no longer exist locally. Defaults to False.extra_opts: an optional, arbitrary string which you may use to pass custom arguments or options torsync.ssh_opts: Likeextra_optsbut specifically for the SSH options string (rsync’s--rshflag.)capture: Sent directly into an innerlocalcall.upload: a boolean controlling whether file synchronization is performed up or downstream. Upstream by default.default_opts: the default rsync options-pthrvz, override if desired (e.g. to remove verbosity, etc).
Furthermore, this function transparently honors Fabric’s port and SSH key settings. Calling this function when the current host string contains a nonstandard port, or when
env.key_filenameis non-empty, will use the specified port and/or SSH key filename(s).For reference, the approximate
rsynccommand-line call that is constructed by this function is the following:rsync [--delete] [--exclude exclude[0][, --exclude[1][, ...]]] \ [default_opts] [extra_opts] <local_dir> <host_string>:<remote_dir>
New in version 1.4.0: The
ssh_optskeyword argument.New in version 1.4.1: The
capturekeyword argument.New in version 1.8.0: The
default_optskeyword argument.
-
fabric.contrib.project.upload_project(local_dir=None, remote_dir='', use_sudo=False)¶ Upload the current project to a remote system via
tar/gzip.local_dirspecifies the local project directory to upload, and defaults to the current working directory.remote_dirspecifies the target directory to upload into (meaning that a copy oflocal_dirwill appear as a subdirectory ofremote_dir) and defaults to the remote user’s home directory.use_sudospecifies which method should be used when executing commands remotely.sudowill be used if use_sudo is True, otherwiserunwill be used.This function makes use of the
tarandgzipprograms/libraries, thus it will not work too well on Win32 systems unless one is using Cygwin or something similar. It will attempt to clean up the local and remote tarfiles when it finishes executing, even in the event of a failure.Changed in version 1.7: Added the
use_sudokwarg.