Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | """By using execfile(this_file, dict(__file__=this_file)) you will |
michael@0 | 2 | activate this virtualenv environment. |
michael@0 | 3 | |
michael@0 | 4 | This can be used when you must use an existing Python interpreter, not |
michael@0 | 5 | the virtualenv bin/python |
michael@0 | 6 | """ |
michael@0 | 7 | |
michael@0 | 8 | try: |
michael@0 | 9 | __file__ |
michael@0 | 10 | except NameError: |
michael@0 | 11 | raise AssertionError( |
michael@0 | 12 | "You must run this like execfile('path/to/activate_this.py', dict(__file__='path/to/activate_this.py'))") |
michael@0 | 13 | import sys |
michael@0 | 14 | import os |
michael@0 | 15 | |
michael@0 | 16 | old_os_path = os.environ['PATH'] |
michael@0 | 17 | os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path |
michael@0 | 18 | base = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
michael@0 | 19 | if sys.platform == 'win32': |
michael@0 | 20 | site_packages = os.path.join(base, 'Lib', 'site-packages') |
michael@0 | 21 | else: |
michael@0 | 22 | site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages') |
michael@0 | 23 | prev_sys_path = list(sys.path) |
michael@0 | 24 | import site |
michael@0 | 25 | site.addsitedir(site_packages) |
michael@0 | 26 | sys.real_prefix = sys.prefix |
michael@0 | 27 | sys.prefix = base |
michael@0 | 28 | # Move the added items to the front of the path: |
michael@0 | 29 | new_sys_path = [] |
michael@0 | 30 | for item in list(sys.path): |
michael@0 | 31 | if item not in prev_sys_path: |
michael@0 | 32 | new_sys_path.append(item) |
michael@0 | 33 | sys.path.remove(item) |
michael@0 | 34 | sys.path[:0] = new_sys_path |