find python binary location from windows registry
This commit is contained in:
parent
11fbd2d2a6
commit
eeb7fd8219
1 changed files with 87 additions and 13 deletions
84
WakaTime.py
84
WakaTime.py
|
@ -13,9 +13,9 @@ __version__ = '4.0.17'
|
||||||
import sublime
|
import sublime
|
||||||
import sublime_plugin
|
import sublime_plugin
|
||||||
|
|
||||||
import glob
|
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
@ -23,6 +23,10 @@ import urllib
|
||||||
import webbrowser
|
import webbrowser
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
|
try:
|
||||||
|
import _winreg as winreg # py2
|
||||||
|
except ImportError:
|
||||||
|
import winreg # py3
|
||||||
|
|
||||||
|
|
||||||
# globals
|
# globals
|
||||||
|
@ -101,6 +105,8 @@ def python_binary():
|
||||||
global PYTHON_LOCATION
|
global PYTHON_LOCATION
|
||||||
if PYTHON_LOCATION is not None:
|
if PYTHON_LOCATION is not None:
|
||||||
return PYTHON_LOCATION
|
return PYTHON_LOCATION
|
||||||
|
|
||||||
|
# look for python in PATH and common install locations
|
||||||
paths = [
|
paths = [
|
||||||
"pythonw",
|
"pythonw",
|
||||||
"python",
|
"python",
|
||||||
|
@ -108,17 +114,85 @@ def python_binary():
|
||||||
"/usr/bin/python",
|
"/usr/bin/python",
|
||||||
]
|
]
|
||||||
for path in paths:
|
for path in paths:
|
||||||
|
path = find_python_in_folder(path)
|
||||||
|
if path is not None:
|
||||||
|
PYTHON_LOCATION = path
|
||||||
|
return path
|
||||||
|
|
||||||
|
# look for python in windows registry
|
||||||
|
path = find_python_from_registry(r'SOFTWARE\Python\PythonCore')
|
||||||
|
if path is not None:
|
||||||
|
PYTHON_LOCATION = path
|
||||||
|
return path
|
||||||
|
path = find_python_from_registry(r'SOFTWARE\Wow6432Node\Python\PythonCore')
|
||||||
|
if path is not None:
|
||||||
|
PYTHON_LOCATION = path
|
||||||
|
return path
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def find_python_from_registry(location, reg=None):
|
||||||
|
if platform.system() != 'Windows':
|
||||||
|
return None
|
||||||
|
|
||||||
|
if reg is None:
|
||||||
|
path = find_python_from_registry(location, reg=winreg.HKEY_CURRENT_USER)
|
||||||
|
if path is None:
|
||||||
|
path = find_python_from_registry(location, reg=winreg.HKEY_LOCAL_MACHINE)
|
||||||
|
return path
|
||||||
|
|
||||||
|
val = None
|
||||||
|
sub_key = 'InstallPath'
|
||||||
|
compiled = re.compile(r'^\d+\.\d+$')
|
||||||
|
|
||||||
|
try:
|
||||||
|
with winreg.OpenKey(reg, location) as handle:
|
||||||
|
versions = []
|
||||||
|
try:
|
||||||
|
for index in range(1024):
|
||||||
|
version = winreg.EnumKey(handle, index)
|
||||||
|
try:
|
||||||
|
if compiled.search(version):
|
||||||
|
versions.append(version)
|
||||||
|
except re.error:
|
||||||
|
pass
|
||||||
|
except EnvironmentError:
|
||||||
|
pass
|
||||||
|
versions.sort(reverse=True)
|
||||||
|
for version in versions:
|
||||||
|
try:
|
||||||
|
path = winreg.QueryValue(handle, version + '\\' + sub_key)
|
||||||
|
if path is not None:
|
||||||
|
path = find_python_in_folder(path)
|
||||||
|
if path is not None:
|
||||||
|
return path
|
||||||
|
except WindowsError:
|
||||||
|
print('[WakaTime] Warning: Could not read registry value "{reg}\\{key}\\{version}\\{sub_key}".'.format(
|
||||||
|
reg='HKEY_CURRENT_USER',
|
||||||
|
key=location,
|
||||||
|
version=version,
|
||||||
|
sub_key=sub_key,
|
||||||
|
))
|
||||||
|
except WindowsError:
|
||||||
|
print('[WakaTime] Warning: Could not read registry value "{reg}\\{key}".'.format(
|
||||||
|
reg='HKEY_CURRENT_USER',
|
||||||
|
key=location,
|
||||||
|
))
|
||||||
|
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
|
def find_python_in_folder(folder):
|
||||||
|
path = os.path.realpath(os.path.join(folder, 'pythonw'))
|
||||||
try:
|
try:
|
||||||
Popen([path, '--version'])
|
Popen([path, '--version'])
|
||||||
PYTHON_LOCATION = path
|
|
||||||
return path
|
return path
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
for path in glob.iglob('/python*'):
|
path = os.path.realpath(os.path.join(folder, 'python'))
|
||||||
path = os.path.realpath(os.path.join(path, 'pythonw'))
|
|
||||||
try:
|
try:
|
||||||
Popen([path, '--version'])
|
Popen([path, '--version'])
|
||||||
PYTHON_LOCATION = path
|
|
||||||
return path
|
return path
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in a new issue