1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5# This script is not to be imported.
6assert __name__ == '__main__'
7import os
8import sys
9# If some older version of virtualenv is installed, it may interfere with this one.
10# So filter-out site-packages and dist-packages directories where it might be installed.
11# This is kinda sorta like invoking python with -S, but invoking a virtualenv python 2.7
12# with -S is broken (sys.path becomes completely wrong, and even `import os` fails).
13# (And while yes, it is kind of silly to use a virtualenv python to run virtualenv, we
14# do)
15sys.path = [p for p in sys.path if os.path.basename(p) not in ('site-packages', 'dist-packages')]
16try:
17    import importlib.util
18    spec = importlib.util.spec_from_file_location('main', os.path.join(os.path.dirname(__file__), '__main__.py'))
19    mod = importlib.util.module_from_spec(spec)
20    spec.loader.exec_module(mod)
21except ImportError:
22    import imp
23    mod_info = imp.find_module('__main__', [os.path.dirname(__file__)])
24    mod = imp.load_module('main', *mod_info)
25
26
27# Fake zipfile module to make `mod.VersionedFindLoad` able to read the extracted
28# files rather than the zipapp.
29class fake_zipfile(object):
30    class ZipFile(object):
31        def __init__(self, path, mode):
32            self._path = path
33            self._mode = mode
34
35        def open(self, path):
36            #  The caller expects a raw file object, with no unicode handling.
37            return open(os.path.join(self._path, path), self._mode + 'b')
38
39        def close(self):
40            pass
41
42mod.zipfile = fake_zipfile
43
44
45def run():
46    with mod.VersionedFindLoad() as finder:
47        sys.meta_path.insert(0, finder)
48        finder._register_distutils_finder()
49        from virtualenv.__main__ import run as run_virtualenv
50
51        run_virtualenv()
52
53
54if __name__ == "__main__":
55    run()
56