-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
diag.vfpy
252 lines (197 loc) · 8.22 KB
/
diag.vfpy
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
#FLM: TypeRig: Diagnostics utility
# -------------------------------------------------------------------------
# ___ ___ ____
# | | | /\ TypeRig
# |___|___|__/__\
# | | / / (C) Font development kit for FontLab
# |___|/___/ (C) Vassil Kateliev, 2017-2021 (http://www.kateliev.com)
# | |\ \
# |___| \___\ www.typerig.com
#
# -------------------------------------------------------------------------
# (C) Vassil Kateliev, 2023 (http://www.kateliev.com)
# (C) Karandash Type Foundry (http://www.karandash.eu)
#--------------------------------------------------------------------------
# www.typerig.com
# No warranties. By using this you agree
# that you use it at your own risk!
# - Dependencies -----------------
from __future__ import print_function
import os
import sys
import shutil
import re
import sysconfig
import site
import json
from pprint import pprint
from PythonQt import QtCore, QtGui
# - Init --------------------------------
app_name, app_version = 'TR | Module diagnostics', '1.4'
# - Config ------------------------------
module_name = 'typerig'
module_path = 'lib'
file_ext = '.py'
file_skip = 'init'
version_pattern = r"__version__\s*=\s*['\"](.+)['\"]" # match __version__ = "x.y.z"
msg_installed = 'TypeRig Core is <b> Installed </b> on your system!\n\n'
msg_not_installed = 'TypeRig Core is <b> Not Installed </b> on your system!\n\n'
current_folder = os.path.dirname(__file__)
libary_folder = os.path.normpath(os.path.join(current_folder, module_path))
python_folder = site.USER_SITE
if python_folder is None: python_folder = sysconfig.get_paths()['purelib']
# - Functions --------------------------
def rev_path(path):
normalized_path = os.path.normpath(path)
split_path = normalized_path.split(os.sep)
return os.sep.join(reversed(split_path))
def get_path_versions(path):
# - Init
module_versions = []
# - Process
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(file_ext) and file_skip not in file:
file_full_path = os.path.join(root, file)
print('READ:\t' + file_full_path)
with open(file_full_path, encoding="utf-8") as f:
content = f.read()
match = re.search(version_pattern, content)
if match:
version = match.group(1)
module_versions.append((file_full_path, version))
return module_versions
# - Widgets ---------------------
class dlg_diagnose(QtGui.QDialog):
def __init__(self):
super(dlg_diagnose, self).__init__()
# - Init
self.path_core = None
# - Images
img_logo = QtGui.QPixmap(os.path.join(current_folder, 'typerig-logo.png'))
# - Labels
'''
self.lbl_logo = QtGui.QLabel()
self.lbl_logo.setPixmap(img_logo.scaled(200, 200, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
self.lbl_logo.setAlignment(QtCore.Qt.AlignCenter)
'''
self.lbl_desc = QtGui.QLabel('TypeRig Core diagnostics tool:')
# - Edit
self.edt_python_path = QtGui.QLineEdit()
self.edt_python_path.setText(python_folder)
self.edt_core_path = QtGui.QLineEdit()
self.edt_core_path.setText(self.path_core)
# - Table
self.tab_versions = QtGui.QTableWidget()
# - Buttons
self.btn_version_get = QtGui.QPushButton('Get core component versions')
self.btn_version_save = QtGui.QPushButton('Save core component versions')
self.btn_version_diff = QtGui.QPushButton('Compare component versions')
self.btn_unistall = QtGui.QPushButton('Unistall TypeRig')
self.btn_unistall.setEnabled(False)
self.btn_version_get.clicked.connect(lambda: self.version_get())
self.btn_version_save.clicked.connect(lambda: self.version_save())
self.btn_version_diff.clicked.connect(lambda: self.version_load())
#self.btn_unistall.clicked.connect(lambda: self.unistall_core())
# - Build layouts
lay_main = QtGui.QVBoxLayout()
box_info = QtGui.QGroupBox('Installation:')
lay_info = QtGui.QVBoxLayout()
box_actions = QtGui.QGroupBox('Components:')
lay_actions = QtGui.QVBoxLayout()
lay_info.addWidget(QtGui.QLabel('Python installation path:'))
lay_info.addWidget(self.edt_python_path)
lay_info.addWidget(QtGui.QLabel('TypeRig installation path:'))
lay_info.addWidget(self.edt_core_path)
lay_info.addWidget(QtGui.QLabel('Status:'))
lay_info.addWidget(self.lbl_desc)
box_info.setLayout(lay_info)
lay_actions.addWidget(self.btn_version_get)
lay_actions.addWidget(self.btn_version_save)
lay_actions.addWidget(self.btn_version_diff)
lay_actions.addWidget(self.tab_versions)
box_actions.setLayout(lay_actions)
#lay_main.addWidget(self.lbl_logo)
lay_main.addWidget(box_info)
lay_main.addWidget(box_actions)
lay_main.addWidget(self.btn_unistall)
# - Set Widget
self.setLayout(lay_main)
self.setWindowTitle('%s %s' %(app_name, app_version))
self.setGeometry(300, 300, 600, 600)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) # Always on top!!
# - Run
self.module_present()
self.show()
# - Functions -----------------------
def module_present(self):
try:
import typerig
self.lbl_desc.setText(msg_installed)
self.path_core = os.path.dirname(typerig.__file__)
self.edt_core_path.setText(self.path_core)
except ModuleNotFoundError:
self.lbl_desc.setText(msg_not_installed)
self.btn_version_get.setEnabled(False)
self.btn_version_save.setEnabled(False)
self.btn_unistall.setEnabled(False)
self.tab_versions.setEnabled(False)
self.edt_core_path.setEnabled(False)
self.edt_core_path.setText('Module not found!')
# -- Getter/setters -----------------
def set_table(self, data):
name_row, name_column = [], []
self.tab_versions.blockSignals(True)
self.tab_versions.setColumnCount(len(data[0]))
self.tab_versions.setRowCount(len(data))
tab_versions_header = self.tab_versions.horizontalHeader()
tab_versions_header.setDefaultAlignment(QtCore.Qt.AlignLeft)
self.tab_versions.verticalHeader().hide()
self.tab_versions.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
self.tab_versions.setAlternatingRowColors(True)
self.tab_versions.setShowGrid(False)
# - Populate
for row, value in enumerate(data):
name_row.append(value)
for col, key in enumerate(data[row]):
name_column.append(key)
rowData = data[row][col]
newitem = QtGui.QTableWidgetItem(str(rowData))
self.tab_versions.setItem(row, col, newitem)
self.tab_versions.setHorizontalHeaderLabels(('Module path', 'Version'))
self.tab_versions.resizeColumnsToContents()
self.tab_versions.resizeRowsToContents()
def get_table(self):
return [(self.tab_versions.item(row, 0).text(), self.tab_versions.item(row, 1).text()) for row in range(self.tab_versions.rowCount)]
# -- Actions --------------------------------------
def version_get(self):
module_versions = get_path_versions(self.path_core)
self.set_table(module_versions)
def version_save(self):
fname = QtGui.QFileDialog.getSaveFileName(self, 'Save TypeRig Core component version information', os.path.split(__file__)[0], 'JSON (*.json);;')
exported_data = self.get_table()
if not len(exported_data):
self.version_get()
exported_data = self.get_table()
try:
with open(fname, 'w') as export_file:
json.dump(exported_data, export_file)
print('DONE:\tTypeRig Core component version saved to: %s' %fname)
except FileNotFoundError:
print('ABORT:\tNo file selected!')
def version_load(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Load TypeRig Core component version information', os.path.split(__file__)[0], 'JSON (*.json);;')
imported_data = None
try:
with open(fname, 'r') as import_file:
imported_data = json.load(import_file)
print('DONE:\tTypeRig Core component version data loaded from: %s' %fname)
except FileNotFoundError:
print('ABORT:\tNo file selected!')
if imported_data is not None:
current_data = self.get_table()
current_vers = list(sorted([(rev_path(item_path), item_ver) for item_path, item_ver in current_data], key= lambda i: i[0]))
other_vers = list(sorted([(rev_path(item_path), item_ver) for item_path, item_ver in imported_data], key= lambda i: i[0]))
pprint(list(zip(current_vers, other_vers)))
# - RUN ------------------------------
dialog = dlg_diagnose()