forked from SynologyOpenSource/pkgscripts-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectDepends.py
executable file
·422 lines (330 loc) · 13.4 KB
/
ProjectDepends.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/python3
# Copyright (c) 2000-2020 Synology Inc. All rights reserved.
import sys
import string
import os
import glob
import argparse
import pickle
import pprint
from collections import defaultdict
import re
from include import pythonutils
from include.python import BuildEnv
from config_parser import DependsParser, ProjectDependsParser
ScriptDir = os.path.dirname(os.path.abspath(sys.argv[0]))
# XXX: unified the variable with pythonutils.py
# config file, to get basic project parameters
section_depends = "project dependency"
section_variables = "variables"
config_path = ScriptDir + "/include/project.depends"
libc_project = "uclibc0929"
# Target sections in SynoBuildConf for packages
target_sections = ["BuildDependent", "BuildDependent-Tag"]
target_sections64 = ["BuildDependent64", "BuildDependent-Tag64"]
dynamic_variable = "dynamic variable list"
class DependencyError(Exception):
def __init__(self, stack, proj):
self.stack = stack
self.project = proj
print("Error! Circluar dependency found!!")
def dumpCircluarDepList(self):
blFound = False
for proj in self.stack:
if proj == self.project:
blFound = True
if blFound:
print(proj + " -> ")
print(self.project)
class ProjectNode:
def __init__(self, proj):
# FIXME name
self.proj = proj
self.depends = []
self.rev_depends = []
self.levels = defaultdict(set)
self.addProject(1, [self.proj])
self.success = False
self.visited = False
def updateDependLevel(self, child_node):
for level, projs in child_node.levels.items():
self.addProject(level + 1, projs)
def addProject(self, level, projs):
self.levels[level].update(projs)
def addDependProj(self, child_node):
self.depends.append(child_node)
child_node.rev_depends.append(self)
self.updateDependLevel(child_node)
def getProjectOrder(self):
orders = []
for level, projs in sorted(self.levels.items(), key=lambda x: x[0], reverse=True):
for proj in projs:
if proj not in orders:
orders.append(proj)
return orders
def update_status(self, success):
self.visited = True
self.success = success
def getProjectDepends(self, level):
order = self.getProjectOrder()
if level == 0:
return order
else:
return sorted(self.getLevelsProjects(1, level), key=lambda x: order.index(x))
def getLevelsProjects(self, low, high):
output = set()
for i in range(low, high + 1):
output.update(self.levels[i])
return output
def __repr__(self):
return self.proj
class DepGraph:
# all these attributes are not necessary, just for prototype reference
def __init__(self, dictDepends, direct):
self.root = ProjectNode('DependRoot')
self.root.update_status(True)
self.direct = direct
self.stack = []
self.dictDepends = dictDepends
self.reverseDep = defaultdict(list)
self.created_projects = {}
def getProjectNode(self, proj):
if proj not in self.created_projects:
raise RuntimeError("%s not created." % proj)
return self.created_projects[proj]
def createDepGraph(self, head, listProjs):
for proj in listProjs:
if proj in self.stack:
raise DependencyError(self.stack, proj)
self.stack.append(proj)
if proj in self.created_projects:
proj_node = self.created_projects[proj]
else:
proj_node = self.created_projects[proj] = ProjectNode(proj)
depProj = self.getDepProjList(proj)
if len(depProj) > 0:
self.createDepGraph(proj_node, depProj)
head.addDependProj(proj_node)
self.stack.pop()
def dumpGraph(self, head):
print(head.proj)
pprint.pprint(head.levels)
for p in head.depends:
self.dumpGraph(p)
def traverseDepends(self, listProjs, level):
try:
self.createDepGraph(self.root, listProjs)
# self.dumpGraph(self.root)
except DependencyError as e:
e.dumpCircluarDepList()
sys.exit(1)
output = []
t_level = level if level == 0 else level + 1
output = self.root.getProjectDepends(t_level)
output.remove(self.root.proj)
if self.direct == 'backwardDependency':
output.reverse()
return output
def getReverseDep(self, proj):
reverseList = []
if proj in self.reverseDep:
return self.reverseDep[proj]
for key in self.dictDepends.keys():
if proj in self.dictDepends[key]:
reverseList.append(key)
self.reverseDep[proj] = reverseList
return reverseList
def getTraverseDep(self, proj):
if proj in self.dictDepends:
return self.dictDepends[proj]
return []
def getDepProjList(self, proj):
# -r : Expand project dependency list reversely.
if self.direct == 'backwardDependency':
return self.getReverseDep(proj)
# -x: Traverse all dependant projects
return self.getTraverseDep(proj)
class DepNode:
def __init__(self, proj):
self.nodeName = proj
self.projDependOn = []
self.projDependedBy = []
def dumpNode(self):
print('Project depened on : ' + self.projDependOn)
print('Project depended by : ' + self.projDependedBy)
def dumpProjectDepends(project, dict):
if project in dict:
print()
print("check this line in file '" + config_path + "'")
print(project + ": " + string.join(dict[project]))
# replace project.depends Variable section in project dependency section
# i.e. ${KernelPacks} to synobios
def replaceVariableSection(config, dictDepends):
for var, value in config.variables.items():
# change key
if var in dictDepends:
dictDepends[var] = dictDepends.pop(var)
# change value
for proj in dictDepends:
dictDepends[proj] = [_.replace(var, value[0]) for _ in dictDepends[proj]]
def isKernelHeaderProj(newProj):
pattern_header = re.compile(r"^linux-.*-virtual-headers$")
if pattern_header.match(newProj):
return True
return False
def normalizeProjects(projects, config, kernels):
out_projects = set()
blAddKernelHeader = None
allKernels = config.all_kernels
for proj in projects:
newProj = proj.rstrip("/")
if isKernelHeaderProj(newProj):
newProj = ""
blAddKernelHeader = True
if newProj in allKernels:
out_projects.update(kernels)
continue
elif newProj == libc_project: # always skip libc
newProj = ""
out_projects.add(newProj)
return blAddKernelHeader, list(out_projects)
def findPlatformDependsProj(platformDependsSection, platforms):
listReplaceProj = set()
projects = []
# if there is no platform specified, check for all platforms
if len(platforms) <= 0:
platforms = platformDependsSection.keys()
# get all kernel projects of all specific platforms
for p in platforms:
if p in platformDependsSection:
projects = platformDependsSection[p]
else: # not in platform depends section
try:
projects = platformDependsSection['default']
except KeyError:
print("No such platform : " + p)
sys.exit(1)
for proj in projects:
listReplaceProj.add(proj)
return listReplaceProj
def replaceVariableInDictSection(dictDepends, variable, projsToReplace):
for proj in dictDepends:
listDepProj = dictDepends[proj]
if variable in listDepProj:
for proj in projsToReplace:
listDepProj.insert(listDepProj.index(variable), proj)
listDepProj.remove(variable)
# Replace ${XXXX} in dependency list by [${XXXX}] section
def replacePlatformDependsVariable(dictDepends, platforms, config):
def replaceVariableWithSection(variable, section_name):
try:
dictSection = config.get_section_dict(section_name)
except KeyError:
print("No such section:" + variable)
listPlatformDependsProj = findPlatformDependsProj(dictSection, platforms)
replaceVariableInDictSection(dictDepends, variable, listPlatformDependsProj)
if not config.dynamic_variables:
# for old project depends compatibility
replaceVariableWithSection("${KernelProjs}", pythonutils.SECTION_KERNEL_OLD)
return
for variable in config.dynamic_variables:
replaceVariableWithSection(variable, variable)
def ParseArgs():
parser = argparse.ArgumentParser()
parser.add_argument('-d', dest='display', action='store_true', help='Display (deprecated)')
parser.add_argument('-p', dest='platform', type=str, help='Platform')
parser.add_argument('-r', dest='r_level', type=int, default=-1, help='Reverse depenedency traverse level')
parser.add_argument('-x', dest='level', type=int, default=-1, help="Traverse level")
parser.add_argument('--header', dest='dump_header', default=False, action='store_true', help="Output kernel header")
parser.add_argument('--dump', dest='dump_pickle', help="Dump project depends graph into pickle file.")
group = parser.add_argument_group('Cache')
group.add_argument('--version', type=int, default=0)
group.add_argument('--type', choices=['tag', 'dep', 'both'], default='both')
parser.add_argument('listProj', nargs='*', type=str, help='Replace project list')
return parser.parse_args()
def loadConfigFiles(config):
dictDepends = config.project_depends
confList = glob.glob(ScriptDir + "/../source/*/SynoBuildConf/depends*")
confList = [conf for conf in confList if re.search(r'^depends(-virtual-[-.\w]+)*$', os.path.basename(conf))]
for confPath in confList:
project = confPath.split('/')[-3]
filename = confPath.split('/')[-1]
if BuildEnv.isVirtualProject(filename):
project = BuildEnv.deVirtual(project) + \
BuildEnv.VIRTUAL_PROJECT_SEPARATOR + \
BuildEnv.getVirtualName(filename)
if os.path.isfile(confPath):
depends = DependsParser(confPath)
if project not in dictDepends:
dictDepends[project] = []
dictDepends[project] = list(set(dictDepends[project] + depends.build_dep))
dictDepends[project] = list(set(dictDepends[project] + depends.build_tag))
return dictDepends
# main procedure
if __name__ == "__main__":
# get command line arguments
dictDepends = {}
listProjs = []
listOut = []
blAddKernelHeader = False
direct = 'forwardDependency'
platforms = []
level = -1
dictArgs = ParseArgs()
if dictArgs.platform:
platforms = dictArgs.platform.strip().split(" ")
if dictArgs.level >= 0 and dictArgs.r_level >= 0:
raise RuntimeError("Error! x and r can not use simultaneously")
if dictArgs.level >= 0:
level = dictArgs.level
elif dictArgs.r_level >= 0:
level = dictArgs.r_level
direct = 'backwardDependency'
# Reorder, we need to traverse all depend to sort the input projects.
if dictArgs.level == -1 and dictArgs.r_level == -1:
level = 0
config = ProjectDependsParser(config_path)
dictDepends = loadConfigFiles(config)
listProjs = dictArgs.listProj
kernels = config.get_platform_kernels(platforms)
if listProjs:
dictDepGraph = {}
blAddKernelHeader, normalizedProjList = normalizeProjects(listProjs, config, kernels)
replaceVariableSection(config, dictDepends)
replacePlatformDependsVariable(dictDepends, platforms, config)
depGraph = DepGraph(dictDepends, direct)
listOut = depGraph.traverseDepends(normalizedProjList, level)
if dictArgs.dump_pickle:
sys.setrecursionlimit(3000)
with open(dictArgs.dump_pickle, 'wb') as fd:
pickle.dump(depGraph, fd)
sys.exit(0)
# reorder need filter while args not contain 'x' and 'r'
if dictArgs.level == -1 and dictArgs.r_level == -1:
listReorder = []
for proj in listOut:
if proj in normalizedProjList:
listReorder.append(proj)
listOut = listReorder
if blAddKernelHeader or dictArgs.level >= 0 or dictArgs.dump_header:
listKernelHeader = []
for kernel in kernels:
listKernelHeader.append(kernel + '-virtual-headers')
listOut = listKernelHeader + listOut
strOut = " ".join(listOut)
if len(strOut) > 0:
print(strOut)
elif len(platforms) > 0:
# has platform specified, print kernel version
if len(kernels) == 0:
sys.stderr.write('Error: No matching kernel found!\n')
sys.exit(1)
if blAddKernelHeader or dictArgs.dump_header:
listKernelHeader = []
for kernel in kernels:
listKernelHeader.append(kernel + '-virtual-headers')
print(" ".join(listKernelHeader))
else:
print(" ".join(kernels))
else: # len(listProjs) <= 0
sys.exit(1)