Files lib2to3-orig/Grammar3.1.1.final.0.pickle and lib2to3-new/Grammar3.1.1.final.0.pickle differ Files lib2to3-orig/PatternGrammar3.1.1.final.0.pickle and lib2to3-new/PatternGrammar3.1.1.final.0.pickle differ diff -urN lib2to3-orig/caching.py lib2to3-new/caching.py --- lib2to3-orig/caching.py 1969年12月31日 16:00:00.000000000 -0800 +++ lib2to3-new/caching.py 2010年01月31日 17:14:24.000000000 -0800 @@ -0,0 +1,62 @@ +#!/usr/bin/python3 +# Copyright: 2009 Brian Harring +# License: PSF-2.2/GPL2/BSD + +import os, hashlib + +def md5_hash_data(data): + chf = hashlib.md5() + chf.update(data) + return chf.hexdigest() + +class CachingMixin(object): + + base_cls = None + + @classmethod + def derive_cls(cls, target): + class kls(cls, target): + base_cls = target + kls.__name__ = "Caching%s" % (target.__name__,) + return kls + + @property + def cache_dir(self): + return self.options["cachedir"] + + def get_cache_path(self, cache_key): + return os.path.join(self.cache_dir, cache_key) + + def update_cache_from_file(self, cache_key, filename, encoding): + cache_dir = self.cache_dir + if not os.path.exists(cache_dir): + os.mkdir(cache_dir) + return None + output = open(filename, 'rb').read().decode(encoding) + open(os.path.join(cache_dir, cache_key), 'wb').write(output.encode(encoding)) + + def check_cache(self, cache_key, encoding): + cache_path = self.get_cache_path(cache_key) + if os.path.isfile(cache_path): + return open(cache_path, 'rb').read().decode(encoding) + return None + + @staticmethod + def compute_cache_key(input, encoding): + return md5_hash_data(input.encode(encoding)) + + def refactor_file(self, filename, write=False, doctests_only=False): + if not write: + return self.base_cls.refactor_file(self, filename, write=write, + doctests_only=doctests_only) + input, encoding = self._read_python_source(filename) + cache_key = self.compute_cache_key(input, encoding) + cache_data = self.check_cache(cache_key, encoding) + if cache_data is None: + self.base_cls.refactor_file(self, filename, write=write, + doctests_only=doctests_only) + self.update_cache_from_file(cache_key, filename, encoding) + else: + print("cache hit") + self.processed_file(cache_data, filename, write=write, + encoding=encoding, old_text=input) diff -urN lib2to3-orig/main.py lib2to3-new/main.py --- lib2to3-orig/main.py 2010年01月31日 16:49:31.000000000 -0800 +++ lib2to3-new/main.py 2010年01月31日 17:12:12.000000000 -0800 @@ -10,7 +10,7 @@ import optparse from . import refactor - +from . import caching def diff_texts(a, b, filename): """Return a unified diff of two strings.""" @@ -73,7 +73,7 @@ print("WARNING: %s" % (msg,), file=sys.stderr) -def main(fixer_pkg, args=None): +def main(fixer_pkg, args=None, refactoring_tool_cls=StdoutRefactoringTool): """Main program. Args: @@ -105,6 +105,11 @@ help="Write back modified files") parser.add_option("-n", "--nobackups", action="store_true", default=False, help="Don't write backups for modified files.") + parser.add_option("--cache-dir", action="store", default=None, + help="Directory to use as a cache for translation using " + "the MD5 of the source to lookup previous conversions. " + "Use at your own risk; the environment variable " + "PY2TO3_CACHEDIR can also be set instead.") # Parse command line arguments refactor_stdin = False @@ -132,6 +137,14 @@ if options.print_function: flags["print_function"] = True + cachedir = options.cache_dir + if cachedir is None: + cachedir = os.environ.get("PY2TO3_CACHEDIR") + if cachedir: + flags['cachedir'] = os.path.abspath(cachedir) + refactoring_tool_cls = caching.CachingMixin.derive_cls( + refactoring_tool_cls) + # Set up logging handler level = logging.DEBUG if options.verbose else logging.INFO logging.basicConfig(format='%(name)s: %(message)s', level=level) @@ -151,7 +164,7 @@ else: requested = avail_fixes.union(explicit) fixer_names = requested.difference(unwanted_fixes) - rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit), + rt = refactoring_tool_cls(sorted(fixer_names), flags, sorted(explicit), options.nobackups, not options.no_diffs) # Refactor all files and directories passed as arguments

AltStyle によって変換されたページ (->オリジナル) /