#!/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.workshop.perforce.com' p4port = 'workshop.perforce.com:1666' p4user = 'matt_attaway' project_field = 'Project' '''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['id'] ) 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''' values_seen = False for line in lines: if line.startswith(pf_start) and not values_seen: updated_lines.append( pf_start + ' ' + projects[:-1] ) values_seen = True else: updated_lines.append( line ) print( '\n'.join(updated_lines) ) # print( P4('jobspec -i', port=p4port, user=p4user, form=('\n'.join(updated_lines)) ) )
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 9106 | Matt Attaway |
Update trigger to use a better project name. Don't flood the preset. Previously I was using name, but that doesn't include our prepended user name. Id is a much better call. it also only sets the values field, not the presets field. |
||
#2 | 8505 | Matt Attaway |
Use '-' to replace '/' instead of '|' I forgot that '|' is a special character in the job query langauge. '-' should be safe. |
||
#1 | 8489 | Matt Attaway |
WIP on a script to update a Perforce jobspec with the list of Swarm projects Running this script as a trigger should allow Swarm projects to drive a list of projects in Perforce jobs that users can file issues against. The script only updates one select list so should work in conunction with any other manual changes that are made. |