diff options
author | David Malcolm <dmalcolm@redhat.com> | 2009-11-20 15:13:05 -0500 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2009-11-20 15:13:05 -0500 |
commit | 1cc12923d5b14848bd153e0eee96770073483302 (patch) | |
tree | d0dbce224ae7b947e936f8233ca9e3d5a4e18094 | |
parent | 2936118b94420798c827733844df8a4ea949b280 (diff) | |
download | 2to3c-1cc12923d5b14848bd153e0eee96770073483302.tar.gz 2to3c-1cc12923d5b14848bd153e0eee96770073483302.tar.xz 2to3c-1cc12923d5b14848bd153e0eee96770073483302.zip |
Use "Fixes" classes for both python and .cocci fix implementations; invoke the .cocci files from 2to3c
-rwxr-xr-x | 2to3c | 10 | ||||
-rw-r--r-- | fixes/__init__.py | 33 | ||||
-rw-r--r-- | fixes/typeobject.py | 5 |
3 files changed, 45 insertions, 3 deletions
@@ -1,13 +1,17 @@ #!/usr/bin/env python def get_fixers(): - from fixes.typeobject import fixup_typeobject_initializers - return [fixup_typeobject_initializers] + from fixes.typeobject import FixTypeobjectInitializers + from fixes import CocciFix + fixes = [FixTypeobjectInitializers()] + for filename in ['RO.cocci', 'int-to-long.cocci', 'ob_type.cocci', 'repr.cocci']: + fixes.append(CocciFix(filename)) + return fixes def fixup_content(content): # Apply the various fixers, in turn: for fixer in get_fixers(): - content = fixer(content) + content = fixer.transform(content) return content diff --git a/fixes/__init__.py b/fixes/__init__.py index e69de29..5435e1b 100644 --- a/fixes/__init__.py +++ b/fixes/__init__.py @@ -0,0 +1,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 diff --git a/fixes/typeobject.py b/fixes/typeobject.py index b0c9004..5615430 100644 --- a/fixes/typeobject.py +++ b/fixes/typeobject.py @@ -35,6 +35,11 @@ def fixup_typeobject_initializers(content): else: return content +from fixes import Fix +class FixTypeobjectInitializers(Fix): + def transform(self, string): + return fixup_typeobject_initializers(string) + import unittest class TestFixups(unittest.TestCase): def test_fixups(self): |