简体   繁体   English

将 p4python 与 grep 命令一起使用

[英]Using p4python with grep command

Does anyone knows how to use grep() command in P4Python?有谁知道如何在 P4Python 中使用grep()命令?
I am developing an script that runs with Perforce and makes it easy for me to search for specific texts inside multiple files .我正在开发一个与 Perforce 一起运行的脚本,它使我可以轻松地在多个文件中搜索特定文本

I already tried to create a tool in Perforce > Tools > Manage Custom Tools using P4 commands like p4 grep -n -B 1 -e text_searched %D but since I want to make the same search in multiple files, it will not work.我已经尝试使用p4 grep -n -B 1 -e text_searched %D等 P4 命令在 Perforce > 工具 > 管理自定义工具中创建一个工具,但由于我想在多个文件中进行相同的搜索,所以它不起作用。

I've searched in P4 grep documentation and P4Python APIs for Scripting but I could not find how to do this.我已经在P4 grep 文档P4Python APIs 中搜索过脚本,但我找不到如何做到这一点。

I've noticed that some commands you can use run_commandName , like:我注意到有些命令可以使用run_commandName ,例如:

from P4 import sys, P4, P4Exception
    p4 = P4()
    p4.run_integrated(fileName)

And it works really well!而且效果非常好! But I cannot use P4().run_grep() =/但我不能使用P4().run_grep() =/

So, what I am trying to do is making a P4Python script.所以,我想做的是制作一个 P4Python 脚本。 On Perfoce I made a Custom Toll like this:在 Perfoce 上,我做了一个这样的自定义收费:

Arguments: C:\\Users\\hmunguba\\Projects\\P4\\scripts\\searchp4pythonscript.py $u $p $c %D参数:C:\\Users\\hmunguba\\Projects\\P4\\scripts\\searchp4pythonscript.py $u $p $c %D

And my code is something like:我的代码是这样的:

from P4 import sys, P4, P4Exception

p4 = P4()
p4.user = sys.argv[1]
p4.port = sys.argv[2]
p4.client = sys.argv[3]

p4.connect()

FILE = str(sys.argv[4])
SEARCH_TEXT = sys.argv[5]

try:
    p4.run("grep", "-e ", SEARCH_TEXT, FILE)
except P4Exception:
    for e in p4.errors:
        print e
finally:
    p4.disconnect()

But the answer I get from this is always a blank screen.但是我从中得到的答案始终是空白屏幕。 Can anyone help me with it?任何人都可以帮助我吗?

Don't expect p4.run() to print to STDOUT . 不要指望p4.run()打印到STDOUT It's an API call - capture its return value (structured data) and process it. 这是一个API调用 - 捕获其返回值(结构化数据)并对其进行处理。

I believe that by convention of the API, you should be able to use p4.run("grep", ARGS) as well as p4.run_grep(ARGS) . 我相信按照惯例,您应该能够使用p4.run("grep", ARGS)以及p4.run_grep(ARGS) It's probably a bug if it's not working. 如果它不起作用,它可能是一个错误。 Please recheck if this is really so after you've managed to get your script working. 在您设法使脚本正常工作之后,请重新检查是否真的如此。

for the question, you may miss suffix like '...';对于这个问题,您可能会错过像“...”这样的后缀; for example, p4 grep -e test //path/to/folder/... means that search under //path/to/folder recursively;例如p4 grep -e test //path/to/folder/...表示在//path/to/folder递归搜索; and `p4 grep -e test //path/to/folder/*' means that search in exactly the folder.和`p4 grep -e test //path/to/folder/*' 意味着精确地搜索文件夹。

and if your folder is large, it may raise too-many revision error;如果您的文件夹很大,可能会引发过多的修订错误; need to use p4 dirs to get sub folders and search for //path/to/folder/{foreach sub folder}/... and //path/to/folder/*需要使用p4 dirs来获取子文件夹并搜索//path/to/folder/{foreach sub folder}/...//path/to/folder/*

recommend to use p4 command directly instead of p4python.建议直接使用p4命令而不是 p4python。 sometimes, some line are too long, p4python merely raise exception;有时,某些行太长,p4python 只是引发异常; but p4 command can print out other result items and print the error message to stderr.但是 p4 命令可以打印出其他结果项并将错误消息打印到 stderr。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM