我正在实现一个小的预提交钩子,它在每次提交之前调用git泄漏保护。
这在终端中运行得很好,但是当试图从VSCode内部提交时,返回一个非描述性的"Git: O“(我假设这只是它的ascii标识的一部分)。
如您所知,我尝试过多种方法让VSCode的Git模块在子模块退出时返回正确的消息。然而,在这方面似乎没有任何效果。
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
exit_code = subprocess.run("gitleaks protect -v --staged -c gitleaks.toml",shell=True)
if exit_code.returncode == 1:
eprint("This is a test")
sys.exit("TEST")
如何在VSCode中返回一个警报窗口,当子进程与退出代码1一起退出时,该窗口将显示一条消息?
编辑:
好的。这在某种程度上是可行的,但是它失败了,因为subprocess.run("gitleaks version", shell=True, stdout=dev_null, stderr=dev_null)
只适用于我的WSL Bash,而subprocess.run("gitleaks version", stdout=dev_null, stderr=dev_null)
(没有shell=True)只适用于使用Windows的VSCode。
有任何方法使这个可移植,所以FileNotFoundError正确地抛到两个系统上?
#!/usr/bin/env python3
# pylint: disable=C0116,W0613
import sys
import warnings
import subprocess
dev_null = subprocess.DEVNULL
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def gitleaks_installed():
try:
subprocess.run("gitleaks version", shell=True, stdout=dev_null, stderr=dev_null)
return True
except FileNotFoundError:
return False
if gitleaks_installed():
exit_code = subprocess.run("gitleaks protect -v --staged -c gitleaks.toml", shell=True, stdout=dev_null, stderr=dev_null)
if exit_code.returncode == 1:
eprint("gitleaks has detected sensitive information in your changes. Commit aborted.")
subprocess.run("gitleaks protect -v --staged -c gitleaks.toml", shell=True)
sys.exit(1)
else:
eprint("gitleaks is not installed or in the PATH.")
sys.exit(1)
EDIT2: NVM.gitleaks_installed
部件在WSL下根本不工作。它要么总是正确的,要么总是假的,这取决于我是否包括shell=True
。
是否有更好的方法来检测是否安装了/是否在路径中?
发布于 2022-01-20 13:33:28
subprocess.run
返回的对象是CompletedProcess对象,而不是返回代码。您必须访问它的.returncode
属性来检查它返回的内容。
但是,您可以添加check=True
,以便在失败时抛出一个错误。
通过自己将命令行解析为令牌,您几乎可以肯定地摆脱了多余的shell=True
。
try:
subprocess.run(
["gitleaks", "protect", "-v", "--staged", "-c", "gitleaks.toml"],
check=True)
except subprocess.CalledProcessError:
eprint("gitleaks has detected sensitive information in your changes. Commit aborted.")
sys.exit(1)
except FileNotFoundError:
eprint("gitleaks is not installed or in the PATH.")
sys.exit(1)
这也避免了仅仅为了获得输出而再次运行该进程。如果您想强迫自己的消息位于顶部,请捕获输出并在消息之后打印出来(添加capture_output=True
和text=True
)。
您还可能希望将eprint
替换为logging.warn
,并在不同的错误场景中返回不同的退出代码。(Bash通常在找不到二进制文件时返回127,但这只是Bash。)
在一个字符串上运行subprocess.run
,而不是在一个列表上运行,而不是在shell=True
上运行,这很奇怪,但我的建议是永远避免为了方便而试图利用它。也许也见Actual meaning of shell=True
in subprocess
发布于 2022-01-20 12:53:31
不是最优雅的解决方案,但这是可行的。我们使用的不是FileNotFoundError
异常,而是subprocess.run
返回代码。
#!/usr/bin/env python3
# pylint: disable=C0116,W0613
import sys
import subprocess
dev_null = subprocess.DEVNULL
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def gitleaks_installed():
exit_code = subprocess.run("gitleaks version", shell=True, stdout=dev_null, stderr=dev_null)
if exit_code.returncode == 0:
return True
else:
return False
if gitleaks_installed():
exit_code = subprocess.run("gitleaks protect -v --staged -c gitleaks.toml", shell=True, stdout=dev_null, stderr=dev_null)
if exit_code.returncode == 1:
eprint("gitleaks has detected sensitive information in your changes. Commit aborted.")
subprocess.run("gitleaks protect -v --staged -c gitleaks.toml", shell=True)
sys.exit(1)
else:
eprint("gitleaks is not installed or in the PATH.")
sys.exit(1)
https://stackoverflow.com/questions/70781897
复制相似问题