-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbak
executable file
·75 lines (66 loc) · 2.02 KB
/
unbak
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import sys
import argparse
import shutil
import time
import os
import xattr
parser = argparse.ArgumentParser(description='Restore a file created by bak, this is the same as bak -u')
parser.add_argument('Source', help='bak file to restore')
args = parser.parse_args()
duplicate = int(1)
def checkdest(dest, duplicate):
if os.path.exists(dest):
if os.path.exists("%s(%s)" % (dest, duplicate)):
duplicate += 1
dest = checkdest(dest, duplicate)
else:
dest = "%s(%s)" % (dest, duplicate)
return dest
def extrattr(source, setget, dest):
msg = ''
if setget == "set":
try:
xattr.setxattr(source.encode('utf-8'), "user.bak", dest.encode('utf-8'))
except IOError as e:
if not "Operation not supported" in e:
msg = e
else:
msg = "Unbak not supported for this file.\n %s" % e
elif setget == "get":
try:
dest = xattr.getxattr(source.encode('utf-8'), "user.bak")
except IOError as e:
if not "Operation not supported" in e:
msg = e
else:
msg = "Unbak not supported for this file.\n %s" % e
else:
try:
xattr.removexattr(source.encode('utf-8'), "user.bak")
except IOError as e:
if not "Operation not supported" in e:
msg = e
else:
msg = "Unbak not supported for this file.\n %s" % e
return dest, msg
def main(args):
source = args.Source
arglist = list()
if source[-1] == '/':
source = source[:-1]
dest, msg = extrattr(source, "get", '')
if not msg == '':
print(msg)
sys.exit(1)
if os.path.isfile(source):
shutil.copy(source, dest)
elif os.path.isdir(source):
if os.path.isdir(dest):
shutil.rmtree(dest)
shutil.copytree(source, dest)
else:
print("%s does not exist" % source)
sys.exit(1)
sys.exit(0)
main(args)