]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/import_duplicity.py
test: add pylint and test imports
[bup.git] / lib / bup / cmd / import_duplicity.py
1
2 from __future__ import absolute_import
3 from calendar import timegm
4 from subprocess import check_call
5 from time import strptime
6 import os, sys, tempfile
7
8 from bup import git, helpers, options
9 from bup.compat import argv_bytes
10 from bup.helpers import (log,
11                          shstr,
12                          saved_errors)
13 import bup.path
14
15
16 optspec = """
17 bup import-duplicity [-n] <duplicity-source-url> <bup-save-name>
18 --
19 n,dry-run  don't do anything; just print what would be done
20 """
21
22 dry_run = False
23
24 def logcmd(cmd):
25     log(shstr(cmd).decode(errors='backslashreplace') + '\n')
26
27 def exc(cmd, shell=False):
28     logcmd(cmd)
29     if not dry_run:
30         check_call(cmd, shell=shell)
31
32 def exo(cmd, shell=False, preexec_fn=None, close_fds=True):
33     logcmd(cmd)
34     if not dry_run:
35         return helpers.exo(cmd, shell=shell, preexec_fn=preexec_fn,
36                            close_fds=close_fds)[0]
37
38 def redirect_dup_output():
39     os.dup2(1, 3)
40     os.dup2(1, 2)
41
42
43 def main(argv):
44     global dry_run
45
46     log('\nbup: import-duplicity is EXPERIMENTAL (proceed with caution)\n\n')
47
48     o = options.Options(optspec)
49     opt, flags, extra = o.parse_bytes(argv[1:])
50     dry_run = opt.dry_run
51
52     if len(extra) < 1 or not extra[0]:
53         o.fatal('duplicity source URL required')
54     if len(extra) < 2 or not extra[1]:
55         o.fatal('bup destination save name required')
56     if len(extra) > 2:
57         o.fatal('too many arguments')
58
59     source_url, save_name = extra
60     source_url = argv_bytes(source_url)
61     save_name = argv_bytes(save_name)
62     bup_path = bup.path.exe()
63
64     git.check_repo_or_die()
65
66     tmpdir = tempfile.mkdtemp(prefix=b'bup-import-dup-')
67     try:
68         dup = [b'duplicity', b'--archive-dir', tmpdir + b'/dup-cache']
69         restoredir = tmpdir + b'/restore'
70         tmpidx = tmpdir + b'/index'
71
72         collection_status = \
73             exo(dup + [b'collection-status', b'--log-fd=3', source_url],
74                 close_fds=False, preexec_fn=redirect_dup_output)  # i.e. 3>&1 1>&2
75         # Duplicity output lines of interest look like this (one leading space):
76         #  full 20150222T073111Z 1 noenc
77         #  inc 20150222T073233Z 1 noenc
78         dup_timestamps = []
79         for line in collection_status.splitlines():
80             if line.startswith(b' inc '):
81                 assert(len(line) >= len(b' inc 20150222T073233Z'))
82                 dup_timestamps.append(line[5:21])
83             elif line.startswith(b' full '):
84                 assert(len(line) >= len(b' full 20150222T073233Z'))
85                 dup_timestamps.append(line[6:22])
86         for i, dup_ts in enumerate(dup_timestamps):
87             tm = strptime(dup_ts.decode('ascii'), '%Y%m%dT%H%M%SZ')
88             exc([b'rm', b'-rf', restoredir])
89             exc(dup + [b'restore', b'-t', dup_ts, source_url, restoredir])
90             exc([bup_path, b'index', b'-uxf', tmpidx, restoredir])
91             exc([bup_path, b'save', b'--strip', b'--date', b'%d' % timegm(tm),
92                  b'-f', tmpidx, b'-n', save_name, restoredir])
93         sys.stderr.flush()
94     finally:
95         exc([b'rm', b'-rf', tmpdir])
96
97     if saved_errors:
98         log('warning: %d errors encountered\n' % len(saved_errors))
99         sys.exit(1)