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
|
import os
import tempfile
from subprocess import Popen, PIPE
class Fix(object):
def transform(self, string):
raise NotImplementedError
class CoccinelleError(RuntimeError):
def __init__(self, p, args, stderr):
self.p = p
self.args = args
self.stderr = stderr
def __str__(self):
return ('Return code: %s (args: %s)\n --- start of captured stderr ---\n%s--- enf of captured stderr ---\n'
% (self.p.returncode, repr(self.args), self.stderr))
class CocciFix(Fix):
def __init__(self, filename):
self.filename = filename
def get_script_path(self):
return os.path.join('fixes', self.filename)
def transform(self, string):
# spatch seems to require the input and output to be actual files,
# rather than stdin/stdout.
(src_hn, src_path) = tempfile.mkstemp(suffix="-%s.in.c" % self.filename)
#print (src_hn, src_path)
(dst_hn, dst_path) = tempfile.mkstemp(suffix="-%s.out.c" % self.filename)
#print (dst_hn, dst_path)
os.write(src_hn, string)
args = ['spatch', '-sp_file', self.get_script_path(), src_path, '-o', dst_path]
p = Popen(args, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = p.communicate()
if p.returncode != 0:
raise CoccinelleError(p, args, stderr)
string = open(dst_path, 'r').read()
os.close(src_hn)
os.close(dst_hn)
return string
import unittest
class NonequalStrings(Exception):
def __init__(self, actual_result, exp_result):
self.actual_result = actual_result
self.exp_result = exp_result
def __str__(self):
from difflib import unified_diff
result = '\n'
for line in unified_diff(self.exp_result.splitlines(),
self.actual_result.splitlines(),
fromfile = 'Expected result',
tofile = 'Actual result',
lineterm=''):
result += line + '\n'
return result
class FixTest(unittest.TestCase):
'''Subclass of TestCase for verifying that a Fix is working as expected'''
def assertTransformsTo(self, fixer, src, exp_result):
actual_result = fixer.transform(src)
if actual_result != exp_result:
raise NonequalStrings(actual_result, exp_result)
|