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
|
# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys
import yaml
import re
from subprocess import run
from PySide6.QtWidgets import QApplication, QWidget, QDialog, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QComboBox, QTextEdit
from PySide6.QtCore import QFile, QDate, QDir
from PySide6.QtUiTools import QUiLoader
class LineDialog(QDialog):
def __init__(self, title, init="", parent=None):
super().__init__(parent)
self.setWindowTitle(title)
self.name_ql = QLineEdit()
self.name_ql.setText(init)
apply_bt = QPushButton("Apply")
layout = QHBoxLayout()
layout.addWidget(self.name_ql)
layout.addWidget(apply_bt)
self.setLayout(layout)
apply_bt.clicked.connect(self.apply)
def apply(self):
self.accept()
class SrcDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Add source")
self.name_ql = QLineEdit()
self.release = QComboBox()
self.release.addItems(["9", "8"])
self.repo = QComboBox()
self.repo.addItems(["core", "nonfree", "tainted"])
apply_bt = QPushButton("Apply")
layout = QHBoxLayout()
layout.addWidget(self.release)
layout.addWidget(self.repo)
layout.addWidget(self.name_ql)
layout.addWidget(apply_bt)
self.setLayout(layout)
apply_bt.clicked.connect(self.apply)
def apply(self):
self.accept()
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.load_ui()
self.setWindowTitle("Mageia advisor")
def load_ui(self):
loader = QUiLoader()
path = Path(__file__).resolve().parent / "form.ui"
ui_file = QFile(path)
ui_file.open(QFile.ReadOnly)
self.ui = loader.load(ui_file, self)
ui_file.close()
self.ui.retrieve_pb.clicked.connect(self.retrieve)
self.ui.add_cve_pb.clicked.connect(self.add_cve)
self.ui.remove_cve_pb.clicked.connect(self.remove_cve)
self.ui.add_src_pb.clicked.connect(self.add_src)
self.ui.remove_src_pb.clicked.connect(self.remove_src)
self.ui.add_ref_pb.clicked.connect(self.add_reference)
self.ui.remove_ref_pb.clicked.connect(self.remove_reference)
self.ui.export_pb.clicked.connect(self.export)
self.ui.cancel_pb.clicked.connect(self.cancel)
self.ui.preview_pb.clicked.connect(self.preview)
self.ui.bug_le.editingFinished.connect(self.valid_number)
# self.populate()
def retrieve(self):
print("Action retrieve")
def add_src(self):
dl = SrcDialog()
name = dl.exec()
self.ui.list_src.addItem(
" ".join((dl.release.currentText(),
dl.repo.currentText(),
self.sanitize_line(dl.name_ql.text())
))
)
def remove_src(self):
self.ui.list_src.takeItem(self.ui.list_src.currentRow())
def add_reference(self):
dl = LineDialog("Add reference")
dl.exec()
self.ui.list_ref.addItem(self.sanitize_line(dl.name_ql.text()))
def remove_reference(self):
self.ui.list_ref.takeItem(self.ui.list_ref.currentRow())
def add_cve(self):
init_value = f"CVE-{QDate.currentDate().year()}-"
dl = LineDialog("Add CVE", init=init_value)
dl.exec()
self.ui.list_cve.addItem(self.sanitize_line(dl.name_ql.text()))
def remove_cve(self):
self.ui.list_cve.takeItem(self.ui.list_cve.currentRow())
def adv_text(self):
data = {}
if self.ui.bugfix_rb.isEnabled():
data['type'] = 'bugfix'
if self.ui.security_rb.isEnabled():
data['type'] = 'security'
if len(self.ui.description_te.toPlainText()) != 0:
data['description'] = self.ui.description_te.toPlainText()
if self.ui.subject_le.text() != "":
data['subject'] = self.sanitize_line(self.ui.subject_le.text())
cves = []
n = 0
for n in range(0, self.ui.list_cve.count()):
cves.append(self.ui.list_cve.item(n).text())
if n != 0:
data['CVE'] = cves
srcs = {}
n = 0
for n in range(0, self.ui.list_src.count()):
release, repo, name = self.ui.list_src.item(n).text().split(" ")
if not release in srcs.keys():
srcs[release] = {}
srcs[release][repo] = []
else:
if not repo in srcs[release].keys():
srcs[release][repo] = []
srcs[release][repo].append(self.sanitize_line(name))
if n != 0:
data['src'] = srcs
refs = []
n = 0
for n in range(0, self.ui.list_ref.count()):
refs.append(self.ui.list_ref.item(n).text())
if n != 0:
data['references'] = refs
return yaml.dump(data, default_flow_style=False, width=75)
def export(self):
if QDir().mkpath(QDir().homePath() + "/mageia-advisories/advisories"):
if self.ui.bug_le.text() != "":
#TODO check that it exists
with open(f"{QDir().homePath()}/mageia-advisories/advisories/{self.ui.bug_le.text()}.adv", 'w') as f:
f.write(self.adv_text())
def cancel(self):
self.close()
def preview(self):
dl = QDialog()
dl.setWindowTitle("Advisory preview")
te = QTextEdit()
te.setPlainText(self.adv_text())
te.setReadOnly(True)
te.setMinimumSize(600, 0)
ok_bt = QPushButton("OK")
layout = QVBoxLayout()
layout.addWidget(te)
layout.addWidget(ok_bt)
dl.setLayout(layout)
ok_bt.clicked.connect(dl.close)
dl.exec()
def sanitize_line(self, line):
if len(line) != 0:
return line.splitlines()[0].strip()
else:
return ""
def valid_number(self):
self.ui.bug_le.setText(re.sub('\D', '', self.ui.bug_le.text()))
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec())
|