/
edit_pom.py
416 lines (376 loc) · 16.9 KB
/
edit_pom.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
import xml.etree.ElementTree as ET
from xml.dom import minidom
import sys
namespaces = {'xmlns' : 'http://maven.apache.org/POM/4.0.0'}
ET.register_namespace('','http://maven.apache.org/POM/4.0.0')
elem_prefix="{http://maven.apache.org/POM/4.0.0}"
arg_agent='''-javaagent:/home/peipei/RepoReaper/AssistInstrumentation/target/javassist-instrument-1.jar='''
xml_agent='''<!-- executes maven test with -javaagent option -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<skipTests>false</skipTests>
<argLine>-javaagent:target/${project.build.finalName}.jar=testUserLog</argLine>
</configuration>
</plugin>'''
xml_javassist='''<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.14.0-GA</version>
<type>jar</type>
</dependency>'''
#ET.register_namespace('', "http://www.opengis.net/wmts/1.0")
#ET.register_namespace('ows', "http://www.opengis.net/ows/1.1")
#ET.register_namespace('xlink', "http://www.w3.org/1999/xlink")
#ET.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")
#ET.register_namespace('gml', "http://www.opengis.net/gml")
def print_subtree(subtree):
for y in subtree:
print "\t", y.tag, ":", y.text
#for x in root:
# print x.tag, x.attrib
# print_subtree(x.getchildren())
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ET.tostring(elem, 'utf-8')
#rough_string = ET.tostring(elem)
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def updateDeps(dependency,groupId,artifactId,version,scope):
EgroupId=dependency.find(elem_prefix,"groupId")
if EgroupId is None:
EgroupId=ET.SubElement(dependency,"groupId")
EgroupId.text=groupId
EartifactId=dependency.find(elem_prefix,"artifactId")
if EartifactId is None:
EartifactId=ET.SubElement(dependency,"artifactId")
EartifactId.text=artifactId
Eversion=dependency.find(elem_prefix,"version")
if Eversion is None:
Eversion=ET.SubElement(dependency,"version")
Eversion.text=version
Escope=dependency.find(elem_prefix,"scope")
if Escope is None:
Escope=ET.SubElement(dependency,"scope")
Escope.text=scope
#
def addDependency(dependencies,groupId,artifactId,version,scope):
dependency=ET.SubElement(dependencies,"dependency")
EgroupId=ET.SubElement(dependency,"groupId")
EgroupId.text=groupId
EartifactId=ET.SubElement(dependency,"artifactId")
EartifactId.text=artifactId
Eversion=ET.SubElement(dependency,"version")
Eversion.text=version
Escope=ET.SubElement(dependency,"scope")
Escope.text=scope
def addSurefirePlugin(plugins,args_agent):
plugin=ET.SubElement(plugins,"plugin")
groupId=ET.SubElement(plugin,"groupId")
groupId.text="org.apache.maven.plugins"
artifactId=ET.SubElement(plugin,"artifactId")
artifactId.text="maven-surefire-plugin"
version=ET.SubElement(plugin,"version")
version.text="2.14"
configuration=ET.SubElement(plugin,"configuration")
skipTests=ET.SubElement(configuration,"skipTests")
skipTests.text="false"
argLine=ET.SubElement(configuration,"argLine")
argLine.text=args_agent
def addAgentProfile(profiles,args_agent):
profile=ET.SubElement(profiles,"profile")
#<id>agent</id>
#<activation>
# <activeByDefault>true</activeByDefault>
#</activation>
id=ET.SubElement(profile,"id")
id.text="agent"
activation=ET.SubElement(profile,"activation")
activationByDefault=ET.SubElement(activation,"activeByDefault")
activationByDefault.text="true"
#<build>
# <plugins>
# <plugin>
# <groupId>org.apache.maven.plugins</groupId>
# <artifactId>maven-surefire-plugin</artifactId>
# <version>2.14</version>
# <configuration>
# <skipTests>false</skipTests>
# <argLine>-javaagent:/home/peipei/RepoReaper/AssistInstrumentation/target/javassist-instrument-1.jar=7bk</argLine>
# </configuration>
# </plugin>
# </plugins>
#</build>
build=ET.SubElement(profile,"build")
plugins=ET.SubElement(build,"plugins")
addSurefirePlugin(plugins,args_agent)
dependencies=ET.SubElement(profile,"dependencies")
addDependency(dependencies,"org.hamcrest","hamcrest-core","1.3","test")
#<dependencies>
# <dependency>
# <groupId>org.hamcrest</groupId>
# <artifactId>hamcrest-core</artifactId>
# <version>1.3</version>
# <scope>test</scope>
# </dependency>
# <dependency>
# <groupId>org.hamcrest</groupId>
# <artifactId>hamcrest-library</artifactId>
# <version>1.3</version>
# <scope>test</scope>
# </dependency>
addDependency(dependencies,"org.hamcrest","hamcrest-library","1.3","test")
# <dependency>
# <groupId>org.mockito</groupId>
# <artifactId>mockito-all</artifactId>
# <version>1.9.5</version>
# <scope>test</scope>
# </dependency>
addDependency(dependencies,"org.mockito","mockito-all","1.9.5","test")
# <dependency>
# <groupId>junit</groupId>
# <artifactId>junit</artifactId>
# <version>4.11</version>
# <scope>test</scope>
# </dependency>
addDependency(dependencies,"junit","junit","4.11","test")
# <dependency>
# <groupId>org.ow2.asm</groupId>
# <artifactId>asm</artifactId>
# <version>4.1</version>
# </dependency>
addDependency(dependencies,"org.ow2.asm","asm","4.1","test")
# <dependency>
# <groupId>org.javassist</groupId>
# <artifactId>javassist</artifactId>
# <version>3.14.0-GA</version>
# <type>jar</type>
# </dependency>
addDependency(dependencies,"org.javassist","javassist","3.14.0-GA","test")
# </dependencies>
def updateSurefire(surefire_plugin,args_agent):
args_CONF=surefire_plugin.find(elem_prefix+"configuration")
if args_CONF is not None:
args_JVM=args_CONF.find(elem_prefix+"argLine")
if args_JVM is not None:
args_JVM.text=args_agent+" "+args_JVM.text
else:##argLine is missing
args_JVM=ET.SubElement(args_CONF,"argLine")
args_JVM.text=args_agent
else: ##argCONF is missing
args_CONF=ET.SubElement(surefire_plugin,"configuration")
args_JVM=ET.SubElement(args_CONF,"argLine")
args_JVM.text=args_agent
def updateProfile(profile,args_agent):
##check if surefire exits
build=profile.find(elem_prefix+"build")
if build is not None:
plugins=build.find(elem_prefix+"plugins")
if plugins is not None:
##check if surefire is lists
is_surefire_plugin=False
surefire_plugin=None
for plugin in plugins.findall(elem_prefix+"plugin"):
artifactId=plugin.find(elem_prefix+"artifactId")
if artifactId is not None and artifactId.text=="maven-surefire-plugin":
surefire_plugin=True
surefire_plugin=plugin
break
if is_surefire_plugin:
updateSurefire(surefire_plugin,args_agent)
else:
plugins=ET.SubElement(build,"plugins")
addSurefirePlugin(plugins,args_agent)
else:
build=ET.SubElement(profile,"build")
plugins=ET.SubElement(build,"plugins")
addSurefirePlugin(plugins,args_agent)
##check dependencies
dependencies=profile.find(elem_prefix+"dependencies")
if dependencies is None:
dependencies=ET.SubElement(profile,"dependencies")
is_javassist=False
is_hamcrest_core=False
is_hamcrest_lib=False
is_junit=False
is_asm=False
is_mockito=False
for dependency in dependencies.findall(elem_prefix+"dependency"):
artifactId=dependency.find(elem_prefix+"artifactId")
groupId=dependency.find(elem_prefix+"groupId")
if groupId and artifactId:
if "org.hamcrest"==groupId and "hamcrest-core"==artifactId:
is_hamcrest_core=True
updateDeps(dependency,"org.hamcrest","hamcrest-core","1.3","test")
elif "org.hamcrest"==groupId and "hamcrest-library"==artifactId:
is_hamcrest_lib=True
updateDeps(dependency,"org.hamcrest","hamcrest-library","1.3","test")
elif "org.mockito"==groupId and "mockito-all"==artifactId:
is_mockito=True
updateDeps(dependency,"org.mockito","mockito-all","1.9.5","test")
elif "junit"==groupId and "junit"==artifactId:
is_junit=True
updateDeps(dependency,"junit","junit","4.11","test")
elif "org.ow2.asm"==groupId and "asm"==artifactId:
is_asm=True
updateDeps(dependency,"org.ow2.asm","asm","4.1","test")
elif "org.javassist"==groupId and "javassist"==artifactId:
is_javassist=True
updateDeps(dependency,"org.javassist","javassist","3.14.0-GA","test")
elif artifactId:
if "hamcrest-core"==artifactId:
is_hamcrest_core=True
updateDeps(dependency,"org.hamcrest","hamcrest-core","1.3","test")
elif "hamcrest-library"==artifactId:
is_hamcrest_lib=True
updateDeps(dependency,"org.hamcrest","hamcrest-library","1.3","test")
elif "mockito-all"==artifactId:
is_mockito=True
updateDeps(dependency,"org.mockito","mockito-all","1.9.5","test")
elif "junit"==artifactId:
is_junit=True
updateDeps(dependency,"junit","junit","4.11","test")
elif "asm"==artifactId:
is_asm=True
updateDeps(dependency,"org.ow2.asm","asm","4.1","test")
elif "javassist"==artifactId:
is_javassist=True
updateDeps(dependency,"org.javassist","javassist","3.14.0-GA","test")
if not is_hamcrest_core:
addDependency(dependencies,"org.hamcrest","hamcrest-core","1.3","test")
if not is_hamcrest_lib:
addDependency(dependencies,"org.hamcrest","hamcrest-library","1.3","test")
if not is_mockito:
addDependency(dependencies,"org.mockito","mockito-all","1.9.5","test")
if not is_junit:
addDependency(dependencies,"junit","junit","4.11","test")
if not is_asm:
addDependency(dependencies,"org.ow2.asm","asm","4.1","test")
if not is_javassist:
addDependency(dependencies,"org.javassist","javassist","3.14.0-GA","test")
def agent_pom(pom_file,proj_name):
print "pom_file: ", pom_file
print "proj_name: ", proj_name
tree = ET.parse(pom_file)
root = tree.getroot()
#proj_name = root.find("name",namespaces=namespaces).text
args_agent=arg_agent+proj_name
##Step 1 Check if pom.xml has profiles or not;if profiles exist, update all profiles
profiles=root.find(elem_prefix+"profiles")
if profiles is None:
profiles=ET.SubElement(root,"profiles")
addAgentProfile(profiles,args_agent)
else:
isActivationDefault=False
# defaultProfile=None
for profile in profiles.findall(elem_prefix+"profile"):
activation=profile.find(elem_prefix+"activation")
if activation is not None:
activationByDefault=activation.find(elem_prefix+"activeByDefault")
if activationByDefault is not None and activationByDefault.text=="true":
isActivationDefault=True
# defaultProfile=profile
updateProfile(profile,args_agent)
if not isActivationDefault:
addAgentProfile(profiles,args_agent)
#ET.dump(root)
rough_string = ET.tostring(root,'utf-8')
#rough_string = ET.tostring(elem)
#reparsed = minidom.parseString(rough_string.encode('utf-8').decode('utf-8'))
# print reparsed.toprettyxml(indent=" ")
#file = open("pom.xml", 'wb')
file = open(pom_file, 'wb')
file.write(rough_string)
file.close()
def agent_pom_old(pom_file,proj_name):
print "pom_file: ", pom_file
print "proj_name: ", proj_name
tree = ET.parse(pom_file)
root = tree.getroot()
#proj_name = root.find("name",namespaces=namespaces).text
args_agent=arg_agent+proj_name
##hanlde surefire maven test javaagent JVM arguments
build=root.find(elem_prefix+"build")
isfound_javaagent=False
if build is not None:
plugins=build.find(elem_prefix+"plugins")
if plugins is not None:
for plugin in plugins.findall(elem_prefix+"plugin"):
artifactId_name=plugin.find(elem_prefix+"artifactId").text
#print "plugin artifactId: ", artifactId_name
#groupId_name=plugin.find(elem_prefix+"groupId").text
#print "plugin groupId: ", groupId_name
#if artifactId_name=="maven-surefire-plugin" and groupId_name=="org.apache.maven.plugins":
if artifactId_name=="maven-surefire-plugin":
args_CONF=plugin.find(elem_prefix+"configuration")
if args_CONF is not None:
args_JVM=args_CONF.find(elem_prefix+"argLine")
if args_JVM is not None:
args_JVM.text=args_agent+" "+args_JVM.text
else:##argLine is missing
args_JVM=ET.SubElement(args_CONF,"argLine")
args_JVM.text=args_agent
else: ##argCONF is missing
args_CONF=ET.SubElement(plugin,"configuration")
args_JVM=ET.SubElement(args_CONF,"argLine")
args_JVM.text=args_agent
isfound_javaagent=True
break
else:
plugins=ET.SubElement(build,"plugins")
else:
build=ET.SubElement(root,"build")
plugins=ET.SubElement(build,"plugins")
if not isfound_javaagent:
plugin_surefire=ET.fromstring(xml_agent)
plugins.insert(len(plugins),plugin_surefire)
args_JVM=plugin_surefire.find("configuration").find("argLine")
args_JVM.text=args_agent
# <plugin>
# <groupId>org.apache.maven.plugins</groupId>
# <artifactId>maven-surefire-plugin</artifactId>
# <version>2.14</version>
# <configuration>
# <skipTests>false</skipTests>
# <argLine>-javaagent:target/${project.build.finalName}.jar=testUserLog</argLine>
# </configuration>
# </plugin>'''
deps=root.find(elem_prefix+"dependencies",namespaces=namespaces)
isfound_javaassist=False
if deps is None:
deps=ET.SubElement(root,"dependencies")
for dep in deps.findall(elem_prefix+"dependency"):
artifactId_name=dep.find(elem_prefix+"artifactId").text
groupId_name=dep.find(elem_prefix+"groupId").text
if artifactId_name=="javassist" and groupId_name=="org.javassist":
isfound_javaassist=True
break
if not isfound_javaassist:
elem_javassist=ET.fromstring(xml_javassist)
deps.insert(len(deps),elem_javassist)
#ET.dump(root)
rough_string = ET.tostring(root,'utf-8')
#rough_string = ET.tostring(elem)
#reparsed = minidom.parseString(rough_string.encode('utf-8').decode('utf-8'))
# print reparsed.toprettyxml(indent=" ")
#file = open("pom.xml", 'wb')
file = open(pom_file, 'wb')
file.write(rough_string)
file.close()
#end
if __name__== '__main__':
if sys.argv is None or len(sys.argv)<2:
sys.exit('Error! You need to specify at least one maven project id!!')
pom_file="/home/peipei/RepoReaper/"
for proj in sys.argv[1:]:
if isinstance(proj,(int,long)):
agent_pom(pom_file+str(proj)+"/pom.xml",str(proj))
#agent_pom_old(pom_file+str(proj)+"/pom.xml",str(proj))
elif isinstance(proj,str):
agent_pom(pom_file+proj+"/pom.xml",proj)
#agent_pom_old(pom_file+proj+"/pom.xml",proj)
else:
sys.exit('Invalid Input! The arguments are project IDs!')