[pypy-svn] r47518 - in pypy/dist/pypy/tool: . test

fijal at codespeak.net fijal at codespeak.net
Wed Oct 17 19:31:02 CEST 2007


Author: fijal
Date: Wed Oct 17 19:31:01 2007
New Revision: 47518

Added:
   pypy/dist/pypy/tool/gcc_cache.py   (contents, props changed)
   pypy/dist/pypy/tool/test/test_gcc_cache.py   (contents, props changed)
Log:
Caching "mechanism"


Added: pypy/dist/pypy/tool/gcc_cache.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/tool/gcc_cache.py	Wed Oct 17 19:31:01 2007
@@ -0,0 +1,34 @@
+
+from pypy.tool.autopath import pypydir
+from pypy.translator.tool.cbuild import build_executable
+import md5
+import py
+import distutils
+
+cache_dir = py.path.local(pypydir).join('_cache', 'gcc')
+cache_dir.ensure(dir=1)
+
+def build_executable_cache(c_files, *args, **kwds):
+    s = "\n\n".join([c_file.read() for c_file in c_files])
+    hash = md5.md5(s).hexdigest()
+    try:
+        return cache_dir.join(hash).read()
+    except py.error.Error:
+        result = py.process.cmdexec(build_executable(c_files, *args, **kwds))
+        cache_dir.join(hash).write(result)
+        return result
+
+def try_compile_cache(c_files, *args, **kwds):
+    s = "\n\n".join([c_file.read() for c_file in c_files])
+    hash = md5.md5(s).hexdigest()
+    try:
+        return eval(cache_dir.join(hash).read())
+    except py.error.Error:
+        try:
+            build_executable(c_files, *args, **kwds)
+            result = True
+        except (distutils.errors.CompileError,
+                distutils.errors.LinkError):
+            result = False
+        cache_dir.join(hash).write(repr(result))
+        return bool(result)

Added: pypy/dist/pypy/tool/test/test_gcc_cache.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/tool/test/test_gcc_cache.py	Wed Oct 17 19:31:01 2007
@@ -0,0 +1,37 @@
+
+from pypy.tool.gcc_cache import *
+from pypy.tool.udir import udir
+import md5
+
+def test_gcc_exec():
+    f = udir.join("x.c")
+    f.write("""
+    #include <stdio.h>
+    int main()
+    {
+       printf("3\\n");
+       return 0;
+    }
+    """)
+    # remove cache
+    try:
+        cache_dir.join(md5.md5(f.read()).hexdigest()).remove()
+    except:
+        pass
+    assert build_executable_cache([f]) == "3\n"
+    assert build_executable_cache([f], compiler_exe="xxx") == "3\n"
+
+def test_gcc_ask():
+    f = udir.join("y.c")
+    f.write("""
+    int main()
+    {
+      return 0;
+    }
+    """)
+    try:
+        cache_dir.join(md5.md5(f.read()).hexdigest()).remove()
+    except:
+        pass
+    assert try_compile_cache([f])
+    assert try_compile_cache([f], compiler_exe="xxx")



More information about the Pypy-commit mailing list