70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
"""
|
|
Add these two varibles before running the script
|
|
#VSCODE_PATH = ''
|
|
#PYTHON_EXE_PATH = ''
|
|
"""
|
|
import sys, os, subprocess, re
|
|
|
|
"""
|
|
figure out the ms-python install location for PTVSD library
|
|
"""
|
|
def locatePythonToolFolder():
|
|
|
|
vscodeExtensionPath = ''
|
|
if sys.platform.startswith('win'):
|
|
vscodeExtensionPath = os.path.expandvars(r'%USERPROFILE%\.vscode\extensions')
|
|
else:
|
|
vscodeExtensionPath = os.path.expanduser('~/.vscode/extensions')
|
|
|
|
if os.path.exists(vscodeExtensionPath) == False:
|
|
return ''
|
|
|
|
msPythons = []
|
|
versionPattern = re.compile(r'ms-python.python-(?P<major>\d+).(?P<minor>\d+).(?P<patch>\d+)')
|
|
for entry in os.scandir(vscodeExtensionPath):
|
|
if entry.is_dir(follow_symlinks=False):
|
|
match = versionPattern.match(entry.name)
|
|
if match:
|
|
try:
|
|
version = tuple(int(match[key]) for key in ('major', 'minor', 'patch'))
|
|
msPythons.append((entry, version))
|
|
except:
|
|
pass
|
|
|
|
msPythons.sort(key=lambda pair: pair[1], reverse=True)
|
|
if (msPythons):
|
|
if None == msPythons[0]:
|
|
return ''
|
|
msPythonPath = os.path.expandvars(msPythons[0][0].path)
|
|
index = msPythonPath.rfind('.')
|
|
version = int(msPythonPath[index+1:])
|
|
msPythonPath = os.path.join(msPythonPath, 'pythonFiles', 'lib','python')
|
|
msPythonPath = os.path.normpath(msPythonPath)
|
|
if os.path.exists(msPythonPath) and os.path.isdir(msPythonPath):
|
|
return msPythonPath
|
|
return ''
|
|
|
|
"""
|
|
add '..\.vscode\extensions\ms-python.python-0.8.0\pythonFiles\PythonTools' to Python search Path so that we can import PTVSD library
|
|
"""
|
|
def addSearchPath(searchPath):
|
|
if searchPath:
|
|
sys.path.append(searchPath)
|
|
return True
|
|
|
|
return False
|
|
|
|
def installVSCodeExtension(vsCodePath,pythonPath):
|
|
if not vsCodePath:
|
|
return False
|
|
my_env = os.environ.copy()
|
|
my_env["PATH"] = pythonPath + os.pathsep + my_env["PATH"]
|
|
sys.executable = os.path.join(pythonPath, 'python')
|
|
subprocess.call([vsCodePath,'--install-extension', 'ms-python.python'],env=my_env)
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
installVSCodeExtension(VSCODE_PATH,PYTHON_EXE_PATH)
|
|
msPythonToolPath = locatePythonToolFolder()
|
|
if (msPythonToolPath):
|
|
addSearchPath(msPythonToolPath) |