Search code examples
pythoncommand-lineperforcep4v

Closing Documents During Runtime


I am using Python to create a script to run perforce commands in command prompt. One of the commands opens a file. I want to write to the bottom of the file, then save the file, and then close the file after. I do not know what the file will be named it is different every time and the command is run 5 or more times for each time I run the program. The Command is: os.system('p4 user -f ' + name)


Solution

  • It sounds like what you're actually trying to do is modify a Perforce user spec. This is super easy. If you use P4Python you have a nice API to the spec objects:

    https://www.perforce.com/perforce/r14.2/manuals/p4script/python.p4.html#python.p4.save_spectype

    and can do things like:

    user = p4.get_user(name)
    # do things
    p4.save_user(user)
    

    If you want to script without P4Python, the basic command line has much better options than trying to script around the editor. For example:

    p4 --field Reviews+=//depot/whatever/... user -o NAME | p4 user -i -f
    

    will add //depot/whatever/... to the bottom of NAME's Reviews field.

    Or if you don't like the --field option you can parse and modify the spec yourself:

    p4 user -o NAME | **REGEX MAGIC** | p4 user -i -f
    

    If you really want to script a command like p4 user and not use a scripting API or the convenient -o/-i/ hooks that avoid having it invoke an editor with a temp file (in other words if you're a scripting masochist looking for a unique challenge), your best bet is to override P4EDITOR and have it point to a script that you've written. Whatever executable is specified by P4EDITOR will get invoked with the temp file name; the modified spec will be uploaded (from the temp file) as soon as P4EDITOR exits.