~mwhudson/pypy/imported-translator-without-old-genc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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()