summaryrefslogtreecommitdiffstats
path: root/fixes/__init__.py
blob: 5435e1b86ccaf94fff9cf07d6087ad890f5768ec (plain)
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
import os
import tempfile
from subprocess import Popen, PIPE

class Fix(object):
    def transform(self, string):
        raise NotImplementedError

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)

        p = Popen(['spatch', '-sp_file', self.get_script_path(), src_path, '-o', dst_path], stdout=PIPE, stderr=PIPE)
        p.wait()

        string = open(dst_path, 'r').read()
        os.close(src_hn)
        os.close(dst_hn)

        return string