[pypy-svn] r15077 - pypy/dist/pypy/translator/llvm2/tool

ericvrp at codespeak.net ericvrp at codespeak.net
Mon Jul 25 21:00:51 CEST 2005


Author: ericvrp
Date: Mon Jul 25 21:00:51 2005
New Revision: 15077

Added:
   pypy/dist/pypy/translator/llvm2/tool/autopath.py
Modified:
   pypy/dist/pypy/translator/llvm2/tool/suggested_primitive.py
Log:
Followed Christians suggestions, code more r0obust now. Does not need to be run from it's own dir..



Added: pypy/dist/pypy/translator/llvm2/tool/autopath.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/tool/autopath.py	Mon Jul 25 21:00:51 2005
@@ -0,0 +1,117 @@
+"""
+self cloning, automatic path configuration 
+
+copy this into any subdirectory of pypy from which scripts need 
+to be run, typically all of the test subdirs. 
+The idea is that any such script simply issues
+
+    import autopath
+
+and this will make sure that the parent directory containing "pypy"
+is in sys.path. 
+
+If you modify the master "autopath.py" version (in pypy/tool/autopath.py) 
+you can directly run it which will copy itself on all autopath.py files
+it finds under the pypy root directory. 
+
+This module always provides these attributes:
+
+    pypydir    pypy root directory path 
+    this_dir   directory where this autopath.py resides 
+
+"""
+
+
+def __dirinfo(part):
+    """ return (partdir, this_dir) and insert parent of partdir
+    into sys.path.  If the parent directories don't have the part
+    an EnvironmentError is raised."""
+
+    import sys, os
+    try:
+        head = this_dir = os.path.realpath(os.path.dirname(__file__))
+    except NameError:
+        head = this_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
+
+    while head:
+        partdir = head
+        head, tail = os.path.split(head)
+        if tail == part:
+            break
+    else:
+        raise EnvironmentError, "'%s' missing in '%r'" % (partdir, this_dir)
+    
+    checkpaths = sys.path[:]
+    pypy_root = os.path.join(head, '')
+    
+    while checkpaths:
+        orig = checkpaths.pop()
+        fullorig = os.path.join(os.path.realpath(orig), '')
+        if fullorig.startswith(pypy_root):
+            if os.path.exists(os.path.join(fullorig, '__init__.py')):
+                sys.path.remove(orig)
+    if head not in sys.path:
+        sys.path.insert(0, head)
+
+    munged = {}
+    for name, mod in sys.modules.items():
+        fn = getattr(mod, '__file__', None)
+        if '.' in name or not isinstance(fn, str):
+            continue
+        newname = os.path.splitext(os.path.basename(fn))[0]
+        if not newname.startswith(part + '.'):
+            continue
+        path = os.path.join(os.path.dirname(os.path.realpath(fn)), '')
+        if path.startswith(pypy_root) and newname != part:
+            modpaths = os.path.normpath(path[len(pypy_root):]).split(os.sep)
+            if newname != '__init__':
+                modpaths.append(newname)
+            modpath = '.'.join(modpaths)
+            if modpath not in sys.modules:
+                munged[modpath] = mod
+
+    for name, mod in munged.iteritems():
+        if name not in sys.modules:
+            sys.modules[name] = mod
+        if '.' in name:
+            prename = name[:name.rfind('.')]
+            postname = name[len(prename)+1:]
+            if prename not in sys.modules:
+                __import__(prename)
+                if not hasattr(sys.modules[prename], postname):
+                    setattr(sys.modules[prename], postname, mod)
+
+    return partdir, this_dir
+
+def __clone():
+    """ clone master version of autopath.py into all subdirs """
+    from os.path import join, walk
+    if not this_dir.endswith(join('pypy','tool')):
+        raise EnvironmentError("can only clone master version "
+                               "'%s'" % join(pypydir, 'tool',_myname))
+
+
+    def sync_walker(arg, dirname, fnames):
+        if _myname in fnames:
+            fn = join(dirname, _myname)
+            f = open(fn, 'rwb+')
+            try:
+                if f.read() == arg:
+                    print "checkok", fn
+                else:
+                    print "syncing", fn
+                    f = open(fn, 'w')
+                    f.write(arg)
+            finally:
+                f.close()
+    s = open(join(pypydir, 'tool', _myname), 'rb').read()
+    walk(pypydir, sync_walker, s)
+
+_myname = 'autopath.py'
+
+# set guaranteed attributes
+
+pypydir, this_dir = __dirinfo('pypy')
+
+if __name__ == '__main__':
+    __clone()

Modified: pypy/dist/pypy/translator/llvm2/tool/suggested_primitive.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/tool/suggested_primitive.py	(original)
+++ pypy/dist/pypy/translator/llvm2/tool/suggested_primitive.py	Mon Jul 25 21:00:51 2005
@@ -1,42 +1,28 @@
 #!/usr/bin/python
 
+import autopath
 import os
 
+from pypy.rpython.module import ll_os, ll_os_path, ll_time, ll_math #XXX keep this list up-to-date
+from pypy.translator.llvm2.extfunction import extfunctions
+
 def main():
+    suggested_primitives = []
+    for module in (ll_os, ll_os_path, ll_time, ll_math):    #XXX keep this list up-to-date too
+        suggested_primitives += [func for func in dir(module) if getattr(module.__dict__[func], 'suggested_primitive', False)]
+
+    implemented_primitives = [f[1:] for f in extfunctions.keys()]
 
-    ll_modules_path = '../../../rpython/module'
-    ll_files = [ll_modules_path + '/' + f for f in os.listdir(ll_modules_path) if f[:3] == 'll_' and f[-3:] == '.py']
-    ll_function = {}    #XXX better use sets
-    for ll_file in ll_files:
-        for s in file(ll_file):
-            s = s.strip()
-            if not s.startswith('ll_') or s.find('suggested_primitive') == -1 or s.find('True') == -1:
-                continue
-            ll_function[s.split('.')[0]] = True
-
-    llvm_modules_path = '..'
-    llvm_files = [llvm_modules_path + '/' + 'extfunction.py']
-    llvm_function = {}
-    for llvm_file in llvm_files:
-        t = 'extfunctions["%'
-        for s in file(llvm_file):
-            s = s.strip()
-            if not s.startswith(t):
-                continue
-            llvm_function[s.split('"')[1][1:]] = True
+    missing_primitives = [func for func in suggested_primitives if func not in implemented_primitives]
 
     print 'rpython suggested primitives:'
-    print ll_function.keys()
+    print suggested_primitives
     print
-
-    print 'llvm implemented functions:'
-    print llvm_function.keys()
+    print 'llvm implemented primitives:'
+    print implemented_primitives
     print
-
     print 'llvm missing primitives:'
-    missing_functions = [func for func in ll_function.keys() if func not in llvm_function]
-    print missing_functions
-    print
+    print missing_primitives
 
 if __name__ == '__main__':
     main()



More information about the Pypy-commit mailing list