def run(self, cmd, code): """ Return a list with the command to execute. The command chosen is resolved as follows: If the `use-cargo` option is set, lint using a `cargo build`. If cargo is not used, and the `use-crate-root` option is set, lint the crate root. Finally, if the crate root cannot be determined, or the `use-crate-root` option is not set, lint the current file. Linting the crate (either through cargo or rustc) means that if errors are caught in other files, errors on the current file might not show up until these other errors are resolved. Linting a single file means that any imports from higher in the module hierarchy will probably cause an error and prevent proper linting in the rest of the file. """ self.use_cargo = self.get_view_settings().get('use-cargo', False) self.use_cargo_check = self.get_view_settings().get( 'use-cargo-check', False) self.use_crate_root = self.get_view_settings().get( 'use-crate-root', False) if self.use_cargo or self.use_cargo_check: cargo_cmd = ['check'] if self.use_cargo_check else self.cmd current_dir = os.path.dirname(self.filename) self.cargo_config = util.find_file(current_dir, 'Cargo.toml') if self.cargo_config: self.tempfile_suffix = '-' old_cwd = os.getcwd() os.chdir(os.path.dirname(self.cargo_config)) try: return util.communicate( ['cargo'] + cargo_cmd + ['--manifest-path', self.cargo_config], code=None, output_stream=self.error_stream, env=self.env) finally: os.chdir(old_cwd) if self.use_crate_root: self.crate_root = self.locate_crate_root() if self.crate_root: cmd.append(self.crate_root) self.tempfile_suffix = '-' return util.communicate(cmd, code=None, output_stream=self.error_stream, env=self.env) self.tempfile_suffix = 'rs' return self.tmpfile(cmd, code)
def run(self, cmd, code): """ Return a list with the command to execute. The command chosen is resolved as follows: If the `use-cargo` option is set, lint using a `cargo build`. If cargo is not used, and the `use-crate-root` option is set, lint the crate root. Finally, if the crate root cannot be determined, or the `use-crate-root` option is not set, lint the current file. Linting the crate (either through cargo or rustc) means that if errors are caught in other files, errors on the current file might not show up until these other errors are resolved. Linting a single file means that any imports from higher in the module hierarchy will probably cause an error and prevent proper linting in the rest of the file. """ self.use_cargo = self.get_view_settings().get('use-cargo', False) self.use_cargo_check = self.get_view_settings().get('use-cargo-check', False) self.use_crate_root = self.get_view_settings().get('use-crate-root', False) if self.use_cargo or self.use_cargo_check: cargo_cmd = ['check'] if self.use_cargo_check else self.cmd current_dir = os.path.dirname(self.filename) self.cargo_config = util.find_file(current_dir, 'Cargo.toml') if self.cargo_config: self.tempfile_suffix = '-' old_cwd = os.getcwd() os.chdir(os.path.dirname(self.cargo_config)) try: return util.communicate( ['cargo'] + cargo_cmd + ['--manifest-path', self.cargo_config], code=None, output_stream=self.error_stream, env=self.env) finally: os.chdir(old_cwd) if self.use_crate_root: self.crate_root = self.locate_crate_root() if self.crate_root: cmd.append(self.crate_root) self.tempfile_suffix = '-' return util.communicate(cmd, code=None, output_stream=self.error_stream, env=self.env) self.tempfile_suffix = 'rs' return self.tmpfile(cmd, code)
def doFormat(self, edit, region, v): """Format a region of text using 9e-sass-lint --fix command.""" cmd = [] cmd.append(util.which("9e-sass-lint")) cmd.append("--stdin") cmd.append("--fix") if not region.empty(): s = v.substr(region) s = util.communicate(cmd, code=s) if len(s) > 0: v.replace(edit, region, s) else: args = cmd, code = s, output_stream = util.STREAM_STDERR error = util.communicate(args) sublime.error_message(error)
def run(self, cmd, code): cmd += [ os.path.dirname(self.filename), '-I', '^%s' % os.path.relpath(self.filename, os.getcwd()) ] result = util.communicate(cmd, output_stream=self.error_stream, env=self.env) return result
def _in_place_lint(self, cmd): filename = os.path.basename(self.filename) cmd = cmd + ['-I', filename] print('gometalinter: in-place linting {}: {}'.format( filename, ' '.join(map(shlex.quote, cmd)))) out = util.communicate(cmd, output_stream=util.STREAM_STDOUT, env=self.env) return out or ''
def run(self, cmd, code): current_dir = os.path.dirname(self.filename) self.cargo_config = util.find_file(current_dir, 'Cargo.toml') try: os.chdir(os.path.dirname(self.cargo_config)) return util.communicate(cmd, code=code, output_stream=self.error_stream, env=self.env) finally: os.chdir(current_dir)
def _in_place_lint(self, cmd): dir, env = self._dir_env() if not dir: print('golangci-lint: skipped linting of unsaved file') return filename = os.path.basename(self.filename) print('golangci-lint: in-place linting {}: {}'.format( filename, ' '.join(map(shlex.quote, cmd)))) out = util.communicate(cmd, output_stream=util.STREAM_BOTH, env=env, cwd=dir) return out or ''
def _in_place_lint(self, cmd): dir, env = self._dir_env() if not dir: print('gometalinter: skipped linting of unsaved file') return filename = os.path.basename(self.filename) cmd = cmd + ['-I', '^' + filename] print('gometalinter: in-place linting {}: {}'.format( filename, ' '.join(map(shlex.quote, cmd)))) out = util.communicate(cmd, output_stream=util.STREAM_STDOUT, env=env, cwd=dir) return out or ''
def _get_version(self): """Determine the linter's version by command invocation.""" success, cmd = self.context_sensitive_executable_path(self.executable) if isinstance(cmd, str): cmd = [cmd] cmd.append('--version') output = util.communicate(cmd) match = re.search(r"(\d+)\.(\d+)(?:\.(\d+))?", output) if not match: logger.info("failed to determine mypy version. output:\n%s", output) return () version = tuple(int(g) for g in match.groups() if g) logger.info("mypy version: %s", version) return version
def communicate(self, cmd, code=''): """ If the code that is linted is an HTML fragment, we wrap it into a valid HTML document. """ if code.find('<html') == -1 and code.find('<body') == -1: code = \ ("<!DOCTYPE html><html><head><title>Fragment</title></head><body>" + \ "%s" + \ "</body></html>") \ % code return util.communicate( cmd, code, output_stream=self.error_stream, env=self.env)
def communicate(self, cmd, code=None): """Customize Linter.communicate for pulling unnecessary file name off.""" settings = self.get_view_settings() cwd = self.get_working_dir(settings) env = self.get_environment(settings) if logger.isEnabledFor(logging.INFO): logger.info('{}: {} {}'.format( self.name, os.path.basename(self.filename or '<unsaved>'), cmd) ) if cwd: logger.info('{}: cwd: {}'.format(self.name, cwd)) return util.communicate( cmd, code, output_stream=self.error_stream, env=env, cwd=cwd)
def tmpdir(cmd, dir, files, filename, code, env=None): """Run an external executable using a temp dir filled with files and return its output.""" with tempfile.TemporaryDirectory(dir=dir, prefix=".golangci-lint-") as tmpdir: for f in files: target = os.path.join(tmpdir, f) f = os.path.join(dir, f) if os.path.basename(target) == os.path.basename(filename): # source file hasn't been saved since change, so update it from our live buffer with open(target, 'wb') as f: if isinstance(code, str): code = code.encode('utf8') f.write(code) else: os.link(f, target) out = util.communicate(cmd, output_stream=util.STREAM_BOTH, env=env, cwd=tmpdir) return out or ''