#/usr/bin/python

import sys, os
import urllib2


def getdata(url, build):
    url = 'http://ci.voltdb.lan:8080/' + url + str(build) + "/api/python"
    #print url
    return eval(urllib2.urlopen(url).read())

if __name__ == '__main__':

    #DATA = {"_class":"hudson.model.FreeStyleBuild","actions":[{"_class":"hudson.model.ParametersAction","parameters":[{"_class":"hudson.model.StringParameterValue","name":"GIT_BRANCH","value":"origin/ENG-12931-PoisonPillInEE"},{"_class":"hudson.model.StringParameterValue","name":"GIT_REPO","value":"voltdb"},{"_class":"hudson.model.StringParameterValue","name":"REPOSITORY_VOLTDB","value":"git@github.com:VoltDB/voltdb.git"},{"_class":"hudson.model.StringParameterValue","name":"REPOSITORY_PRO","value":"git@github.com:VoltDB/pro.git"},{"_class":"hudson.model.StringParameterValue","name":"GIT_REPOSITORY_PRO","value":"git@github.com:VoltDB/pro.git"},{"_class":"hudson.model.StringParameterValue","name":"GIT_REPOSITORY_VOLTDB","value":"git@github.com:VoltDB/voltdb.git"}]},{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$UpstreamCause","shortDescription":"Started by upstream project \"SyncProjectBranchesWithJenkins\" build number 33,941","upstreamBuild":33941,"upstreamProject":"SyncProjectBranchesWithJenkins","upstreamUrl":"job/SyncProjectBranchesWithJenkins/"}]},{},{},{},{}],"artifacts":[],"building":False,"description":None,"displayName":"#31944.voltdb origin/ENG-12931-PoisonPillInEE","duration":23487,"estimatedDuration":24740,"executor":None,"fullDisplayName":"SyncStartBranchJobs #31944.voltdb origin/ENG-12931-PoisonPillInEE","id":"31944","keepLog":False,"number":31944,"queueId":13006,"result":"SUCCESS","timestamp":1501871991747,"url":"http://ci:8080/job/SyncStartBranchJobs/31944/","builtOn":"","changeSet":{"_class":"hudson.scm.EmptyChangeLogSet","items":[],"kind":None}}
    #print DATA
    DATA = getdata('job/'+os.environ['JOB_BASE_NAME'], '/'+ os.environ['BUILD_NUMBER'])

    affectedPaths = []
    while True:
        #for f in DATA.iteritems():
        #    print f
        # look at job's changeset
        cs = dict(DATA['changeSet'])

        if len(cs['items']) > 0:
            # process changes
            for i in cs['items']:
                affectedPaths.extend(i['affectedPaths'])
            print affectedPaths
            sys.exit(len(affectedPaths))

        else:
            actions = DATA['actions']
            for a in actions:
                if 'causes' in a:
                    upstream = a['causes']
                    if len(upstream) > 0:
                        upstream = upstream[0]
                        DATA = getdata(upstream['upstreamUrl'], str(upstream['upstreamBuild']))
                        break

@@ -1980,16 +1980,17 @@
 hs = %5B%5D%0A
+%0A
     whil
@@ -2054,16 +2054,17 @@
 print f%0A
+%0A
         
@@ -2190,16 +2190,47 @@
 changes%0A
+            affectedPaths = %5B%5D%0A
         
@@ -2348,68 +2348,9 @@
 ths%0A
-            sys.exit(len(affectedPaths))%0A%0A        else:%0A    
+%0A
     
@@ -2383,28 +2383,24 @@
 s'%5D%0A        
-    
 for a in act
@@ -2405,20 +2405,16 @@
 ctions:%0A
-    
         
@@ -2443,36 +2443,32 @@
                 
-    
 upstream = a%5B'ca
@@ -2494,38 +2494,41 @@
     
-    if len(upstream) %3E 0:%0A    
+if 'upstreamUrl' in upstream%5B0%5D:%0A
     
@@ -2566,24 +2566,56 @@
 ream%5B0%5D%0A    
+                #print upstream%0A
             
@@ -2718,14 +2718,86 @@
     
-    
 break
+%0A                else:%0A                    sys.exit(len(affectedPaths) == 0)
 %0A

update jenkins tools findchangedpaths.py