You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

site-patch.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. def __boot():
  2. import sys
  3. import os
  4. PYTHONPATH = os.environ.get('PYTHONPATH')
  5. if PYTHONPATH is None or (sys.platform == 'win32' and not PYTHONPATH):
  6. PYTHONPATH = []
  7. else:
  8. PYTHONPATH = PYTHONPATH.split(os.pathsep)
  9. pic = getattr(sys, 'path_importer_cache', {})
  10. stdpath = sys.path[len(PYTHONPATH):]
  11. mydir = os.path.dirname(__file__)
  12. for item in stdpath:
  13. if item == mydir or not item:
  14. continue # skip if current dir. on Windows, or my own directory
  15. importer = pic.get(item)
  16. if importer is not None:
  17. loader = importer.find_module('site')
  18. if loader is not None:
  19. # This should actually reload the current module
  20. loader.load_module('site')
  21. break
  22. else:
  23. try:
  24. import imp # Avoid import loop in Python 3
  25. stream, path, descr = imp.find_module('site', [item])
  26. except ImportError:
  27. continue
  28. if stream is None:
  29. continue
  30. try:
  31. # This should actually reload the current module
  32. imp.load_module('site', stream, path, descr)
  33. finally:
  34. stream.close()
  35. break
  36. else:
  37. raise ImportError("Couldn't find the real 'site' module")
  38. known_paths = dict([(makepath(item)[1], 1) for item in sys.path]) # 2.2 comp
  39. oldpos = getattr(sys, '__egginsert', 0) # save old insertion position
  40. sys.__egginsert = 0 # and reset the current one
  41. for item in PYTHONPATH:
  42. addsitedir(item)
  43. sys.__egginsert += oldpos # restore effective old position
  44. d, nd = makepath(stdpath[0])
  45. insert_at = None
  46. new_path = []
  47. for item in sys.path:
  48. p, np = makepath(item)
  49. if np == nd and insert_at is None:
  50. # We've hit the first 'system' path entry, so added entries go here
  51. insert_at = len(new_path)
  52. if np in known_paths or insert_at is None:
  53. new_path.append(item)
  54. else:
  55. # new path after the insert point, back-insert it
  56. new_path.insert(insert_at, item)
  57. insert_at += 1
  58. sys.path[:] = new_path
  59. if __name__ == 'site':
  60. __boot()
  61. del __boot