def _interpret_host_string(host_string): """ Apply given host string to the env dict. Split it into hostname, username and port (using `~fabric.network.normalize`) and store the full host string plus its constituent parts into the appropriate env vars. Returns the parts as split out by ``normalize`` for convenience. """ from fabric.state import env username, hostname, port = _normalize(host_string) env.host_string = host_string env.host = hostname env.user = username env.port = port return username, hostname, port
def _rsync(from_path, to_path, reverse=False, exclude=(), delete=False, extra_opts="", ssh_opts="", capture=False): """Perform rsync from some remote location to some local location. Optionally does exactly the reverse (syncs from some local location to some remote location) """ # Adapted from: # https://github.com/fabric/fabric/blob/master/fabric/contrib/project.py # Create --exclude options from exclude list exclude_opts = ' --exclude "%s"' * len(exclude) # Double-backslash-escape exclusions = tuple([str(s).replace('"', '\\\\"') for s in exclude]) # Honor SSH key(s) key_string = '' keys = _key_filenames() if keys: key_string = '-i ' + ' -i '.join(keys) # Port user, host, port = _normalize(_env.host_string) if host.startswith('@'): host = host[1:] port_string = '-p {0:s}'.format(port) # RSH rsh_string = '' rsh_parts = [key_string, port_string, ssh_opts] if any(rsh_parts): rsh_string = '--rsh="ssh {0:s}"'.format(' '.join(rsh_parts)) # Set remote sudo if _env.hostout.options.get('remote-sudo') == 'true': remote_sudo = ' --rsync-path="sudo rsync"' extra_opts = (extra_opts + remote_sudo).strip() # Set up options part of string options_map = { 'delete': '--delete' if delete else '', 'exclude': exclude_opts % exclusions, 'rsh': rsh_string, 'extra': extra_opts } options = ('%(delete)s%(exclude)s -pthlrz ' '%(extra)s %(rsh)s') % options_map # Interpret direction and define command if not reverse: cmd = 'rsync {0:s} {1:s}@{2:s}:{3:s} {4:s}'.format( options, user, host, from_path, to_path) else: cmd = 'rsync {0:s} {1:s} {2:s}@{3:s}:{4:s}'.format( options, to_path, user, host, from_path) if _env.hostout.options.get('local-sudo') == 'true': cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] rsync: {0:s}'.format(cmd)) return _local(cmd, capture=capture)