pythonjenkinsoutmassive

How to fetch python output and get to dynamic massive


I have got Jenkins and python, and some jobs in a queue. I have got a script whic

import jenkins 
j = jenkins.Jenkins('http:///', '....', '.......')
queue_info = j.get_queue_info()
print(queue_info)

This command give me the following output:

[{u'task': {u'url': u'http://', u'color': u'aborted', u'name': u'f1111111111'}, u'stuck': False, u'url': 
u'queue/item/37/', u'inQueueSince': 1397554800875L, u'actions': [{u'causes': [{u'userName': u'admin', u'userId': u'admin', u'shortDescription':
u'Started by user admin'}]}], u'why': u'Waiting for next available executor on NO_1', u'buildable': True, u'params': u'', u'buildableStartMilliseconds': 1397554800878L, u'id': 37, u'pending': False, u'blocked': False}

How to fetch the name of u'name' and get it to dynamic massive in python


Solution

  • The output is a list of dictionaries, so you can simply access the first entry in the list and use the dictionary's keys.

    Let's start by defining a function here:

    import subprocess as sp
    
    ''' Runs a program '''
    def run(prog):
          print("Lets run program %s" % prog)
          sp.Popen([prog], stdout=sp.STDOUT, stderr=sp.STDOUT)
    
          # This makes the script wait until the end of the program
          sp.wait()
    

    Then, in the for loop, you just need to call the run function:

    for n in queue_info:
          run(n['task']['name'])