-
Notifications
You must be signed in to change notification settings - Fork 41
/
rubocop_runner.py
95 lines (80 loc) · 2.57 KB
/
rubocop_runner.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
95
import os
import subprocess
import shlex
import locale
RVM_DEFAULT_PATH = '~/.rvm/bin/rvm-auto-ruby'
RBENV_DEFAULT_PATH = '~/.rbenv/bin/rbenv'
class RubocopRunner(object):
"""This class takes care of the rubocop location and its execution"""
def __init__(self, args):
self.set_default_paths()
self.on_windows = False
self.is_st2 = False
self.custom_rubocop_cmd = ''
self.rubocop_config_file = ''
self.chdir = None
vars(self).update(args)
def set_default_paths(self):
self.rvm_auto_ruby_path = RVM_DEFAULT_PATH
self.rbenv_path = RBENV_DEFAULT_PATH
def load_cmd_prefix(self):
self.cmd_prefix = []
if not self.load_rvm():
self.load_rbenv()
def load_rvm(self):
if self.use_rvm:
rvm_cmd = os.path.expanduser(self.rvm_auto_ruby_path)
self.cmd_prefix = [rvm_cmd, '-S']
return True
return False
def load_rbenv(self):
if self.use_rbenv:
rbenv_cmd = os.path.expanduser(self.rbenv_path)
self.cmd_prefix = [rbenv_cmd, 'exec']
return True
return False
def run(self, pathlist, options=[]):
call_list = self.command_list(pathlist, options)
use_shell = False
if self.on_windows:
use_shell = True
p = subprocess.Popen(call_list, shell=use_shell,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.chdir)
out, err = p.communicate()
return out
def command_string(self, pathlist, options=[]):
list = self.command_list(pathlist, options)
return ' '.join(list)
def command_list(self, pathlist, options=[]):
result = []
# Command
if not self.custom_rubocop_cmd:
self.load_cmd_prefix()
result += self.cmd_prefix
result.append('rubocop')
else:
if self.on_windows and self.is_st2:
result += self.custom_rubocop_cmd.split()
else:
result += shlex.split(self.custom_rubocop_cmd)
# Options
if options:
for option in options:
result.append(option)
if self.rubocop_config_file:
result.append('-c')
result.append(self.rubocop_config_file)
# Paths
for path in pathlist:
if self.on_windows:
path = path.replace('\\', '/')
result.append(path)
# ST 2 uses Python 2.6.5 which uses CreateProcessA
# within subprocess on Windows. Does not work well
# with strings including 0 bytes. This is a hack
# which just removes 0 bytes from each command part.
# This won't work with paths which include unicode
# characters.
if self.on_windows and self.is_st2:
result = map(lambda s:s.replace('\x00', ''), result)
return result