diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2016-01-06 20:12:03 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2016-01-06 20:12:03 +0000 | 
| commit | 9e6d35490a6542f9c97607f93c2ef8ca8e03cbcc (patch) | |
| tree | dd2a1ddf0476664c2b823409c36cbccd52662ca7 /utils/sync-source/lib/transfer | |
| parent | 3bd2e91faeb9eeec1aae82c64a3253afff551cfd (diff) | |
Notes
Diffstat (limited to 'utils/sync-source/lib/transfer')
| -rw-r--r-- | utils/sync-source/lib/transfer/__init__.py | 0 | ||||
| -rw-r--r-- | utils/sync-source/lib/transfer/protocol.py | 7 | ||||
| -rw-r--r-- | utils/sync-source/lib/transfer/rsync.py | 60 | ||||
| -rw-r--r-- | utils/sync-source/lib/transfer/transfer_spec.py | 11 | 
4 files changed, 78 insertions, 0 deletions
| diff --git a/utils/sync-source/lib/transfer/__init__.py b/utils/sync-source/lib/transfer/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 --- /dev/null +++ b/utils/sync-source/lib/transfer/__init__.py diff --git a/utils/sync-source/lib/transfer/protocol.py b/utils/sync-source/lib/transfer/protocol.py new file mode 100644 index 000000000000..6a4e6e0a3a45 --- /dev/null +++ b/utils/sync-source/lib/transfer/protocol.py @@ -0,0 +1,7 @@ +class Protocol(object): +    def __init__(self, options, config): +        self.options = options +        self.config = config + +    def transfer(transfer_specs, dry_run): +        raise "transfer must be overridden by transfer implementation" diff --git a/utils/sync-source/lib/transfer/rsync.py b/utils/sync-source/lib/transfer/rsync.py new file mode 100644 index 000000000000..7a89344b6991 --- /dev/null +++ b/utils/sync-source/lib/transfer/rsync.py @@ -0,0 +1,60 @@ +import os.path +import pprint +import subprocess +import sys + +import transfer.protocol + + +class RsyncOverSsh(transfer.protocol.Protocol): +    def __init__(self, options, config): +        super(RsyncOverSsh, self).__init__(options, config) +        self.ssh_config = config.get_value("ssh") + +    def build_rsync_command(self, transfer_spec, dry_run): +        dest_path = os.path.join( +            self.ssh_config["root_dir"], +            transfer_spec.dest_path) +        flags = "-avz" +        if dry_run: +            flags += "n" +        cmd = [ +            "rsync", +            flags, +            "-e", +            "ssh -p {}".format(self.ssh_config["port"]), +            "--rsync-path", +            # The following command needs to know the right way to do +            # this on the dest platform - ensures the target dir exists. +            "mkdir -p {} && rsync".format(dest_path) +        ] + +        # Add source dir exclusions +        if transfer_spec.exclude_paths: +            for exclude_path in transfer_spec.exclude_paths: +                cmd.append("--exclude") +                cmd.append(exclude_path) + +        cmd.extend([ +            "--delete", +            transfer_spec.source_path + "/", +            "{}@{}:{}".format( +                self.ssh_config["user"], +                self.ssh_config["dest_host"], +                dest_path)]) +        return cmd + +    def transfer(self, transfer_specs, dry_run): +        if self.options.verbose: +            printer = pprint.PrettyPrinter() +            for spec in transfer_specs: +                printer.pprint(spec) + +        for spec in transfer_specs: +            cmd = self.build_rsync_command(spec, dry_run) +            if self.options.verbose: +                print "executing the following command:\n{}".format(cmd) +            result = subprocess.call( +                cmd, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr) +            if result != 0: +                return result diff --git a/utils/sync-source/lib/transfer/transfer_spec.py b/utils/sync-source/lib/transfer/transfer_spec.py new file mode 100644 index 000000000000..cc76c70bf41c --- /dev/null +++ b/utils/sync-source/lib/transfer/transfer_spec.py @@ -0,0 +1,11 @@ +class TransferSpec(object): +    def __init__(self, source_path, exclude_paths, dest_path): +        self.source_path = source_path +        self.exclude_paths = exclude_paths +        self.dest_path = dest_path + +    def __repr__(self): +        fmt = ( +            "TransferSpec(source_path='{}', exclude_paths='{}', " +            "dest_path='{}')") +        return fmt.format(self.source_path, self.exclude_paths, self.dest_path) | 
