]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/import_duplicity.py
Remove Client __del__ in favor of context management
[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 dry_run:
35         return b''
36     return helpers.exo(cmd, shell=shell, preexec_fn=preexec_fn,
37                        close_fds=close_fds)[0]
38
39 def redirect_dup_output():
40     os.dup2(1, 3)
41     os.dup2(1, 2)
42
43
44 def main(argv):
45     global dry_run
46
47     log('\nbup: import-duplicity is EXPERIMENTAL (proceed with caution)\n\n')
48
49     o = options.Options(optspec)
50     opt, flags, extra = o.parse_bytes(argv[1:])
51     dry_run = opt.dry_run
52
53     if len(extra) < 1 or not extra[0]:
54         o.fatal('duplicity source URL required')
55     if len(extra) < 2 or not extra[1]:
56         o.fatal('bup destination save name required')
57     if len(extra) > 2:
58         o.fatal('too many arguments')
59
60     source_url, save_name = extra
61     source_url = argv_bytes(source_url)
62     save_name = argv_bytes(save_name)
63     bup_path = bup.path.exe()
64
65     git.check_repo_or_die()
66
67     tmpdir = tempfile.mkdtemp(prefix=b'bup-import-dup-')
68     try:
69         dup = [b'duplicity', b'--archive-dir', tmpdir + b'/dup-cache']
70         restoredir = tmpdir + b'/restore'
71         tmpidx = tmpdir + b'/index'
72
73         collection_status = \
74             exo(dup + [b'collection-status', b'--log-fd=3', source_url],
75                 close_fds=False, preexec_fn=redirect_dup_output)  # i.e. 3>&1 1>&2
76         # Duplicity output lines of interest look like this (one leading space):
77         #  full 20150222T073111Z 1 noenc
78         #  inc 20150222T073233Z 1 noenc
79         dup_timestamps = []
80         for line in collection_status.splitlines():
81             if line.startswith(b' inc '):
82                 assert(len(line) >= len(b' inc 20150222T073233Z'))
83                 dup_timestamps.append(line[5:21])
84             elif line.startswith(b' full '):
85                 assert(len(line) >= len(b' full 20150222T073233Z'))
86                 dup_timestamps.append(line[6:22])
87         for i, dup_ts in enumerate(dup_timestamps):
88             tm = strptime(dup_ts.decode('ascii'), '%Y%m%dT%H%M%SZ')
89             exc([b'rm', b'-rf', restoredir])
90             exc(dup + [b'restore', b'-t', dup_ts, source_url, restoredir])
91             exc([bup_path, b'index', b'-uxf', tmpidx, restoredir])
92             exc([bup_path, b'save', b'--strip', b'--date', b'%d' % timegm(tm),
93                  b'-f', tmpidx, b'-n', save_name, restoredir])
94         sys.stderr.flush()
95     finally:
96         exc([b'rm', b'-rf', tmpdir])
97
98     if saved_errors:
99         log('warning: %d errors encountered\n' % len(saved_errors))
100         sys.exit(1)