python - 将字符串作为文件传递给 rubocop

标签 python git bash unix

我正在用 python 编写一个 git-hook,它会对推送到存储库的文件进行 lints,如果文件未通过 linting 要求,则拒绝推送。我已经使用 php 代码嗅探器为 php 编写了一个类似的钩子(Hook),代码如下所示:

php_cs = subprocess.Popen(['phpcs', '--standard=' + self._standard], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = php_cs.communicate(input="file contents".encode('utf-8'))

此代码适用于 php_cs,但也适用:

cat file.php | phpcs

据我所知,这是Python代码正在做的所有事情。我想使用 RuboCop 对 ruby​​ 做一些非常类似的事情。

使用与 phpcs 非常相似的 python 代码,rubocop 版本如下所示:

  rubo_cop = subprocess.Popen(['RuboCop'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, err = rubo_cop.communicate(input="file contents".encode('utf-8'))

本例中的输出如下所示:

enter image description here

这表明 rubocop 不以 phpcs 理解的方式理解此输入,这就是原因:

 cat file.rb | rubocop 

不适用于 rubocop

我的问题如何将字符串传递给命令,以便它将其视为文件?

最佳答案

尝试 cat file.php | xargs rubocop

这是我为 Rails 项目准备的预提交脚本 https://gist.github.com/qr8r/70510a0bc0cb54fd8427

关于python - 将字符串作为文件传递给 rubocop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28886328/

相关文章:

regex - Find 命令中的正则表达式

python - 使用 Python(正则表达式)将数据仅放在数据框中的方括号中

git - 当 get 没有针对远程分支的跟踪信息时,这意味着什么?

git - `git checkout` 未重置文件

git - Visual Studio Online 计划生成找不到我的分支

bash - bash 中的代码块用法 { }

使用 pickle.load() 时 python3 退出时没有任何错误

python - 将整数转换为给定字母表上的字符串的算法

python - 使用 sqlite3 db 缺失表对 django 项目进行单元测试

bash - 在 bash "for file in $list"构造中一次取两个