-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
94 lines (63 loc) · 2.17 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import sublime
import yaml
from pathlib import Path
import os
from subprocess import Popen, PIPE
import shutil
def is_sublime_3():
return sublime.version()[0] == "3"
def settings(key):
return sublime.load_settings("RuboCop.sublime-settings").get(key)
def debug(*args):
if settings("debug"):
print("[RuboCop]", *args)
class SafeLoaderIgnoreUnknown(yaml.SafeLoader):
def ignore_unknown(self, node):
return None
SafeLoaderIgnoreUnknown.add_constructor(None,
SafeLoaderIgnoreUnknown.ignore_unknown)
def start_rubocop(folders, restart=False):
for folder in folders:
run_rubocop(["--restart-server"], folder=folder)
def stop_rubocop(folders):
for folder in folders:
run_rubocop(["--stop-server"], folder=folder)
def run_rubocop(args, folder=None):
rubocop_config_file = Path(os.path.join(folder, ".rubocop.yml"))
bundler_config_file = Path(os.path.join(folder, "Gemfile"))
if folder is None:
debug("no folder provided, so skipping")
return (False, "")
if not rubocop_config_file.is_file():
debug("no rubocop config file found, so no need to stop server")
return (False, "")
cmd = []
if settings("bundler") and bundler_config_file.is_file():
cmd += [os.path.expanduser(settings("bundler_command")), "exec", "rubocop"]
else:
cmd += [os.path.expanduser(settings("rubocop_command"))]
cmd += args
if settings("server") and ("--stop-server"
not in cmd) and ("--restart-server" not in cmd):
cmd += ["--server"]
debug("running command", cmd, "for", folder)
os.chdir(folder)
result = Popen(
cmd,
stdout=PIPE,
stderr=PIPE,
cwd=folder,
close_fds=True,
)
stdout = result.stdout.read()
stderr = result.stderr.read()
success = len(stderr) == 0
debug("stderr:")
debug(stderr)
return success, stdout
def clear_cache():
debug("clearing cache")
cache_dir = Path(
os.path.join(sublime.packages_path(), "User", "RuboCop", "Cache"))
if cache_dir.is_dir():
shutil.rmtree(str(cache_dir))