-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
executable file
·115 lines (95 loc) · 2.98 KB
/
backup.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python3
import argparse
import os
import sys
import gui
import util
from user import User
from backuppers import BACKUPPERS
from backupper import Backupper
__version__ = "1.9"
def main(action: str, game_name: str = None, dry: bool = False):
if not os.path.exists("./user.json"):
print("user.json not found, please create")
return False
user = User()
if not user.load():
print("[Backup] User load error")
return False
Backupper.dry_run = dry
Backupper.alt_save_location = user.alt_save_location
if dry:
print("[Backup] Dry-run enabled, not making any changes")
if action == "version":
print(f"Backup.py version {__version__}")
return True
elif action == "printuser":
print(user)
return True
elif action == "backup":
print("[Backup] Back up started")
print(user)
backupper_classes = []
for game in user.paths.keys():
if game in BACKUPPERS:
backupper_classes.append(BACKUPPERS[game])
print(f"[Backup] Available backuppers for this computer: {backupper_classes}")
backupper_objects = []
for cls in backupper_classes:
try:
backupper_objects.append(cls(user.paths, user.machine_name, user.config))
except TypeError:
backupper_objects.append(cls(user.paths, user.machine_name))
if game_name is not None:
game_backupper = None
for obj in backupper_objects:
if obj.game_name == game_name:
game_backupper = obj
break
if game_backupper is None:
print(
f"[Backup] Could not find backupper for game '{game_name}' or game not registered in user.json"
)
return False
else:
game_backupper.backup()
print("[Backup] Single-game backup done")
return True
for obj in backupper_objects:
obj.backup()
print("[Backup] All-game backup done")
return True
elif action == "gui":
gui.start_gui()
return True
return True
def set_wd():
script_path = os.path.abspath(__file__)
script_dir = os.path.dirname(script_path)
os.chdir(script_dir)
def entry():
parser = argparse.ArgumentParser()
parser.add_argument(
"action",
type=str,
choices=["version", "backup", "printuser", "gui"],
help="what to do",
)
parser.add_argument(
"--game",
"-g",
type=str,
help="when 'backup' action, only backup the specified game",
)
parser.add_argument(
"--dry",
"-d",
action="store_true",
help="when 'backup' action, don't do any actual changes"
)
args = parser.parse_args()
set_wd()
if not main(args.action, args.game, args.dry):
sys.exit(1)
if __name__ == "__main__":
entry()