-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinit.py
43 lines (30 loc) · 1.09 KB
/
init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
import re
from restic.internal import command_executor
def run(restic_base_command,
copy_chunker_params=False,
from_repo=None,
from_password_file=None,
repository_version=None):
cmd = restic_base_command + ['init']
if copy_chunker_params:
cmd.append('--copy-chunker-params')
if from_repo:
cmd.extend(['--from-repo', from_repo])
if from_password_file:
cmd.extend(['--from-password-file', from_password_file])
if repository_version:
cmd.extend(['--repository-version', str(repository_version)])
return _parse_result(command_executor.execute(cmd))
def _parse_result(result):
try:
return json.loads(result)['id']
except json.decoder.JSONDecodeError:
return _parse_plaintext_result(result)
def _parse_plaintext_result(result):
"""Parse legacy plaintext result.
Prior to restic 0.15.0, restic returned a plaintext response even when the
caller specified --json.
"""
return re.match(r'created restic repository ([a-z0-9]+) at .+',
result).group(1)