Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set project_root where the config file may be #53

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from collections import defaultdict
import hashlib
from itertools import chain, takewhile
import logging
import os
import shutil
Expand All @@ -32,12 +33,57 @@
class TemporaryDirectory(Protocol):
name = None # type: str

HOME = os.path.expanduser('~')

USER = getpass.getuser()
TMPDIR_PREFIX = "SublimeLinter-contrib-mypy-%s" % USER

logger = logging.getLogger("SublimeLinter.plugin.mypy")


def paths_upwards(path):
while True:
yield path

next_path = os.path.dirname(path)
# Stop just before root in *nix systems
if next_path == '/':
return

if next_path == path:
return

path = next_path


def paths_upwards_until_home(path):
return chain(takewhile(lambda p: p != HOME, paths_upwards(path)), [HOME])


def find_project_root(src):
"""Attempt to get the project root."""
for src in paths_upwards_until_home(src):
if os.path.exists(os.path.join(src, "mypy.ini")):
return src

if os.path.exists(os.path.join(src, ".mypy.ini")):
return src

if os.path.exists(os.path.join(src, "pyproject.toml")):
return src

if os.path.exists(os.path.join(src, "setup.cfg")):
return src

if os.path.exists(os.path.join(src, ".git")):
return src

if os.path.exists(os.path.join(src, ".hg")):
return src

return src


# Mapping for our created temporary directories.
# For smarter caching purposes,
# we index different cache folders based on the working dir.
Expand Down Expand Up @@ -115,6 +161,8 @@ def cmd(self):

self.settings.set('cache-dir', cache_dir)

self.context['project_root'] = find_project_root(self.view.file_name())

return cmd

def run(self, cmd, code):
Expand Down