koder aka kdanilov | 80cb619 | 2015-02-02 03:06:08 +0200 | [diff] [blame] | 1 | import os.path |
| 2 | |
| 3 | |
| 4 | def normalize_dirpath(dirpath): |
| 5 | while dirpath.endswith("/"): |
| 6 | dirpath = dirpath[:-1] |
| 7 | return dirpath |
| 8 | |
| 9 | |
| 10 | def ssh_mkdir(sftp, remotepath, mode=0777, intermediate=False): |
| 11 | remotepath = normalize_dirpath(remotepath) |
| 12 | if intermediate: |
| 13 | try: |
| 14 | sftp.mkdir(remotepath, mode=mode) |
| 15 | except IOError: |
| 16 | ssh_mkdir(sftp, remotepath.rsplit("/", 1)[0], mode=mode, |
| 17 | intermediate=True) |
| 18 | return sftp.mkdir(remotepath, mode=mode) |
| 19 | else: |
| 20 | sftp.mkdir(remotepath, mode=mode) |
| 21 | |
| 22 | |
| 23 | def ssh_copy_file(sftp, localfile, remfile, preserve_perm=True): |
| 24 | sftp.put(localfile, remfile) |
| 25 | if preserve_perm: |
| 26 | sftp.chmod(remfile, os.stat(localfile).st_mode & 0777) |
| 27 | |
| 28 | |
| 29 | def put_dir_recursively(sftp, localpath, remotepath, preserve_perm=True): |
| 30 | "upload local directory to remote recursively" |
| 31 | |
| 32 | assert remotepath.startswith("/"), "%s must be absolute path" % remotepath |
| 33 | |
| 34 | # normalize |
| 35 | localpath = normalize_dirpath(localpath) |
| 36 | remotepath = normalize_dirpath(remotepath) |
| 37 | |
| 38 | try: |
| 39 | sftp.chdir(remotepath) |
| 40 | localsuffix = localpath.rsplit("/", 1)[1] |
| 41 | remotesuffix = remotepath.rsplit("/", 1)[1] |
| 42 | if localsuffix != remotesuffix: |
| 43 | remotepath = os.path.join(remotepath, localsuffix) |
| 44 | except IOError: |
| 45 | pass |
| 46 | |
| 47 | for root, dirs, fls in os.walk(localpath): |
| 48 | prefix = os.path.commonprefix([localpath, root]) |
| 49 | suffix = root.split(prefix, 1)[1] |
| 50 | if suffix.startswith("/"): |
| 51 | suffix = suffix[1:] |
| 52 | |
| 53 | remroot = os.path.join(remotepath, suffix) |
| 54 | |
| 55 | try: |
| 56 | sftp.chdir(remroot) |
| 57 | except IOError: |
| 58 | if preserve_perm: |
| 59 | mode = os.stat(root).st_mode & 0777 |
| 60 | else: |
| 61 | mode = 0777 |
| 62 | ssh_mkdir(sftp, remroot, mode=mode, intermediate=True) |
| 63 | sftp.chdir(remroot) |
| 64 | |
| 65 | for f in fls: |
| 66 | remfile = os.path.join(remroot, f) |
| 67 | localfile = os.path.join(root, f) |
| 68 | ssh_copy_file(sftp, localfile, remfile, preserve_perm) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def copy_paths(conn, paths): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 72 | sftp = conn.open_sftp() |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 73 | try: |
| 74 | for src, dst in paths.items(): |
| 75 | try: |
| 76 | if os.path.isfile(src): |
| 77 | ssh_copy_file(sftp, src, dst) |
| 78 | elif os.path.isdir(src): |
| 79 | put_dir_recursively(sftp, src, dst) |
| 80 | else: |
| 81 | templ = "Can't copy {0!r} - " + \ |
| 82 | "it neither a file not a directory" |
| 83 | msg = templ.format(src) |
| 84 | raise OSError(msg) |
| 85 | except Exception as exc: |
| 86 | tmpl = "Scp {0!r} => {1!r} failed - {2!r}" |
| 87 | msg = tmpl.format(src, dst, exc) |
| 88 | raise OSError(msg) |
| 89 | finally: |
| 90 | sftp.close() |