November 16, 2010

PrintZip.py

Integration
Traceability

I have recently been asked if Perforce could provide a command that would allow users to extract files and dump them into an archive (such as zip) instead of having to sync them to a workspace first and then place them into an archive. Such a command could be useful for build engineers and persons responsible for delivering results to external sites.

It turns out that it is easy to create such a command as a script, with the additional advantage that the archive format is flexible. I have written such a script using P4Python for zip - the concept could easily be expanded to support tar and other formats as well.

The basic idea here is to use 'p4 print' to extract the content of the files to be archived, intercept the output and place the files into an archive instead. The only problem with this idea is that 'p4 print' returns the depot path name - and this is not usable for a portable archive format, which needs the files in a relative path.

Perforce provides the perfect tool for this requirement: a client workspace. Instead of sync'ing the files to the workspace, we simply use the client workspace view mapping to determine which files should be archived and where they are supposed to be placed in relation to the archive root.

The resulting tool can be found on the Perforce public depot here: //guest/sven_erik_knop/P4Pythonlib/scripts/PrintZip.py. It requires Python 2.6 and P4Python 2009.2 or higher. To use the script, call it in the following way:

python PrintZip.py [options] archive-file filepattern ...

The only options currently supported are the client workspace ('-c' or '--client') and the Perforce port('-p' or '--port').

The client workspace is only used for its view and does not have to be owned by the user or linked to the user's host. If you look through the code of the script, you'll find an interesting using of the Map class again: I use it to map the client workspace view to the relative path used by Python's ZIP library.

Let me know if you find this tool interesting, and I might extend it to other archive types.

Happy hacking

Sven Erik