Processing Perforce command output with python.
The Perforce command line client supports returning Python marshalled dictionary objects as command output via the ‘-G’ option. This functionality can be used to easily export data from Perforce to various formats to allow further analysis. The following script exports the submitted change lists returned by the p4 changes command to an CSV file which can easily be loaded into Microsoft Excel for analysis. The script also converts the returned timestamp into human readable date and time columns.
"""Perforce changes dumped into CSV format.""" import marshal, subprocess def Run( cmd ): """Run supplied perforce command and return output as a list of dictionaries.""" results =  cmd = "p4 -G %s" % cmd # Run the Perforce command using -G option to get results as Python marshalled dictionary objects. process = subprocess.Popen( cmd, stdout=subprocess.PIPE, shell=True ) # Harvest stdout output until end of file exception is thrown. try: while 1: output = marshal.load( process.stdout ) results.append( output ) except EOFError: pass finally: process.stdout.close() return results if __name__=="__main__": import csv, datetime # Perforce command to run, fetch the latest submitted changelists. cmd = "changes -L" # Get results. results = Run( cmd ) print "'p4 -G %s' - %d" % (cmd,len(results)) # Convert timestamp into human readable form. for result in results: result['timestamp'] = result['time'] result['date'] = datetime.datetime.fromtimestamp(int(result['time'])).strftime("%d/%m/%Y") result['time'] = datetime.datetime.fromtimestamp(int(result['time'])).strftime("%H:%M:%S") # Write results to file. f = open( 'p4.csv', 'wb' ) w = csv.DictWriter( f, results.keys() ) w.writeheader() for result in results: w.writerow( result ) f.close()
This script could also be used to export the results of most Perforce commands to CSV simply by changing the cmd string (line 25). It could also be adapted to export into a different format e.g. JSON, XML or even into a database like SQLite.