[pypy-svn] r31298 - in pypy/dist/pypy: module/posix module/sys rpython translator/goal

arigo at codespeak.net arigo at codespeak.net
Mon Aug 14 17:40:03 CEST 2006


Author: arigo
Date: Mon Aug 14 17:40:00 2006
New Revision: 31298

Modified:
   pypy/dist/pypy/module/posix/interp_posix.py
   pypy/dist/pypy/module/sys/__init__.py
   pypy/dist/pypy/module/sys/state.py
   pypy/dist/pypy/rpython/ros.py
   pypy/dist/pypy/translator/goal/app_main.py
Log:
issue184 testing

Build a pypy-c that should be movable to a new location or a new machine.
It looks for its library paths (lib-python and pypy/lib) by walking dirs
upwards until it finds them.  It also looks if these paths can be found
in a subdirectory "share/pypy-0.9" along the way.


Modified: pypy/dist/pypy/module/posix/interp_posix.py
==============================================================================
--- pypy/dist/pypy/module/posix/interp_posix.py	(original)
+++ pypy/dist/pypy/module/posix/interp_posix.py	Mon Aug 14 17:40:00 2006
@@ -249,9 +249,8 @@
         s = ros.environ(idx)
         if s is None:
             break
-        p = s.find('=');
+        p = s.find('=')
         if p >= 0:
-            assert p >= 0
             key = s[:p]
             value = s[p+1:]
             space.setitem(w_env, space.wrap(key), space.wrap(value))

Modified: pypy/dist/pypy/module/sys/__init__.py
==============================================================================
--- pypy/dist/pypy/module/sys/__init__.py	(original)
+++ pypy/dist/pypy/module/sys/__init__.py	Mon Aug 14 17:40:00 2006
@@ -35,6 +35,7 @@
         'builtin_module_names'  : 'state.w_None',
         'pypy_getudir'          : 'state.pypy_getudir', 
         'pypy_repr'             : 'state.pypy_repr',
+        'pypy_initial_path'     : 'state.pypy_initial_path',
 
         '_getframe'             : 'vm._getframe', 
         'setrecursionlimit'     : 'vm.setrecursionlimit', 

Modified: pypy/dist/pypy/module/sys/state.py
==============================================================================
--- pypy/dist/pypy/module/sys/state.py	(original)
+++ pypy/dist/pypy/module/sys/state.py	Mon Aug 14 17:40:00 2006
@@ -3,8 +3,9 @@
 """
 import pypy
 from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import ObjSpace
 
-import sys, os 
+import sys, os, stat, errno
 
 # ____________________________________________________________
 #
@@ -23,20 +24,53 @@
         # Initialize the default path
         from pypy.interpreter import autopath
         srcdir = os.path.dirname(autopath.pypydir)
-        python_std_lib = os.path.normpath(
-                os.path.join(autopath.pypydir, os.pardir,'lib-python', '2.4.1'))
-        python_std_lib_modified = os.path.normpath(
-                os.path.join(autopath.pypydir, os.pardir,'lib-python', 'modified-2.4.1'))
-
-        pypy_lib = os.path.join(autopath.pypydir, 'lib') 
-        assert os.path.exists(python_std_lib) 
-        assert os.path.exists(python_std_lib_modified)
-        importlist = ['']
-        for p in os.environ.get('PYTHONPATH', '').split(':'): 
-            if p: 
-                importlist.append(p) 
-        importlist.extend([pypy_lib, python_std_lib_modified, python_std_lib])
-        self.w_path = space.newlist([space.wrap(x) for x in importlist])
+        path = getinitialpath(srcdir)
+        self.w_path = space.newlist([space.wrap(p) for p in path])
+
+def checkdir(path):
+    st = os.stat(path)
+    if not stat.S_ISDIR(st[0]):
+        raise OSError(errno.ENOTDIR, path)
+
+def getinitialpath(srcdir):
+    # build the initial path from the srcdir, which is the path of
+    # the "dist" directory of a PyPy checkout.
+    from pypy.module.sys.version import CPYTHON_VERSION
+    from pypy.rpython import ros
+
+    dirname = '%d.%d.%d' % (CPYTHON_VERSION[0],
+                            CPYTHON_VERSION[1],
+                            CPYTHON_VERSION[2])
+    lib_python = os.path.join(srcdir, 'lib-python')
+
+    python_std_lib = os.path.join(lib_python, dirname)
+    checkdir(python_std_lib)
+    python_std_lib_modified = os.path.join(lib_python, 'modified-' + dirname)
+    checkdir(python_std_lib_modified)
+    pypydir = os.path.join(srcdir, 'pypy')
+    pypy_lib = os.path.join(pypydir, 'lib')
+    checkdir(pypy_lib)
+
+    importlist = ['']
+    pythonpath = ros.getenv('PYTHONPATH')
+    if pythonpath:
+        for p in pythonpath.split(os.pathsep):
+            if p:
+                importlist.append(p)
+    importlist.append(pypy_lib)
+    importlist.append(python_std_lib_modified)
+    importlist.append(python_std_lib)
+    return importlist
+
+def pypy_initial_path(space, srcdir):
+    try:
+        path = getinitialpath(srcdir)
+    except OSError:
+        return space.w_None
+    else:
+        return space.newlist([space.wrap(p) for p in path])
+
+pypy_initial_path.unwrap_spec = [ObjSpace, str]
 
 def get(space): 
     return space.fromcache(State)

Modified: pypy/dist/pypy/rpython/ros.py
==============================================================================
--- pypy/dist/pypy/rpython/ros.py	(original)
+++ pypy/dist/pypy/rpython/ros.py	Mon Aug 14 17:40:00 2006
@@ -16,6 +16,20 @@
     if idx < len(_initial_items):
         return '%s=%s' % _initial_items[idx]
 
+def getenv(name):
+    # slowish, ok for non-repeated use
+    pattern = name + '='
+    idx = 0
+    while 1:
+        s = environ(idx)
+        if s is None:
+            break
+        if s.startswith(pattern):
+            value = s[len(pattern):]
+            return value
+        idx += 1
+    return None
+
 
 class DIR(object):
     # a simulated DIR structure from C, i.e. a directory opened by

Modified: pypy/dist/pypy/translator/goal/app_main.py
==============================================================================
--- pypy/dist/pypy/translator/goal/app_main.py	(original)
+++ pypy/dist/pypy/translator/goal/app_main.py	Mon Aug 14 17:40:00 2006
@@ -1,3 +1,4 @@
+#! /usr/bin/env python
 # App-level version of py.py.
 # XXX this is probably still incomplete.
 """
