0

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)

Michael McMullin
  • 1,450
  • 1
  • 14
  • 25
Bryant
  • 59
  • 10

1 Answers1

2

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.

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • The easier the better currently I'm reading the names from a text file and then creating the users that way using the command I showed above is there a way to use the command set the authentication to LDAP without opening the temp file? Or if the P4python is the best option how do I go about setting it up – Bryant Sep 06 '18 at 01:21
  • Yup, either of the options I outlined above will work for creating users with whatever options you want! Can't give good code samples in these tiny comment fields so feel free to post a new and more specific question if you'd like an answer more geared to what you're trying to do specifically. :) – Samwise Sep 06 '18 at 07:00