source: ferringb/snakeoil-dev/snakeoil/caching_2to3.py @ ferringb@gmail.com-20100215031044-boc6qr4uvsnzm3l3

Revision ferringb@gmail.com-20100215031044-boc6qr4uvsnzm3l3, 2.9 KB checked in by Brian Harring <ferringb@…>, 6 months ago (diff)

mangle the crap out of the caching mixin since multipleprocess doesn't play great with it- specifically was caching the pre transform data. super sucks balls, kthnx.

  • Property executable set to True
Line 
1#!/usr/bin/python3
2# Copyright: 2009 Brian Harring <ferringb@gmail.com>
3# License: PSF-2.2/GPL2/BSD
4
5import lib2to3.main
6import lib2to3.refactor
7import os, hashlib
8
9def md5_hash_data(data):
10    chf = hashlib.md5()
11    chf.update(data)
12    return chf.hexdigest()
13
14class caching_mixin(object):
15
16    base_cls = None
17
18    @property
19    def cache_dir(self):
20        return os.environ.get("PY2TO3_CACHEDIR", "cache")
21
22    def get_cache_path(self, cache_key):
23        return os.path.join(self.cache_dir, cache_key)
24
25    def update_cache_from_file(self, cache_key, filename, encoding, new_text=None):
26        cache_dir = self.cache_dir
27        if not os.path.exists(cache_dir):
28            os.mkdir(cache_dir)
29        if new_text is None:
30            new_text = open(filename, 'rb').read().decode(encoding)
31        open(os.path.join(cache_dir, cache_key), 'wb').write(new_text.encode(encoding))
32
33    def check_cache(self, cache_key, encoding):
34        cache_path = self.get_cache_path(cache_key)
35        if os.path.isfile(cache_path):
36            return open(cache_path, 'rb').read().decode(encoding)
37        return None
38
39    @staticmethod
40    def compute_cache_key(input, encoding):
41        return md5_hash_data(input.encode(encoding))
42
43    def refactor_file(self, filename, write=False, doctests_only=False):
44        input, encoding = self._read_python_source(filename)
45        cache_key = self.compute_cache_key(input, encoding)
46        cache_data = self.check_cache(cache_key, encoding)
47
48        if not write or cache_data is None:
49            return super(caching_mixin, self).refactor_file(filename, write=write,
50                doctests_only=doctests_only)
51        else:
52            self.processed_file(cache_data, filename, write=write,
53                encoding=encoding, old_text=input)
54
55    def processed_file(self, new_text, filename, old_text=None, write=False,
56        encoding=None):
57        if write:
58            if old_text is None:
59                cache_key = self.compute_cache_key(*self._read_python_source(filename))
60            else:
61                cache_key = self.compute_cache_key(old_text, encoding)
62            self.update_cache_from_file(cache_key, filename, encoding,
63                new_text=new_text)
64        return super(caching_mixin, self).processed_file(new_text, filename,
65            old_text=old_text, write=write, encoding=encoding)
66
67
68class RefactoringTool(caching_mixin, lib2to3.refactor.RefactoringTool):
69    pass
70
71class MultiprocessRefactoringTool(caching_mixin, lib2to3.refactor.MultiprocessRefactoringTool):
72    pass
73
74
75def StdoutRefactoringTool(*args):
76    # stupid hacks...
77    lib2to3.main.StdoutRefactoringTool = my_StdoutRefactoringTool.base_cls
78    inst = my_StdoutRefactoringTool.base_cls(*args)
79    inst.__class__ = my_StdoutRefactoringTool
80    return inst
81
82if __name__ == '__main__':
83    lib2to3.main.StdoutRefactoringTool = StdoutRefactoringTool
84    import sys
85    sys.exit(lib2to3.main.main("lib2to3.fixes"))
Note: See TracBrowser for help on using the repository browser.