#!/usr/bin/env python # Copyright (c) 2013, Matthew Attaway # All rights reserved. # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # # Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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. ''' This script updates the Perforce job spec with the list of projects in Swarm so that they can easily have jobs filed against them. The field used is configurable. ''' ''' Method to run a Perforce command, optionally in a nice dictionary. Note that the marshaled output only works with Python 2.7 because we can't have nice things. ''' import subprocess, marshal def P4( cmd, port="", user="", client="", p4="/usr/local/bin/p4", marshaled=True, form=0 ): results = [] c = p4 '''build up the command''' if port != "": c+=" -p %s " % port if user != "": c+=" -u %s " % user if client != "": c+=" -c %s " % client if marshaled and not form: c+=' -G ' c+= "%s" % cmd '''if we're sending in a form, fire it off and run''' if form: process = subprocess.Popen( c, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) output = process.communicate(input=form)[0] results.append( output ) return results '''otherwise we're running a normal command''' process = subprocess.Popen( c, stdout=subprocess.PIPE, shell=True ) try: while 1: if marshaled: output = marshal.load( process.stdout ) else: output = process.stdout.read() if output == "": break results.append( output ) except EOFError: pass finally: process.stdout.close() return results if __name__=="__main__": import json, requests, re '''change these to work with your installation''' swarm_url = 'https://swarm.perforce.com' p4port = 'qaplay.perforce.com:1999' p4user = 'matt' project_field = 'Subsystem' '''grab all of the projects and escape the output to make it safe for a p4 job select field''' projects = "" data = requests.get(url=swarm_url+'/projects').json() for project in data: if project['deleted'] != 'false': name = re.sub( '/','-', project['name'] ) name = re.sub(' ', '_', name) projects+=name + '/' '''grab the current jobspec''' results = P4('jobspec -o', port=p4port, user=p4user, marshaled=False) lines = results[0].splitlines() updated_lines = [] pf_start = "\t%s" % project_field '''update the list of projects''' for line in lines: if line.startswith(pf_start): updated_lines.append( pf_start + ' ' + projects[:-1] ) else: updated_lines.append( line ) print( P4('jobspec -i', port=p4port, user=p4user, form=('\n'.join(updated_lines)) ) )