0

上下文:我正在尝试查找 python 包的 github 存储库。为此,我是 github url 的 zgrep'ping 包存档。它工作正常,直到我将输出限制为 1 个结果:

# works, returns a lot of results
subprocess.check_output(["zgrep", "-oha", "github", 'Django-1.10.1.tgz'])  #  works, a lot of results
# add -m1 to limit output, returns status 2 (doesn't work)
subprocess.check_output(["zgrep", "-m1", "-oha", "github", 'Django-1.10.1.tgz'])  #  works, a lot of results
# same command, different file - works
subprocess.check_output(["zgrep", "-m1", "-oha", "github", 'grabber.py'])

从命令行,所有三个命令都可以正常工作。有任何想法吗?

追溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 574, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['zgrep', '-m1', '-oha', 'github', 'pkgs/Django-1.10.1.tar.gz']' returned non-zero exit status 2

命令行:

$ zgrep -m1 -oha "github.com/[^/]\+/django" pkgs/Django-1.10.1.tar.gz
github.com/django/django
4

1 回答 1

0

所以,原因是:zgrep 是一个 shell 脚本,它只是通过 gzip 和 egrep 对归档进行管道传输。如果我们限制结果的数量,egrep 会终止管道,因此 gzip 会退出并抱怨。在控制台中我们永远看不到它,但是子进程以某种方式捕获了这个信号并引发了一个异常。

解决方法:写一个不会报错的迷你版zgrep

gunzip < $FILE 2> /dev/null | egrep -m1 -ohia $PATTERN
于 2016-09-29T19:59:21.770 回答