| 1 | # Copyright (c) Microsoft Corporation. All rights reserved.
|
|---|
| 2 | # Licensed under the MIT License.
|
|---|
| 3 |
|
|---|
| 4 | if __name__ != "__main__":
|
|---|
| 5 | raise Exception("{} cannot be imported".format(__name__))
|
|---|
| 6 |
|
|---|
| 7 | import os
|
|---|
| 8 | import os.path
|
|---|
| 9 | import runpy
|
|---|
| 10 | import sys
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | def normalize(path):
|
|---|
| 14 | return os.path.normcase(os.path.normpath(path))
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | # We "isolate" the script/module (sys.argv[1]) by removing current working
|
|---|
| 18 | # directory or '' in sys.path and then sending the target on to runpy.
|
|---|
| 19 | cwd = normalize(os.getcwd())
|
|---|
| 20 | sys.path[:] = [p for p in sys.path if p != "" and normalize(p) != cwd]
|
|---|
| 21 | del sys.argv[0]
|
|---|
| 22 | module = sys.argv[0]
|
|---|
| 23 | if module == "-c":
|
|---|
| 24 | ns = {}
|
|---|
| 25 | for code in sys.argv[1:]:
|
|---|
| 26 | exec(code, ns, ns)
|
|---|
| 27 | elif module.startswith("-"):
|
|---|
| 28 | raise NotImplementedError(sys.argv)
|
|---|
| 29 | elif module.endswith(".py"):
|
|---|
| 30 | runpy.run_path(module, run_name="__main__")
|
|---|
| 31 | else:
|
|---|
| 32 | runpy.run_module(module, run_name="__main__", alter_sys=True)
|
|---|