PageRenderTime 202ms CodeModel.GetById 81ms app.highlight 9ms RepoModel.GetById 51ms app.codeStats 0ms

/silversupport/transfermethods.py

https://bitbucket.org/ianb/silverlining/
Python | 86 lines | 70 code | 16 blank | 0 comment | 18 complexity | c966310e00154d37c179ef75a66bfe53 MD5 | raw file
 1import os
 2from silversupport.shell import run
 3import warnings
 4warnings.filterwarnings('ignore', r'tempnam is a potential security risk')
 5
 6
 7def local(dir, dest):
 8    if is_archive(dest):
 9        archive(dir, dest)
10    else:
11        run(['cp', '-ar', dir, dest])
12
13
14def ssh(dir, dest):
15    if is_archive(dest):
16        tmp = make_temp_name(dest)
17        try:
18            archive(dir, tmp)
19            run(['scp', '-p', tmp, dest])
20        finally:
21            if os.path.exists(tmp):
22                os.unlink(tmp)
23    else:
24        run(['scp', '-rp', dir, dest])
25
26
27def rsync(dir, dest):
28    if is_archive(dest):
29        tmp = make_temp_name(dest)
30        try:
31            archive(dir, tmp)
32            run(['rsync', '-t', dir, dest])
33        finally:
34            if os.path.exists(tmp):
35                os.unlink(tmp)
36    else:
37        run(['rsync', '-rt', dir, dest])
38
39
40def make_temp_name(dest):
41    name = os.tempnam() + extension(dest)
42    return name
43
44
45def is_archive(name):
46    return extension(name) in ('.zip', '.tar.gz', '.tar.bz2', '.tgz', 'tbz2')
47
48
49def extension(name):
50    path, ext = os.path.splitext(name)
51    if ext in ('.gz', '.bz2'):
52        ext = os.path.splitext(path)[1] + ext
53    return ext
54
55
56def archive(dir, dest):
57    ext = extension(dest)
58    if ext == '.zip':
59        run(['zip', '-r', '--symlinks', dest, dir])
60    elif ext in ('.tar.gz', '.tgz', '.tar.bz2', '.tbz2'):
61        if ext in ('.tar.gz', '.tgz'):
62            op = 'z'
63        else:
64            op = 'j'
65        run(['tar', op + 'fc', dest, '.'],
66            cwd=dir)
67    else:
68        assert 0, 'unknown extension %r' % ext
69
70
71def unarchive(archive, dest):
72    if not os.path.exists(dest):
73        os.makedirs(dest)
74    ext = extension(dest)
75    archive = os.path.abspath(archive)
76    if ext == '.zip':
77        run(['unzip', archive], cwd=dest)
78    elif ext in ('.tar.gz', '.tgz', '.tar.gz2', '.tbz2'):
79        if ext in ('.tar.gz', '.tgz'):
80            op = 'z'
81        else:
82            op = 'j'
83        run(['tar', op+'fx', archive],
84            cwd=dest)
85    else:
86        assert 0, 'unknown extension: %r' % ext