简体   繁体   中英

How can I checkout a file from a changelist on perforce server using Perforce Python API's (p4python)

Originally I am trying to implement a customized python based linter to help me out with the lexical code analysis. This linter requires me to point to the directory where the scripts or code files (which needed to be analyzed) resides. The original code (which needed to be analyzed) is present on a perforce server.

What I want to do is to be able to get all the files shelved by a user on perforce. How can i use the change-list number to get these shelved files from perforce server to my local machine and i want to do that in my python based application preferably using p4python api's.

All help is appreciated. Thanks

There are two options for getting files from a shelved changelist; one is easy but requires managing more persistent state on the server, and the other is more stateless but requires more logic on the client side.


The standard workflow (ie what most human Perforce users would do) is to unshelve the files into your client workspace:

p4 unshelve -s CHANGE

The files in the shelved changelist will be automatically synced to your workspace (according to your client view) and opened in your default changelist. This is the easy option because you just have to run that one command to have all the files in a predefined local location. From there you can modify them freely, and either submit them or reshelve them to a different changelist (or the same changelist if you modify it to make yourself the owner).

This requires that you have a workspace already set up, and that the files are not already open there, so if you're doing this in a script, the script will need to be configured ahead of time with a suitable workspace and it will need to make sure that at some point it either submits or reverts the changes, or it will need to know how to create its own workspace by running p4 client , picking a unique client root, etc.


The stateless option is to use p4 print to get the contents of the shelved changelist:

p4 print @=CHANGE

This will stream the files to stdout, with headers in between giving the depot path. If your linter can handle streaming data, this may be the easy option; if it needs to read files from disk, you'll have to implement the logic required to put the files in a suitable location, and clean them up when you're done.


Translating either of the above to p4python is pretty straightforward (I'd try to whip up a code sample but the p4python installer is currently broken on Windows so I haven't been able to use p4python in a while), but my recommendation is to familiarize yourself with the above commands at the terminal in a test environment first so you can better understand the interface and the requirements around workspace setup, authentication, etc. before you implement that logic in a script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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