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
199 lines (148 loc) · 5.46 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 *-*
#
# This file is part of Minecraft Backup Manager
#
# Minecraft Backup Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# Minecraft Backup Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Minecraft Backup Manager. If not, see <http://www.gnu.org/licenses/>.
#
# Source: https://github.com/LuqueDaniel/Minecraft_backup.git
"""
This module contain all functions and class for manage backup
"""
# Minecraft Backup Manager
from minecraft_backup.resources import GAME_PATH
# 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
# sys
import sys
# 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"""
#Encode in windows-1252 for create folders in Windows
if sys.platform == 'win32':
self.dst = dst_.decode('utf-8').encode('windows-1252')
else:
self.dst = dst_
self.backup_list = load_backup_list()
if self.backup_list is not False:
if unicode(backup_name, 'utf-8') not in self.backup_list:
self.check_exist_directory(backup_name)
else:
self.emit(SIGNAL('nameexists()'))
self.exit()
else:
self.check_exist_directory(backup_name)
def check_exist_directory(self, backup_name):
"""This function checks if exists directory"""
if path.exists(self.dst):
self.emit(SIGNAL('direxists()'))
self.exit()
else:
self.make_backup(self.dst, backup_name)
self.exit()
def make_backup(self, dst, backup_name):
"""This function make a Minecraft backup"""
mkdir(dst)
try:
self.make_backup_continue = True
copy_backup_files(GAME_PATH, dst)
except IOError:
self.make_backup_continue = False
self.emit(SIGNAL('IOdenied()'))
rmtree(dst)
if self.make_backup_continue is True:
self.save_backup_list(backup_name, dst)
self.emit(SIGNAL('makeend()'))
def save_backup_list(self, backup_name, path):
"""This function create and save backup list"""
#Decode for save in utf-8
if sys.platform == 'win32':
self.path = path.decode('windows-1252').encode('utf-8')
else:
self.path = path
self.backup_list = load_backup_list()
if self.backup_list is not False:
self.backup_list[backup_name] = {'name': backup_name,
'path': self.path}
save_backup_file(self.backup_list)
else:
self.first_backup_list = {}
self.first_backup_list[backup_name] = {'name': backup_name,
'path': self.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()
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_list = load_backup_list()
rmtree(backup_list[backup_name]['path'])
remove_backup_name(backup_name)
def restore_backup(backup_name):
"""This function restore backup"""
backup_list = load_backup_list()
src = backup_list[backup_name]['path']
if path.exists(GAME_PATH):
copy_backup_files(src, GAME_PATH)
else:
mkdir(GAME_PATH)
copy_backup_files(src, GAME_PATH)