Skip to content

Commit

Permalink
Merge pull request #1466 from SublimeLinter/fix-pipfile
Browse files Browse the repository at this point in the history
Fix pipenv support
  • Loading branch information
kaste committed Jul 13, 2018
2 parents fd0801f + 8662a00 commit a5a8dca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
42 changes: 15 additions & 27 deletions lint/base_linter/python_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,44 +130,28 @@ def find_script_by_python_env(python_env_path, script):
return None


def get_project_path():
"""Return the project_path using Sublime's window.project_data() API."""
window = sublime.active_window()
# window.project_data() is a relative new API.
# I don't know what we can expect from 'folders' here. Can we just take
# the first one, if any, and be happy?
project_data = window.project_data() or {}
folders = project_data.get('folders', [])
if folders:
return folders[0]['path'] # ?


def ask_pipenv(linter_name, cwd):
"""Ask pipenv for a virtual environment and maybe resolve the linter."""
# Some pre-checks bc `pipenv` is super slow
project_path = get_project_path()
if not project_path:
if cwd is None:
return

pipfile = os.path.join(project_path, 'Pipfile')
pipfile = os.path.join(cwd, 'Pipfile')
if not os.path.exists(pipfile):
return

# Defer the real work to another function we can cache.
# ATTENTION: If the user has a Pipfile, but did not (yet) installed the
# environment, we will cache a wrong result here.
return _ask_pipenv(linter_name, cwd)
try:
venv = ask_pipenv_for_venv(linter_name, cwd)
except Exception:
return

return find_script_by_python_env(venv, linter_name)


@lru_cache(maxsize=None)
def _ask_pipenv(linter_name, cwd):
def ask_pipenv_for_venv(linter_name, cwd):
cmd = ['pipenv', '--venv']
venv = util.check_output(cmd, cwd=cwd).strip().split('\n')[-1]

if not venv:
return

return find_script_by_python_env(venv, linter_name)
return util.check_output(cmd, cwd=cwd).strip().split('\n')[-1]


VERSION_RE = re.compile(r'(?P<major>\d+)(?:\.(?P<minor>\d+))?')
Expand All @@ -176,7 +160,11 @@ def _ask_pipenv(linter_name, cwd):
@lru_cache(maxsize=None)
def get_python_version(path):
"""Return a dict with the major/minor version of the python at path."""
output = util.check_output([path, '-V'])
try:
output = util.check_output([path, '-V'])
except Exception:
output = ''

return extract_major_minor_version(output.split(' ')[-1])


Expand Down
2 changes: 1 addition & 1 deletion lint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def check_output(cmd, cwd=None):
logger.warning(
"Executing `{}` failed\n {}".format(' '.join(cmd), str(err))
)
return ''
raise
else:
return process_popen_output(output)

Expand Down

0 comments on commit a5a8dca

Please sign in to comment.