-
Notifications
You must be signed in to change notification settings - Fork 8
/
iosbackuptool-gui.py
218 lines (177 loc) · 8.78 KB
/
iosbackuptool-gui.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import sys
import os
import tempfile
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QLineEdit, QFileDialog, QComboBox, QProgressBar, QPlainTextEdit, QMessageBox
from PyQt6.QtCore import QThread, pyqtSignal
import subprocess
import re
import py7zr
# Function to strip ANSI escape codes
def strip_ansi_escape_codes(text):
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)
class LogicalBackupWorker(QThread):
progress_updated = pyqtSignal(int)
log_updated = pyqtSignal(str)
backup_finished = pyqtSignal(str)
def __init__(self, command, backup_directory=None):
super().__init__()
self.command = command
self.backup_directory = backup_directory
def run(self):
try:
# Execute the command based on user input
self.execute_command(self.command)
except Exception as e:
self.log_updated.emit(f"Error: {e}")
def execute_command(self, command):
if command == 'backup':
self.backup()
elif command == 'list-devices':
self.list_connected_devices()
def backup(self):
if not self.backup_directory:
self.log_updated.emit("Backup directory not selected.")
return
self.log_updated.emit(f"Starting backup to {self.backup_directory}...")
# Create a temporary directory for the backup process
with tempfile.TemporaryDirectory() as temp_backup_dir:
try:
process = subprocess.Popen(['pymobiledevice3', 'backup2', 'backup', '--full', temp_backup_dir],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True, encoding='utf-8')
total_lines = 100 # Assuming progress is reported in lines as in your example
progress = 0
for line in process.stdout:
self.log_updated.emit(strip_ansi_escape_codes(line.strip()))
# Example: Parsing progress from a line like " 5%|### | 5.0/100.0 [00:05<02:10, 0.5/it]"
if re.search(r'(\d+)%\|\s*.*\|\s*(\d+\.\d+)/(\d+\.\d+)', line):
progress = int(re.search(r'(\d+)%\|\s*.*\|\s*(\d+\.\d+)/(\d+\.\d+)', line).group(1))
self.progress_updated.emit(progress)
# Check if progress has reached 100%
if progress >= 100:
break # Exit loop when progress reaches 100%
process.communicate()
if process.returncode != 0:
self.log_updated.emit(f"Error executing backup command.")
else:
self.log_updated.emit("Backup completed successfully.")
# Create a zip archive from the temporary backup directory
# Find the actual backup folder created by pymobiledevice3
backup_folder_name = None
for item in os.listdir(temp_backup_dir):
if os.path.isdir(os.path.join(temp_backup_dir, item)):
backup_folder_name = item
break
if backup_folder_name:
backup_folder_path = os.path.join(temp_backup_dir, backup_folder_name)
self.log_updated.emit(f"Compressing backup directory to zip file...")
zip_path = os.path.join(self.backup_directory, f"{backup_folder_name}.zip")
# Create a zip archive using py7zr module
with py7zr.SevenZipFile(zip_path, 'w') as archive:
archive.writeall(backup_folder_path, arcname=os.path.basename(backup_folder_path))
self.log_updated.emit(f"Backup directory compressed to {zip_path}.")
self.backup_finished.emit(f"Backup completed and saved to {zip_path}.") # Emit signal when backup is finished
else:
self.log_updated.emit("Error: Backup folder not found.")
except KeyboardInterrupt:
self.log_updated.emit("Backup process aborted by user.")
except subprocess.CalledProcessError as e:
self.log_updated.emit(f"Error executing backup command: {e}")
def list_connected_devices(self):
self.log_updated.emit("Listing connected devices...")
try:
process = subprocess.Popen(['pymobiledevice3', 'usbmux', 'list'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
for line in process.stdout:
self.log_updated.emit(strip_ansi_escape_codes(line.strip()))
_, stderr = process.communicate()
if process.returncode != 0:
self.log_updated.emit(f"Error listing connected devices: {stderr}")
except subprocess.CalledProcessError as e:
self.log_updated.emit(f"Error listing connected devices: {e}")
class LogicalBackupApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Logical Backup Tool")
self.setGeometry(100, 100, 400, 400)
layout = QVBoxLayout()
self.command_label = QLabel("Options:")
self.command_combo = QComboBox()
self.command_combo.addItem("Select Options")
self.command_combo.addItem("backup")
self.command_combo.addItem("list-devices")
self.execute_button = QPushButton("Apply", self)
self.execute_button.setObjectName("blueButton")
self.execute_button.clicked.connect(self.execute_command)
self.backup_label = QLabel("Backup Directory:")
self.backup_path_edit = QLineEdit()
self.browse_button = QPushButton("Browse", self)
self.browse_button.setObjectName("browseButton")
self.browse_button.clicked.connect(self.browse_backup_directory)
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 100)
self.progress_bar.setValue(0)
self.log_box = QPlainTextEdit()
self.log_box.setReadOnly(True)
layout.addWidget(self.command_label)
layout.addWidget(self.command_combo)
layout.addWidget(self.backup_label)
layout.addWidget(self.backup_path_edit)
layout.addWidget(self.browse_button)
layout.addWidget(self.execute_button)
layout.addWidget(self.progress_bar)
layout.addWidget(self.log_box)
self.setLayout(layout)
self.setStyleSheet("""
QPushButton#blueButton, #browseButton {
background-color: #3498db;
border: none;
color: white;
padding: 10px 20px;
font-size: 16px;
border-radius: 4px;
}
QPushButton#blueButton:hover, #browseButton:hover {
background-color: #2980b9;
}
""")
self.worker = None
def browse_backup_directory(self):
backup_directory = QFileDialog.getExistingDirectory(self, "Select Backup Directory")
if backup_directory:
self.backup_path_edit.setText(backup_directory)
def execute_command(self):
command = self.command_combo.currentText()
backup_directory = self.backup_path_edit.text()
if command == "Select Options":
self.show_message("Error", "Please select an option.")
return
if command == "backup" and not backup_directory:
self.show_message("Error", "Please select a backup directory.")
return
self.worker = LogicalBackupWorker(command, backup_directory)
self.worker.log_updated.connect(self.update_log)
self.worker.progress_updated.connect(self.update_progress)
self.worker.backup_finished.connect(self.show_backup_completed_popup) # Connect to backup finished signal
self.worker.start()
def update_log(self, message):
self.log_box.appendPlainText(message)
# Check if the message contains progress information
if re.search(r'\d+%.*', message):
progress = int(re.search(r'\d+', message).group())
self.progress_bar.setValue(progress)
def update_progress(self, progress):
self.progress_bar.setValue(progress)
def show_backup_completed_popup(self, message):
QMessageBox.information(self, "Backup Completed", message)
def show_message(self, title, message):
QMessageBox.information(self, title, message)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = LogicalBackupApp()
window.show()
sys.exit(app.exec())