PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/sys/test/autopath.py

https://bitbucket.org/dac_io/pypy
Python | 131 lines | 122 code | 0 blank | 9 comment | 3 complexity | 64d4a5015cfc5c92bff8dd14dda18a8a MD5 | raw file
  1. """
  2. self cloning, automatic path configuration
  3. copy this into any subdirectory of pypy from which scripts need
  4. to be run, typically all of the test subdirs.
  5. The idea is that any such script simply issues
  6. import autopath
  7. and this will make sure that the parent directory containing "pypy"
  8. is in sys.path.
  9. If you modify the master "autopath.py" version (in pypy/tool/autopath.py)
  10. you can directly run it which will copy itself on all autopath.py files
  11. it finds under the pypy root directory.
  12. This module always provides these attributes:
  13. pypydir pypy root directory path
  14. this_dir directory where this autopath.py resides
  15. """
  16. def __dirinfo(part):
  17. """ return (partdir, this_dir) and insert parent of partdir
  18. into sys.path. If the parent directories don't have the part
  19. an EnvironmentError is raised."""
  20. import sys, os
  21. try:
  22. head = this_dir = os.path.realpath(os.path.dirname(__file__))
  23. except NameError:
  24. head = this_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
  25. error = None
  26. while head:
  27. partdir = head
  28. head, tail = os.path.split(head)
  29. if tail == part:
  30. checkfile = os.path.join(partdir, os.pardir, 'pypy', '__init__.py')
  31. if not os.path.exists(checkfile):
  32. error = "Cannot find %r" % (os.path.normpath(checkfile),)
  33. break
  34. else:
  35. error = "Cannot find the parent directory %r of the path %r" % (
  36. partdir, this_dir)
  37. if not error:
  38. # check for bogus end-of-line style (e.g. files checked out on
  39. # Windows and moved to Unix)
  40. f = open(__file__.replace('.pyc', '.py'), 'r')
  41. data = f.read()
  42. f.close()
  43. if data.endswith('\r\n') or data.endswith('\r'):
  44. error = ("Bad end-of-line style in the .py files. Typically "
  45. "caused by a zip file or a checkout done on Windows and "
  46. "moved to Unix or vice-versa.")
  47. if error:
  48. raise EnvironmentError("Invalid source tree - bogus checkout! " +
  49. error)
  50. pypy_root = os.path.join(head, '')
  51. try:
  52. sys.path.remove(head)
  53. except ValueError:
  54. pass
  55. sys.path.insert(0, head)
  56. munged = {}
  57. for name, mod in sys.modules.items():
  58. if '.' in name:
  59. continue
  60. fn = getattr(mod, '__file__', None)
  61. if not isinstance(fn, str):
  62. continue
  63. newname = os.path.splitext(os.path.basename(fn))[0]
  64. if not newname.startswith(part + '.'):
  65. continue
  66. path = os.path.join(os.path.dirname(os.path.realpath(fn)), '')
  67. if path.startswith(pypy_root) and newname != part:
  68. modpaths = os.path.normpath(path[len(pypy_root):]).split(os.sep)
  69. if newname != '__init__':
  70. modpaths.append(newname)
  71. modpath = '.'.join(modpaths)
  72. if modpath not in sys.modules:
  73. munged[modpath] = mod
  74. for name, mod in munged.iteritems():
  75. if name not in sys.modules:
  76. sys.modules[name] = mod
  77. if '.' in name:
  78. prename = name[:name.rfind('.')]
  79. postname = name[len(prename)+1:]
  80. if prename not in sys.modules:
  81. __import__(prename)
  82. if not hasattr(sys.modules[prename], postname):
  83. setattr(sys.modules[prename], postname, mod)
  84. return partdir, this_dir
  85. def __clone():
  86. """ clone master version of autopath.py into all subdirs """
  87. from os.path import join, walk
  88. if not this_dir.endswith(join('pypy','tool')):
  89. raise EnvironmentError("can only clone master version "
  90. "'%s'" % join(pypydir, 'tool',_myname))
  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. _myname = 'autopath.py'
  107. # set guaranteed attributes
  108. pypydir, this_dir = __dirinfo('pypy')
  109. if __name__ == '__main__':
  110. __clone()