forked from rmeloca/EcosystemsAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateGEXFContextGraph.py
179 lines (170 loc) · 5.71 KB
/
generateGEXFContextGraph.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
import sys
from ecosystemDataManager.ecosystemDataManager import EcosystemDataManager
from ecosystemDataManager.package import Package
from ecosystemDataManager.version import Version
from ecosystemDataManager.dependency import Dependency
from ecosystemDataManager.occurrence import Occurrence
PARENT_VERTICES = []
OCCURRENCE_EDGES = []
DESCENDENT_VERTICES = []
DEPENDENCY_EDGES = []
FILE = None
def getOccurrences(entity):
if type(entity) == Version:
return entity.getOccurrences()
elif type(entity) == Package:
return entity.getPackagesOccurrences()
else:
raise Exception
def getDependencies(entity):
if type(entity) == Version:
return entity.getDependencies()
elif type(entity) == Package:
return entity.getPackagesDependencies()
else:
raise Exception
def getInVersion(entity):
if type(entity) == Dependency or type(entity) == Occurrence:
return entity.getInVersion()
elif type(entity) == Package:
return entity
else:
raise Exception
def generateOccurrences(entity):
if entity in PARENT_VERTICES:
return
PARENT_VERTICES.append(entity)
for occurrence in getOccurrences(entity):
OCCURRENCE_EDGES.append((getInVersion(occurrence), entity))
generateOccurrences(getInVersion(occurrence))
def generateDependencies(entity):
if entity in DESCENDENT_VERTICES:
return
DESCENDENT_VERTICES.append(entity)
for dependency in getDependencies(entity):
DEPENDENCY_EDGES.append((entity, getInVersion(dependency)))
generateDependencies(getInVersion(dependency))
def getAttributes(entity):
attributes = {}
size = 3
if type(entity) == Version:
globalRegularityRate = entity.getGlobalRegularityRate()
else:
globalRegularityRate = 0 if entity.isIrregular() else 1
if (globalRegularityRate == 0):
r = 249
g = 22
b = 22
size = 3.5
elif (globalRegularityRate == 1):
r = 49
g = 249
b = 22
elif (globalRegularityRate <= 0.25):
r = 255
g = 133
b = 20
elif (globalRegularityRate > 0.25 and globalRegularityRate <= 0.75):
r = 255
g = 235
b = 20
elif (globalRegularityRate > 0.75):
r = 20
g = 129
b = 255
attributes["red"] = r
attributes["green"] = g
attributes["blue"] = b
attributes["alpha"] = 0.5
attributes["size"] = size
attributes["shape"] = "disc"
return attributes
"""
Generate a directed graph by package in ecosystem, this graph most be read by Gephi
"""
def generateGraph(entity):
generateDependencies(entity)
generateOccurrences(entity)
FILE.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
FILE.write("<gexf xmlns=\"http://www.gexf.net/1.2draft\" xmlns:viz=\"http://www.gexf.net/1.1draft/viz\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\" version=\"1.2\">")
FILE.write("<graph>")
FILE.write("<nodes>")
FILE.write("<node id=\"" + str(entity) + "\" label=\"" + str(entity) + "\"> <viz:color r=\"22\" g=\"66\" b=\"186\" a=\"1\"/> <viz:size value=\"3\"/> <viz:shape value=\"disc\"/></node>")
for vertex in PARENT_VERTICES:
attributes = getAttributes(vertex)
r = attributes["red"]
g = attributes["green"]
b = attributes["blue"]
size = attributes["size"]
FILE.write("<node id=\"" + str(vertex) + "\" label=\"" + str(vertex) + "\"> <viz:color r=\""+str(r)+"\" g=\""+str(g)+"\" b=\""+str(b)+"\" a=\"0.5\"/> <viz:size value=\""+str(size)+"\"/> <viz:shape value=\"disc\"/></node>")
for vertex in DESCENDENT_VERTICES:
attributes = getAttributes(vertex)
r = attributes["red"]
g = attributes["green"]
b = attributes["blue"]
size = attributes["size"]
FILE.write("<node id=\"" + str(vertex) + "\" label=\"" + str(vertex) + "\"> <viz:color r=\""+str(r)+"\" g=\""+str(g)+"\" b=\""+str(b)+"\" a=\"1\"/> <viz:size value=\""+str(size)+"\"/> <viz:shape value=\"disc\"/></node>")
FILE.write("</nodes>")
FILE.write("<edges>")
i = 0
for edge in OCCURRENCE_EDGES:
r = 249
g = 22
b = 22
if (edge[1].isIrregular()):
r = 49
g = 249
b = 22
FILE.write("<edge id=\"" + str(i) + "\" source=\"" + str(edge[0]) + "\" target=\"" + str(edge[1]) + "\"><viz:color r=\""+str(r)+"\" g=\""+str(g)+"\" b=\""+str(b)+"\" a=\"0.2\"/></edge>")
i += 1
for edge in DEPENDENCY_EDGES:
r = 249
g = 22
b = 22
if (edge[0].isIrregular()):
r = 49
g = 249
b = 22
FILE.write("<edge id=\"" + str(i) + "\" source=\"" + str(edge[0]) + "\" target=\"" + str(edge[1]) + "\"><viz:color r=\""+str(r)+"\" g=\""+str(g)+"\" b=\""+str(b)+"\" a=\"0.6\"/></edge>")
i += 1
FILE.write("</edges>")
FILE.write("</graph>")
FILE.write("</gexf>")
if __name__ == '__main__':
package = None
version = None
if len(sys.argv) < 2:
print("Usage:", sys.argv[0], "<ecosystem> [[package|version] [<package> [<version>]]]")
sys.exit(1)
elif len(sys.argv) > 2:
if sys.argv[2] != "package" and sys.argv[2] != "version":
print("Usage:", sys.argv[0], "<ecosystem> [[package|version] [<package> [<version>]]]")
sys.exit(1)
graphType = sys.argv[2]
if len(sys.argv) > 3:
package = sys.argv[3]
if len(sys.argv) > 4:
version = sys.argv[4]
else:
graphType = "version"
ecosystem = sys.argv[1]
ecosystemDataManager = EcosystemDataManager(ecosystem)
if package:
package = ecosystemDataManager.getPackage(package)
else:
print("no package provided. Retrieving Most Popular")
package = ecosystemDataManager.getMostPopularPackages(1)[0]
if graphType == "version":
if version:
version = package.getVersion(version)
else:
print("no version provided. Retrieving Most Popular")
version = package.getMostPopularVersions(1)[0]
print("generating GEXF to", version)
with open(ecosystem + "_" + package.getName() + "_" + version.getName() + ".gexf", "w") as FILE:
generateGraph(version)
print("done")
else:
with open(ecosystem + "_" + package.getName() + ".gexf", "w") as FILE:
generateGraph(package)
print("done")