gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:14:15 27 4
gpt4 key购买 nike

我正在用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com