-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxerolinux-rollback
executable file
·300 lines (262 loc) · 11.4 KB
/
xerolinux-rollback
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env -S python
import os
import sys
import subprocess as sp
from qtpy.QtWidgets import *
from qtpy.QtGui import *
from qtpy.QtCore import *
import psutil
# Current version
current_version='0.1.34'
class Version:
@staticmethod
def getAppName():
return 'Xerolinux Rollback Utility'
@staticmethod
def getVersion():
return current_version
class Partition:
@staticmethod
def isSnapperPresentAndConfigured():
retValue = False
outputStatus = sp.getstatusoutput('which snapper')
if outputStatus[0] == 0:
# verifify if config ok
outputStatus = sp.getstatusoutput('snapper list')
if outputStatus[0] == 0:
retValue = True
return retValue
@staticmethod
def getFsType():
partitions = psutil.disk_partitions()
fstype = 'unknow'
for partition in partitions:
if partition.mountpoint == '/':
fstype = partition.fstype
break
return fstype
@staticmethod
def getRootPartition():
partitions = psutil.disk_partitions()
print('Querying root partition...')
retValue = None
for partition in partitions:
if partition.mountpoint == '/':
print('Found!')
retValue = partition.device
return retValue
class AboutBox(QDialog):
def __init__(self, parent=None):
super(AboutBox, self).__init__(parent)
self.setFixedWidth(480)
self.centerMe()
self.setWindowTitle(f'{Version.getAppName()} v. {Version.getVersion()}')
alignCenter = Qt.AlignHCenter
self.okButton = QDialogButtonBox(QDialogButtonBox.Ok)
self.okButton.accepted.connect(self.hideMe)
self.layout = QVBoxLayout()
# ------------------------------------------------------------------------------------------
# Logo
self.logoLabel = QLabel()
self.logoLabel.setPixmap(QPixmap('/usr/share/xerolinux-rollback-git/images/xerolinux-logo.png'))
self.layout.addWidget(self.logoLabel)
self.logoLabel.setAlignment(alignCenter)
# ------------------------------------------------------------------------------------------
# text about box
xerolinux_rollbackLabel = QLabel(f'<strong>{Version.getAppName()}</strong>')
xerolinux_rollbackLabel.setTextFormat(Qt.RichText)
xerolinux_rollbackLabel.setAlignment(alignCenter)
self.layout.addWidget(xerolinux_rollbackLabel)
versionLabel = QLabel(f'Version <strong>{Version.getVersion()}</strong>')
versionLabel.setTextFormat(Qt.RichText)
versionLabel.setAlignment(alignCenter)
self.layout.addWidget(versionLabel)
# contributors text
contHeader = QLabel('Contributors')
contHeader.setAlignment(Qt.AlignHCenter)
cont1 = QLabel('<a href="https://www.youtube.com/c/TheDuckChannel">The Duck Channel</a>')
cont1.setAlignment(Qt.AlignHCenter)
cont2 = QLabel('<a href="https://github.com/theduckchannel">Fred Junior</a>')
cont2.setAlignment(Qt.AlignHCenter)
self.layout.addWidget(contHeader)
self.layout.addWidget(cont1)
self.layout.addWidget(cont2)
# warrantyText
textAboutLabel = QLabel('This program comes with absolutely no warranty')
textAboutLabel.setWordWrap(True)
textAboutLabel.setTextFormat(Qt.RichText)
textAboutLabel.setAlignment(alignCenter)
self.layout.addWidget(textAboutLabel)
urlLink = '<a href=\"https://github.com/theduckchannel/xerolinux-rollback/blob/master/LICENSE\">https://github.com/theduckchannel/xerolinux-rollback/blob/master/LICENSE</a>'
urlLabel = QLabel(urlLink)
urlLabel.setAlignment(alignCenter)
urlLabel.setOpenExternalLinks(True)
urlLabel.setWordWrap(True)
self.layout.addWidget(urlLabel)
self.layout.addSpacing(30)
self.layout.addWidget(self.okButton, alignment=Qt.AlignHCenter)
self.setLayout(self.layout)
def centerMe(self):
screenGeo = QApplication.desktop().screenGeometry()
x = (screenGeo.width() - self.width()) / 2
y = (screenGeo.height() - self.height()) / 2
self.move(int(x), int(y))
def hideMe(self):
self.hide()
# Display an Alert
class Alert(QDialog):
def __init__(self, title, message):
super(Alert, self).__init__()
self.setWindowTitle(title)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok)
self.buttonBox.accepted.connect(self.accept)
messageLabel = QLabel(message)
self.layout = QVBoxLayout()
self.layout.addWidget(messageLabel)
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
class mainWindow(QMainWindow):
horHeaders = ['ID', 'Type', 'Date', 'User', 'Cleanup', 'Description']
commands = {
'snapper-list': f"snapper list | sed '1,3d'",
'konsole-rollback': f"konsole --hide-menubar --hide-tabbar -e rollback-frontend"
}
app = QApplication(sys.argv)
snapshotsTableWidget = QTableWidget()
aboutBox = AboutBox()
def __init__(self):
super().__init__(parent=None)
self.setWindowTitle(f'Xerolinux Rollback Utility v. {Version.getVersion()}')
self.setFixedSize(QSize(1024, 768))
###################################################
# Vertical Layout ###
verticalLayout = QVBoxLayout()
verticalLayout.setAlignment(Qt.AlignTop)
####
# Horizontal Layout Logo and QLabel for message
topHorizontalLayout = QHBoxLayout()
####
# Xerolinux Logotype Label
xerolinuxLabel = QLabel(self)
pixmap = QPixmap('/usr/share/xerolinux-rollback-git/images/xerolinux-logo.png')
xerolinuxLabel.setPixmap(pixmap)
# xerolinuxLabel.setStyleSheet("background-color: red")
xerolinuxLabel.setFixedWidth(96)
topHorizontalLayout.addWidget(xerolinuxLabel)
####
# Info Label
infoLabel = QLabel("Select the snapshot from the list below and click the rollback button.")
# infoLabel.setStyleSheet("background-color: red")
infoLabel.setFont(QFont('Fira Code', 14))
infoLabel.setWordWrap(True)
topHorizontalLayout.addWidget(infoLabel)
####
# aboutPushButton
aboutPushButton = QPushButton()
aboutPushButton.setIcon(QIcon('/usr/share/xerolinux-rollback-git/images/xerolinux-about.png'))
aboutPushButton.setIconSize(QSize(32, 32))
aboutPushButton.setFixedSize(QSize(42, 42))
# aboutPushButton.setStyleSheet("background-color: rgba(255, 255, 255, 0);")
aboutPushButton.setCursor(QCursor(Qt.PointingHandCursor))
aboutPushButton.clicked.connect(self.showAboutBox)
topHorizontalLayout.addWidget(aboutPushButton)
###
verticalLayout.addLayout(topHorizontalLayout)
############
# Snapshots List QTableWidget
self.snapshotsTableWidget.verticalHeader().hide()
verticalLayout.addWidget(self.snapshotsTableWidget)
self.snapshotsTableWidget.setColumnCount(6)
self.snapshotsTableWidget.setHorizontalHeaderLabels(self.horHeaders)
self.snapshotsTableWidget.setEditTriggers(QTableWidget.NoEditTriggers)
self.snapshotsTableWidget.setSelectionMode(QAbstractItemView.SingleSelection)
############
# Two PushButtons (Rollback and Exit)
rollbackPushButton = QPushButton("&Rollback")
rollbackPushButton.setFixedHeight(32)
# rollbackPushButton.setIcon(QIcon(f'{FileUtil.getResourcePath()}/images/rollback.png'))
# rollbackPushButton.setIconSize(QSize(24, 24))
rollbackPushButton.clicked.connect(self.rollback)
rollbackPushButton.setFont(QFont('Fira Code', 12))
exitPushButton = QPushButton("&Exit")
exitPushButton.setFixedHeight(32)
# exitPushButton.setIcon(QIcon(f'{FileUtil.getResourcePath()}/images/exit.png'))
exitPushButton.setFont(QFont('Fira Code', 12))
exitPushButton.clicked.connect(self.exitApp)
bottonHorizontalLayout = QHBoxLayout()
bottonHorizontalLayout.addWidget(rollbackPushButton)
bottonHorizontalLayout.addWidget(exitPushButton)
verticalLayout.addLayout(bottonHorizontalLayout)
# Window Icon
self.setWindowIcon(QIcon('/usr/share/xerolinux-rollback-git/images/xerolinux-logo.png'))
#######
# Set the central widget of the Window.
centralWidget = QWidget()
centralWidget.setLayout(verticalLayout)
self.setCentralWidget(centralWidget)
############
if Partition.getFsType()!='btrfs':
Alert('Error!', 'This app is made for btrfs filesystems only!').exec()
self.exitApp()
print('passou por aqui')
if not Partition.isSnapperPresentAndConfigured():
Alert('Error!', 'You don´t have any snapshots. Did you configure snapper to fire snapshots automatically?').exec()
self.exitApp()
else:
self.show()
self.refreshSnapshotsList()
sys.exit(self.app.exec())
def exitApp(self):
self.app.quit()
sys.exit(0)
def rollback(self):
index = self.snapshotsTableWidget.selectionModel().currentIndex()
snapshotID = index.sibling(index.row(), 0).data()
rootPartition = Partition.getRootPartition()
print(f'Snapshot ID: {snapshotID}')
print(f'Root Partition: {rootPartition}')
sp.getoutput(f'{self.commands["konsole-rollback"]} {snapshotID} {rootPartition}')
def refreshSnapshotsList(self):
lines = self.getSnapshotLines()
# print(lines)
self.snapshotsTableWidget.setRowCount(len(lines))
for idx, line in enumerate(lines):
col = line.split('|')
# ID
idItem = QTableWidgetItem(col[0].rstrip())
self.snapshotsTableWidget.setItem(idx, 0, idItem)
# Type
typeItem = QTableWidgetItem(col[1].rstrip())
self.snapshotsTableWidget.setItem(idx, 1, typeItem)
# Date
dateItem = QTableWidgetItem(col[3].rstrip())
self.snapshotsTableWidget.setItem(idx, 2, dateItem)
# User
userItem = QTableWidgetItem(col[4].rstrip())
self.snapshotsTableWidget.setItem(idx, 3, userItem)
# Cleanup
cleanupItem = QTableWidgetItem(col[5].rstrip())
self.snapshotsTableWidget.setItem(idx, 4, cleanupItem)
# Description
descriptionItem = QTableWidgetItem(col[6].rstrip())
self.snapshotsTableWidget.setItem(idx, 5, descriptionItem)
header = self.snapshotsTableWidget.horizontalHeader()
header.setSectionResizeMode(QHeaderView.ResizeToContents)
header.setSectionResizeMode(5, QHeaderView.Stretch)
self.snapshotsTableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
if len(lines) > 0:
self.snapshotsTableWidget.selectRow(0)
def getSnapshotLines(self):
output = sp.getoutput(self.commands['snapper-list'])
lines = output.splitlines()
return lines
def clearData(self):
for header in self.data:
self.data[header].clear()
def showAboutBox(self):
self.aboutBox.show()
if __name__ == '__main__':
os.environ['QT_DEBUG_PLUGINS'] = '1'
os.environ['QT_LOGGING_RULES'] = "qt5ct.debug=false"
os.environ['QT_API'] = 'pyqt5'
window = mainWindow()