1

Does anyone knows how to use grep() command in P4Python?
I am developing an script that runs with Perforce and makes it easy for me to search for specific texts inside multiple files.

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.

I've searched in P4 grep documentation and P4Python APIs for Scripting but I could not find how to do this.

I've noticed that some commands you can use run_commandName, like:

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

And it works really well! But I cannot use P4().run_grep() =/

So, what I am trying to do is making a P4Python script. On Perfoce I made a Custom Toll like this:

Arguments: 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?

hadmung
  • 78
  • 1
  • 7

2 Answers2

0

Don't expect p4.run() to print to STDOUT. It's an API call - capture its return value (structured data) and process it.

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). 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.

sferencik
  • 3,144
  • 1
  • 24
  • 36
0

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; and `p4 grep -e test //path/to/folder/*' means that search in exactly the 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/*

recommend to use p4 command directly instead of p4python. sometimes, some line are too long, p4python merely raise exception; but p4 command can print out other result items and print the error message to stderr.

Doz Parp
  • 279
  • 4
  • 23