#! /usr/bin/perl =comment Example script illustrating how to use P4-Perl to convert 'p4 changes' output into a CSV file. $Id: //guest/jason_gibson/misc/changes_csv.pl#1 $ $DateTime: 2010/12/28 11:05:22 $ $Author: jason_gibson $ $Change: 7837 $ =cut =comment Copyright (c) 2010, Perforce Software, Inc. 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. =cut =comment 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. =cut use warnings; use strict; use Carp; use P4; use Data::Dumper; # Text::CSV::Slurp requires Text::CSV. We use Slurp over the plain module # because it auto-adds the field header and accepts the $p4->Run() output as-is. # # http://search.cpan.org/~robbiebow/Text-CSV-Slurp-0.8/lib/Text/CSV/Slurp.pm # http://search.cpan.org/~makamaka/Text-CSV-1.21/lib/Text/CSV.pm # # To use without installing to Perl's system directory, do this: # # tar xzf Text-CSV-Slurp-0.8.tar.gz # cd Text-CSV-Slurp-0.8 # perl Makefile.PL # cd .. # # Repeat those steps for Text::CSV, then run the script: # # perl -I Text-CSV-1.21/lib -I Text-CSV-Slurp-0.8/lib changes_csv.pl > out.csv # use Text::CSV::Slurp; sub p4_emsg { return join '', map { $_->GetText() } $_[ 0 ]->Errors, $_[ 0 ]->Warnings } sub p4_errs { return $_[ 0 ]->WarningCount() + $_[ 0 ]->ErrorCount() } sub check { croak p4_emsg $_[ 0 ] if p4_errs $_[ 0 ] } ################################################################################ my $p4 = new P4; $p4->Connect or die p4_emsg $p4; my $changes = $p4->Run( 'changes', '-l', '-t', '-m', 100 ); check $p4; $p4->Disconnect(); # Convert epoch time into text. E.g. 1292610186 -> Fri Dec 17 10:23:06 2010 # map { $_->{ time } = ( scalar localtime( $_->{ time } ) ) } @$changes; # To limit output to certain fields, supply create() with a list specifying # the ones you want. E.g.: field_order => [ 'change', 'user', 'status' ] # print Text::CSV::Slurp->create ( input => $changes, binary => 1, always_quote => 1 ) . "\n"; exit 0;