MCPcopy Create free account
hub / github.com/Persper/code-analytics / transform_src_to_tree

Function transform_src_to_tree

persper/graphs/srcml.py:53–83  ·  view source on GitHub ↗
(source_code, ext='.c')

Source from the content-addressed store, hash-verified

51
52
53def transform_src_to_tree(source_code, ext='.c'):
54 root = None
55 try:
56 f = tempfile.NamedTemporaryFile(mode='wb+', delete=False)
57 f.write(source_code.encode('utf-8', 'replace'))
58 f.close()
59 except UnicodeEncodeError as e:
60 print("UnicodeEncodeError in transform_src_to_tree!")
61 if not f.closed:
62 f.close()
63 os.remove(f.name)
64 return None
65
66 # rename so that srcml can open it
67 new_fname = f.name + ext
68 os.rename(f.name, new_fname)
69 xml_path = f.name + ".xml"
70 cmd = 'srcml {} --position -o {}'.format(new_fname, xml_path)
71 subprocess.call(cmd, shell=True)
72 try:
73 root = etree.parse(xml_path).getroot()
74 except:
75 print("Unable to parse xml file!")
76 finally:
77 if not f.closed:
78 f.close()
79 os.remove(new_fname)
80 if os.path.exists(xml_path):
81 os.remove(xml_path)
82
83 return root
84
85
86def main():

Callers 5

test_detect_changeFunction · 0.90
test_detect_changeFunction · 0.90
update_graphMethod · 0.90
parseMethod · 0.90
_get_xml_rootMethod · 0.90

Calls 2

writeMethod · 0.80
parseMethod · 0.45

Tested by 2

test_detect_changeFunction · 0.72
test_detect_changeFunction · 0.72