~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/jit/codegen/i386/autopath.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
self cloning, automatic path configuration 
 
3
 
 
4
copy this into any subdirectory of pypy from which scripts need 
 
5
to be run, typically all of the test subdirs. 
 
6
The idea is that any such script simply issues
 
7
 
 
8
    import autopath
 
9
 
 
10
and this will make sure that the parent directory containing "pypy"
 
11
is in sys.path. 
 
12
 
 
13
If you modify the master "autopath.py" version (in pypy/tool/autopath.py) 
 
14
you can directly run it which will copy itself on all autopath.py files
 
15
it finds under the pypy root directory. 
 
16
 
 
17
This module always provides these attributes:
 
18
 
 
19
    pypydir    pypy root directory path 
 
20
    this_dir   directory where this autopath.py resides 
 
21
 
 
22
"""
 
23
 
 
24
 
 
25
def __dirinfo(part):
 
26
    """ return (partdir, this_dir) and insert parent of partdir
 
27
    into sys.path.  If the parent directories don't have the part
 
28
    an EnvironmentError is raised."""
 
29
 
 
30
    import sys, os
 
31
    try:
 
32
        head = this_dir = os.path.realpath(os.path.dirname(__file__))
 
33
    except NameError:
 
34
        head = this_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
 
35
 
 
36
    while head:
 
37
        partdir = head
 
38
        head, tail = os.path.split(head)
 
39
        if tail == part:
 
40
            break
 
41
    else:
 
42
        raise EnvironmentError, "'%s' missing in '%r'" % (partdir, this_dir)
 
43
    
 
44
    pypy_root = os.path.join(head, '')
 
45
    try:
 
46
        sys.path.remove(head)
 
47
    except ValueError:
 
48
        pass
 
49
    sys.path.insert(0, head)
 
50
 
 
51
    munged = {}
 
52
    for name, mod in sys.modules.items():
 
53
        if '.' in name:
 
54
            continue
 
55
        fn = getattr(mod, '__file__', None)
 
56
        if not isinstance(fn, str):
 
57
            continue
 
58
        newname = os.path.splitext(os.path.basename(fn))[0]
 
59
        if not newname.startswith(part + '.'):
 
60
            continue
 
61
        path = os.path.join(os.path.dirname(os.path.realpath(fn)), '')
 
62
        if path.startswith(pypy_root) and newname != part:
 
63
            modpaths = os.path.normpath(path[len(pypy_root):]).split(os.sep)
 
64
            if newname != '__init__':
 
65
                modpaths.append(newname)
 
66
            modpath = '.'.join(modpaths)
 
67
            if modpath not in sys.modules:
 
68
                munged[modpath] = mod
 
69
 
 
70
    for name, mod in munged.iteritems():
 
71
        if name not in sys.modules:
 
72
            sys.modules[name] = mod
 
73
        if '.' in name:
 
74
            prename = name[:name.rfind('.')]
 
75
            postname = name[len(prename)+1:]
 
76
            if prename not in sys.modules:
 
77
                __import__(prename)
 
78
                if not hasattr(sys.modules[prename], postname):
 
79
                    setattr(sys.modules[prename], postname, mod)
 
80
 
 
81
    return partdir, this_dir
 
82
 
 
83
def __clone():
 
84
    """ clone master version of autopath.py into all subdirs """
 
85
    from os.path import join, walk
 
86
    if not this_dir.endswith(join('pypy','tool')):
 
87
        raise EnvironmentError("can only clone master version "
 
88
                               "'%s'" % join(pypydir, 'tool',_myname))
 
89
 
 
90
 
 
91
    def sync_walker(arg, dirname, fnames):
 
92
        if _myname in fnames:
 
93
            fn = join(dirname, _myname)
 
94
            f = open(fn, 'rwb+')
 
95
            try:
 
96
                if f.read() == arg:
 
97
                    print "checkok", fn
 
98
                else:
 
99
                    print "syncing", fn
 
100
                    f = open(fn, 'w')
 
101
                    f.write(arg)
 
102
            finally:
 
103
                f.close()
 
104
    s = open(join(pypydir, 'tool', _myname), 'rb').read()
 
105
    walk(pypydir, sync_walker, s)
 
106
 
 
107
_myname = 'autopath.py'
 
108
 
 
109
# set guaranteed attributes
 
110
 
 
111
pypydir, this_dir = __dirinfo('pypy')
 
112
 
 
113
if __name__ == '__main__':
 
114
    __clone()