/
ldf2set.py
executable file
·68 lines (65 loc) · 1.82 KB
/
ldf2set.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
#!/usr/bin/python
import sys
from elementtree import ElementTree
def copyfile(x,y):
xh=open(x,'r')
yh=open(y,'w')
yh.writelines(xh.readlines())
xh.close()
yh.close()
def unpacksamples(where,dir):
library={}
cx = 0
tree = ElementTree.parse(where)
for xmlnode in tree.findall("//sample"):
cx+=1
if xmlnode.attrib.has_key('id'):
name = xmlnode.attrib['id']
library[name]=xmlnode.text
else:
name = 'untitled'+`cx`
if xmlnode.attrib.has_key('sort'):
print 'Test case',name,'of sort',xmlnode.attrib['sort'],'not extracted.'
else:
src = open (dir+'/'+name+'.src',"w")
for line in xmlnode.text.split('\n'):
if line.strip()!='':
src.write(line.strip()+'\n')
src.close()
print 'Test case',name,'extracted.'
# All executions
for xmlnode in tree.findall("//runnable"):
cx+=1
if xmlnode.attrib.has_key('id'):
name = xmlnode.attrib['id']
else:
name = 'untitled'+`cx`
if xmlnode.findtext('context'):
if not library.has_key(xmlnode.findtext('context')):
print "No context found for sample",name,'('+xmlnode.findtext('context')+'), test case not used'
continue
else:
con = open (dir+'/'+name+'.ctx','w')
for line in library[xmlnode.findtext('context')].split('\n'):
if line.strip()!='':
con.write(line.strip()+'\n')
con.close()
if xmlnode.findtext('yields'):
con = open (dir+'/'+name+'.val','w')
con.write(xmlnode.findtext('yields'))
con.close()
run = open (dir+'/'+name+'.run','w')
line = xmlnode.findtext('main')
for arg in xmlnode.findall("argument"):
line += ' ' + arg.text
run.write(line+'\n')
run.close()
print cx,'samples in the test set.'
if __name__ == "__main__":
print 'Sample Set Extractor'
if len(sys.argv) == 3:
unpacksamples(sys.argv[1],sys.argv[2])
else:
print 'Usage:'
print ' ',sys.argv[0],'<ldf>','<dir>'
sys.exit(1)