Ticket #5531: pyvsc-run-isolated.py

File pyvsc-run-isolated.py, 900 bytes (added by Tristan Croll, 4 years ago)

Added by email2trac

Line 
1# Copyright (c) Microsoft Corporation. All rights reserved.
2# Licensed under the MIT License.
3
4if __name__ != "__main__":
5 raise Exception("{} cannot be imported".format(__name__))
6
7import os
8import os.path
9import runpy
10import sys
11
12
13def 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.
19cwd = normalize(os.getcwd())
20sys.path[:] = [p for p in sys.path if p != "" and normalize(p) != cwd]
21del sys.argv[0]
22module = sys.argv[0]
23if module == "-c":
24 ns = {}
25 for code in sys.argv[1:]:
26 exec(code, ns, ns)
27elif module.startswith("-"):
28 raise NotImplementedError(sys.argv)
29elif module.endswith(".py"):
30 runpy.run_path(module, run_name="__main__")
31else:
32 runpy.run_module(module, run_name="__main__", alter_sys=True)