This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_manager.py
155 lines (108 loc) · 3.64 KB
/
backup_manager.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 *-*
# This file is part of Minecraft Backup Manager
"""
This module contain all functions and class for manage backup
"""
# Minecraft Backup Manager
from minecraft_backup.resources import GAME_PATH
from minecraft_backup.core.configuration import get_os
# PyQt4.QtCore
from PyQt4.QtCore import QThread
from PyQt4.QtCore import SIGNAL
# OS
from os import listdir
from os import mkdir
from os import path
from os import remove
# Shutil
from shutil import copytree
from shutil import copy2
from shutil import copystat
from shutil import rmtree
# Json
from json import dumps
from json import loads
class make_backup_thread(QThread):
"""This class create a Minecraft backup. Inherit of QThread"""
def __init__(self, parent=None):
QThread.__init__(self, parent)
def run(self, dst, backup_name):
"""Start QThread"""
if path.exists(dst):
self.emit(SIGNAL('direxists()'))
self.exit()
else:
self.make_backup(dst, backup_name)
self.exit()
def make_backup(self, dst, backup_name):
"""This function make a Minecraft backup"""
os = get_os()
mkdir(dst)
copy_backup_files(GAME_PATH[os], dst)
self.save_backup_list(backup_name, dst)
self.emit(SIGNAL('makeend()'))
def save_backup_list(self, backup_name, path):
"""This function save backup list"""
self.backup_list = load_backup_list()
if self.backup_list is not False:
self.backup_list[backup_name] = path
save_backup_file(self.backup_list)
else:
self.first_backup_list = {}
self.first_backup_list[backup_name] = path
save_backup_file(self.first_backup_list)
def copy_backup_files(src, dst):
"""Function for copy and remove files"""
names = listdir(src)
for name in names:
src_name = path.join(src, name)
dst_name = path.join(dst, name)
if path.isdir(src_name):
if path.exists(dst_name):
rmtree(dst_name)
copytree(src_name, dst_name)
else:
if path.exists(dst_name):
remove(dst_name)
copy2(src_name, dst_name)
copystat(src, dst)
def save_backup_file(backup_list):
"""Create template and save template in blist.json"""
save_backup_list_file = open('blist.json', 'w')
save_template_list_backup = dumps(backup_list, indent=4, encoding='utf-8')
save_backup_list_file.write(save_template_list_backup)
save_backup_list_file.close()
def load_backup_list():
"""check and load blist.json"""
if path.exists('blist.json'):
list_backup_file = open('blist.json', 'r')
load_backup_file = loads(list_backup_file.read(), encoding='utf-8')
list_backup_file.close()
print load_backup_file
return load_backup_file
else:
return False
def remove_backup_name(backup_name):
"""Remove data of backup and save blis.json"""
backup_list = load_backup_list()
backup_list.pop(backup_name)
save_backup_file(backup_list)
def remove_backup(backup_name):
"""Remove data and backup files"""
backup = unicode(backup_name, 'utf-8')
print backup
backup_list = load_backup_list()
print backup_list[backup]
rmtree(backup_list[backup])
remove_backup_name(backup)
def restore_backup(os, backup_name):
"""This function restore backup"""
backup = str(backup_name)
backup_list = load_backup_list()
dst = os
src = backup_list[backup]
if path.exists(os):
copy_backup_files(src, dst)
else:
mkdir(os)
copy_backup_files(src, dst)