PrintZip.py #3

  • //
  • guest/
  • sven_erik_knop/
  • P4Pythonlib/
  • scripts/
  • PrintZip.py
  • View
  • Commits
  • Open Download .zip Download (4 KB)
#!/usr/bin/env python 
#
# Copyright (c) 2010, Sven Erik Knop, Perforce Software. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
# 
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# User contributed content on the Perforce Public Depot is not supported by Perforce,
# although it may be supported by its author. This applies to all contributions 
# even those submitted by Perforce employees.
#
# For any questions or problems please contact me at sknop@perforce.com 
# 
# python script to emulate a potential 'p4 zip'
#
# Usage: python p4zip.py [options] archive file[revRange] ...
#   options: 
#           -p [--port] P4PORT
#           -c [--client] P4CLIENT, used as the view template


import P4
import zipfile
from optparse import OptionParser

class PrintZip:
    def __init__( self, options, filename ):
        self.options = options
        self.filename = filename
        self.p4 = P4.P4()
    
    def process(self, args):
        self.zipfile = zipfile.ZipFile(self.filename, 'w', zipfile.ZIP_DEFLATED, True)
        try:
            if self.options.port: self.p4.port = self.options.port
            if self.options.client: self.p4.client = self.options.client
            if self.options.manifest: self.manifest = ""
            
            self.p4.connect()
            self.setupMap()
            
            for arg in args:
                print( "processing %s" % arg )
            result = self.p4.run_print(arg)
            while len(result) > 0:
                # bit of Python black magic to shift the first two elements out
                (dname, content), result[:2] = result[:2], []
                fname = self.map.translate(dname['depotFile'])
                if self.options.manifest:
                    manifestEntry = dname['depotFile'] + '#' + dname['rev']
                    self.manifest += manifestEntry + '\n'
            
                self.zipfile.writestr(fname, content)
        except P4.P4Exception as e:
            print( e )
        except Exception as e:
            print( "Something failed: " + str(e))
            raise
        finally:
            self.p4.disconnect()
            if self.options.manifest:
                self.zipfile.writestr("Manifest", self.manifest)
            self.zipfile.close()
    
    def setupMap(self):
        cl = self.p4.fetch_client()
        m1 = P4.Map(cl._view)
        m2 = P4.Map("//" + cl._client + "/... ...")
        self.map = P4.Map.join(m1, m2)
    
if __name__ == "__main__":
    usage = "usage: %prog [options] archive file[revRange] ..."
    version = "1.1"
    
    parser = OptionParser(usage, version=version)
    parser.add_option("-p", "--port", dest="port", help="P4PORT")
    parser.add_option("-c", "--client", dest="client", help="P4CLIENT")
    parser.add_option("-m", "--manifest", action="store_true", dest="manifest", help="Add manifest file")
    (options, args) = parser.parse_args()
    if( len(args) < 2 ):
        parser.error("incorrect number of arguments")
    
    p = PrintZip(options, args[0])
    p.process(args[1:])
    
# Change User Description Committed
#3 7836 Sven Erik Knop Added option -m (--manifest) to add a manifest file that lists files and revisions in the zip file.
#2 7809 Sven Erik Knop Updated the supportability notice and added my email address.
Also updated the usage comments.
#1 7781 Sven Erik Knop Renamed p4zip.py to PrintZip.py.
Names starting with p4 are reserved for actual products.
//guest/sven_erik_knop/P4Pythonlib/scripts/p4zip.py
#1 7646 Sven Erik Knop Added p4zip.py, a simple script that creates zip files from depot files without the need
to load the files into the workspace first.

Using 'p4 print', p4zip will store the specified files in a zip file. The files names are mapped to local
names using the workspace view mapping. Note that you do not have to be in the workspace to do so.

Usage: p4zip.py [-c|--client client] [-p|--port port] zipfile file[revRange] ...

The workspace view map is used to transform the depot files into local files. For example, if the workspace
has the following view

  //depot/Jam/MAIN/src/... //ws/Jam/MAIN/src/...

then the following command

  p4zip.py --client ws //depot/Jam/MAIN/src/...

would transform the file '//depot/Jam/MAIN/src/compile.c' into 'Jam/MAIN/src/compile.c' in the zip file.

No local files are created but the zip file. Note that the script could consume a lot of memory if the files
to be loaded into the zip files are very large.