@@ -10,7 +11,7 @@
   --info       print translation information about this PyPy executable
 """
 
-import sys
+import sys, os
 
 originalexcepthook = sys.__excepthook__
 
@@ -126,8 +127,40 @@
 # ____________________________________________________________
 # Main entry point
 
+AUTOSUBPATH = 'share' + os.sep + 'pypy-%d.%d'
+
 def entry_point(executable, argv):
-    sys.executable = executable
+    # find the full path to the executable, assuming that if there is no '/'
+    # in the provided one then we must look along the $PATH
+    if os.sep not in executable:
+        path = os.getenv('PATH')
+        if path:
+            for dir in path.split(os.pathsep):
+                fn = os.path.join(dir, executable)
+                if os.path.isfile(fn):
+                    executable = fn
+                    break
+    sys.executable = os.path.abspath(executable)
+
+    # set up a sys.path that depends on the local machine
+    autosubpath = AUTOSUBPATH % sys.pypy_version_info[:2]
+    search = executable
+    while 1:
+        dirname = resolvedirof(search)
+        if dirname == search:
+            # not found!  let's hope that the compiled-in path is ok
+            print >> sys.stderr, ('debug: WARNING: library path not found, '
+                                  'using compiled-in sys.path')
+            break
+        newpath = sys.pypy_initial_path(dirname)
+        if newpath is None:
+            newpath = sys.pypy_initial_path(os.path.join(dirname, autosubpath))
+            if newpath is None:
+                search = dirname    # walk to the parent directory
+                continue
+        sys.path = newpath      # found!
+        break
+
     go_interactive = False
     i = 0
     while i < len(argv):
@@ -173,9 +206,7 @@
                     exec cmd in mainmodule.__dict__
                 run_toplevel(run_it)
             else:
-                import os
-                # XXX resolve symlinks
-                scriptdir = os.path.dirname(os.path.abspath(sys.argv[0]))
+                scriptdir = resolvedirof(sys.argv[0])
                 sys.path.insert(0, scriptdir)
                 run_toplevel(execfile, sys.argv[0], mainmodule.__dict__)
         else: 
@@ -190,9 +221,36 @@
     else:
         return 0
 
+def resolvedirof(filename):
+    try:
+        filename = os.path.abspath(filename)
+    except OSError:
+        pass
+    dirname = os.path.dirname(filename)
+    if os.path.islink(filename):
+        try:
+            link = os.readlink(filename)
+        except OSError:
+            pass
+        else:
+            return resolvedirof(os.path.join(dirname, link))
+    return dirname
+
 if __name__ == '__main__':
     # obscure! try removing the following line, see how it crashes, and
     # guess why...
     ImStillAroundDontForgetMe = sys.modules['__main__']
+
     # debugging only
-    sys.exit(entry_point(sys.argv[0], sys.argv[1:]))
+    def pypy_initial_path(s):
+        from pypy.module.sys.state import getinitialpath
+        try:
+            return getinitialpath(s)
+        except OSError:
+            return None
+
+    from pypy.module.sys.version import PYPY_VERSION
+    sys.pypy_version_info = PYPY_VERSION
+    sys.pypy_initial_path = pypy_initial_path
+    #sys.exit(entry_point(sys.argv[0], sys.argv[1:]))
+    sys.exit(entry_point('app_main.py', sys.argv[1:]))



More information about the Pypy-commit mailing list