-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
executable file
·279 lines (208 loc) · 7.89 KB
/
utils.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
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
#!/usr/bin/python3
import os, argparse
BUNDLE_FILENAME = "bundle.conf"
IMPORTS_FILENAME = "_imports.scss"
THEME_CONFIG_FILENAME = "theme.conf"
EMPTY = ""
UPPER_DIR_INDICATOR = "../"
CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
# Console colors
class colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def read_file(filename: str):
try:
with open(filename, "r") as file:
imports = file.read().splitlines()
return imports
except FileNotFoundError:
print(colors.FAIL + f"File '{filename}' doesn't exist" + colors.ENDC)
exit(2)
### Format methods
def clean(line: str):
line = line.strip("@import '")
line = line.strip("';")
return line
def is_empty(value):
if value == "" or value == None:
return True
else:
return False
def split(line: str):
line = line.strip(UPPER_DIR_INDICATOR)
return [line.split('/widgets/')[0], line.split('/widgets/')[1]]
def filter_theme(pack_theme, theme: str):
# Remove trailing slash before, it causes issues
if theme.endswith("/"):
theme = theme[:-1]
if not is_empty(theme):
if pack_theme == theme:
return True
else:
if "/" in theme:
return filter_theme(pack_theme, theme.split("/")[1])
else:
return False
else:
return True
def filter_widget(pack_widget, widget: str):
if not is_empty(widget):
if pack_widget == widget:
return True
else:
return False
else:
return True
### Logic methods
def parents(theme: str, parent: str, widget: str, skip_widgets: bool):
resultset = get_parents(theme, parent, widget)
parents = resultset[0]
parent_count = len(resultset[1])
# For theme-only list
previous = ''
for item in parents:
if skip_widgets and item[0] != previous:
print(f"- {item[0]}")
previous = item[0]
elif not skip_widgets:
print(f"{item[0]}/widgets/{item[1]}") # parent theme and widget
if skip_widgets:
print(f"Found {parent_count} parents for theme {theme}")
else:
print(f"Found {len(parents)} results over {parent_count} external dependencies")
def get_parents(theme: str, parent: str, widget: str):
imports = get_external_imports(theme)
parent_list = []
ret = []
for pack in imports:
if filter_theme(pack[0], parent) and filter_widget(pack[1], widget):
ret += [pack]
if pack[0] not in parent_list:
parent_list.append(pack[0])
return [ret, parent_list]
def get_external_imports(theme: str):
external_imports = []
imports = read_file(f"{theme}/{IMPORTS_FILENAME}")
for line in imports:
line = clean(line)
if is_external_resource(line):
external_imports += [split(line)]
return external_imports
def get_internal_imports(theme: str):
internal_imports = []
imports = read_file(f"{theme}/{IMPORTS_FILENAME}")
for line in imports:
line = clean(line)
if not is_external_resource(line):
internal_imports += [line.replace("widgets/", EMPTY)]
return internal_imports
def is_external_resource(line: str):
return line.startswith(UPPER_DIR_INDICATOR)
def children(theme: str, widget: str, skip_widgets: bool):
resultset = []
children = []
child_count = 0
resultset = get_children(theme, widget)
children = resultset[0]
child_count = len(resultset[1])
# For theme-only list
previous = ''
for item in children:
if skip_widgets and item[0] != previous:
print(f"- {item[0]}")
previous = item[0]
elif not skip_widgets:
print(f"[{item[0]}] {item[1]}") # parent theme and widget
if skip_widgets:
print(f"Found {child_count} children for theme {theme}")
else:
print(f"Found {len(children)} results over {child_count} children")
def get_children(theme: str, widget: str):
resultset = []
children = []
child_list = []
resultset = get_imports_recursive(EMPTY, subdirs(CURRENT_DIRECTORY))
themes = resultset[0]
imports = resultset[1]
for idx in range(len(themes)):
for pack in imports[idx]:
if filter_theme(pack[0], theme) and filter_widget(pack[1], widget):
children.append([themes[idx], pack[1]])
if themes[idx] not in child_list:
child_list.append(themes[idx])
return [children, child_list]
def get_imports_recursive(prefix: str, folders):
# prefix is the parent folder name + /, if it's not the base folder
themes = []
imports = []
sub_results = []
for DIR in folders:
DIR_PATH = f"{prefix}{DIR}"
if is_theme(DIR_PATH):
themes += [DIR]
imports += [get_external_imports(DIR_PATH)]
if is_bundle(DIR_PATH):
sub_results = get_imports_recursive(f"{DIR_PATH}/", subdirs(DIR_PATH))
themes.extend(sub_results[0])
imports.extend(sub_results[1])
return [themes, imports]
def is_bundle(folder: str):
return os.path.isfile(f"{CURRENT_DIRECTORY}/{folder}/{BUNDLE_FILENAME}")
def is_theme(folder: str):
return os.path.isfile(f"{CURRENT_DIRECTORY}/{folder}/{THEME_CONFIG_FILENAME}")
def subdirs(source: str):
return sorted([name for name in os.listdir(source) if
os.path.isdir(os.path.join(source, name)) and not name.startswith('.')])
def implementations(theme: str):
imports = get_internal_imports(theme)
print(f"Implemented widgets for {theme}")
for item in imports:
print(f"- {item}") # parent theme and widget
print(f"Found {len(imports)} results")
def conflicts(theme: str):
parents = get_parents(theme, EMPTY, EMPTY)[1]
children = get_children(theme, EMPTY)[1]
for theme in parents:
for child in children:
if filter_theme(child, theme) or filter_theme(theme, child):
print(f"CRITICAL: {theme} is also a child!")
return
print("No cross-dependencies found")
# parents(TEST_THEME, TEST_PARENT, TEST_WIDGET)
# children(TEST_THEME, TEST_WIDGET)
# implementations(TEST_THEME)
# conflicts(TEST_THEME)
TEST_THEME = "Win_XP/luna"
TEST_PARENT = "Azurra"
TEST_WIDGET = ""
def main():
parser = argparse.ArgumentParser(description="Azurra Utils, a management script for the Azurra framework")
ops = parser.add_mutually_exclusive_group()
optargs = parser.add_mutually_exclusive_group()
# operational arguments
ops.add_argument("-p", "--parents", type=str, help='Shows external dependencies for <TARGET> theme')
ops.add_argument("-c", "--children", type=str, help='Shows themes depending on <TARGET> theme')
ops.add_argument("-i", "--implementations", type=str, help='Shows unique widgets for <TARGET>')
ops.add_argument("-x", "--conflicts", type=str, help='Detect if theme is cross-dependent on any child')
# optional arguments
optargs.add_argument("-w", "--widget", type=str, help='Filter results to match <WIDGET>')
optargs.add_argument("-l", "--list", help='Show only theme names, skipping widgets', action='store_true')
# pretty args
parser.add_argument("-v", "--version", action='version', version='Azurra Utils, version 1.0',
help='Shows script version and exists')
args = parser.parse_args()
if args.parents:
parents(args.parents, EMPTY, args.widget, args.list)
elif args.children:
children(args.children, args.widget, args.list)
elif args.implementations:
implementations(args.implementations)
elif args.conflicts:
conflicts(args.conflicts)
if __name__ == "__main__": main()