python - 在 Python 脚本中运行 git 命令会导致语法错误

标签 python bash git

当我运行时

git log  --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

在Linux终端中,输出正确:

added lines: 23322, removed lines: 8536, total lines: 14786

由于我不想记住这么复杂的命令,所以我编写了一个Python脚本来完成同样的事情:

import os
GitCommand = 'git log  --pretty=tformat: --numstat | awk "{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }"'
report = os.system(GitCommand)

但是当我运行它时,Git 报告语法错误:

awk: cmd. line:1: { add += $1; subs += $2; loc += $1 - $2 } END         { printf "added lines: %s, removed lines: %s, total lines: %s
awk: cmd. line:1:                                                                ^ unterminated string
awk: cmd. line:1: { add += $1; subs += $2; loc += $1 - $2 } END         { printf "added lines: %s, removed lines: %s, total lines: %s
awk: cmd. line:1:                                                                ^ syntax error

我也尝试过使用子进程,输出是类似的。问题可能在于命令字符串的编码,尤其是引号,但我不知道如何修复它。

最佳答案

Python 脚本

(本节直接回答问题,意味着获取一个 Python 脚本来完成提问者想要完成的任务。但是,shell 脚本可能更合适;请参阅“Shell 脚本”部分了解更多信息。)

我制作了一个Python脚本来完成你想要的。我通过采用您的脚本并进行了两项修改来制作此脚本:

  • 我将 awk 的唯一参数周围的预期双引号更改为转义单引号 \'
  • 我将文字换行符 \n 更改为“\n”的转义版本,即 \\n

以下是显示修改后的脚本工作的输出示例:

$ git log  --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
added lines: 5, removed lines: 1, total lines: 4

$ cat script.py
import os
GitCommand = 'git log  --pretty=tformat: --numstat | awk \'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\\n", add, subs, loc }\''
report = os.system(GitCommand)

$ python3 script.py
added lines: 5, removed lines: 1, total lines: 4

以下显示了 Python 脚本的差异,从问题的版本到此答案的工作版本:

$ git diff head~ head --word-diff-regex=. script.py
diff --git a/script.py b/script.py
[...]
--- a/script.py
+++ b/script.py
@@ -1,3 +1,3 @@
import os
GitCommand = 'git log  --pretty=tformat: --numstat | awk [-"-]{+\'+}{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: % , total lines: %s\{+\+}n", add, subs, loc }[-"-]{+\'+}'
report = os.system(GitCommand)

Shell 脚本

正如本问题其他地方提到的,拥有 shell 脚本文件可能是简单且重复地调用任何给定 shell 命令的最合适方法,无论出于何种原因,不希望将这些命令存储在诸如 ~/之类的内容中.bashrc~/.bash_profile 或类似文件。

专门针对这个问题,这里有一个例子:

$ git log  --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
added lines: 5, removed lines: 1, total lines: 4

$ cat ./total-lines.sh
git log --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

$ ./total-lines.sh
added lines: 5, removed lines: 1, total lines: 4

我的环境

$ systeminfo | grep --extended-regexp --regexp="^OS (Name|Version)"
OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.19043 N/A Build 19043

$ bash --version | head --lines=1
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)

$ git --version
git version 2.33.0.windows.2

$ python3 --version
Python 3.9.7

$ awk --version | head --lines=1
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)

关于python - 在 Python 脚本中运行 git 命令会导致语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69305595/

相关文章:

python - Django 使用对象自己的数据对多个更新进行建模?

git - 检查详细文件列表以推送 git

python 在文件中查找带有路径的图像

Python 相当于 Ruby 的 File.read 方法

bash - 使用 bash,您使用哪些脚本或命令来提高工作效率?

c++ - 执行 bash shell 命令并提取输出 --> 无效文件错误

linux - 转换时区并减去 x 时间量

git - git中有 "current branch"的字符吗?

git - git标签可以重用吗

python - 如何在Python爬虫中访问多页面表单的发布数据