1+ from __future__ import print_function
12import os
2- import time
3+ from time import time
34import datetime
45import jenkins
56import socket
67from xml .etree import ElementTree
78
8- colors = {'blue' : '\033 [94m' ,
9+ COLORS = {'blue' : '\033 [94m' ,
910 'green' : '\033 [92m' ,
1011 'red' : '\033 [91m' ,
1112 'yellow' : '\033 [93m' ,
@@ -24,6 +25,14 @@ class CliException(Exception):
2425class JenkinsCli (object ):
2526 SETTINGS_FILE_NAME = '.jenkins-cli'
2627
28+ QUEUE_EMPTY_TEXT = "Building Queue is empty"
29+
30+ INFO_TEMPLATE = ("Last build name: %s (result: %s)\n "
31+ "Last success build name: %s\n "
32+ "Build started: %s\n "
33+ "Building now: %s\n "
34+ "%s branch set to: %s" )
35+
2736 def __init__ (self , args , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
2837 self .jenkins = self .auth (args .host , args .username , args .password , timeout )
2938
@@ -58,23 +67,23 @@ def read_settings_from_file(cls):
5867 for setting_line in jenkins_settings .split ('\n ' ):
5968 if "=" in setting_line :
6069 key , value = setting_line .split ("=" , 1 )
61- settings_dict [key ] = value
70+ settings_dict [key . strip () ] = value . strip ()
6271 return settings_dict
6372
6473 def run_command (self , args ):
6574 command = args .jenkins_command
6675 getattr (self , command )(args )
6776
6877 def jobs (self , args ):
69- jobs = self .jenkins . get_jobs ( )
78+ jobs = self ._get_jobs ( args )
7079 for job in jobs :
71- print ("%s***%s %s" % (colors .get (job ['color' ], job ['color' ]), colors ['endcollor' ], job ['name' ]))
80+ print ("%s***%s %s" % (COLORS .get (job ['color' ], job ['color' ]), COLORS ['endcollor' ], job ['name' ]))
7281
7382 def _get_jobs (self , args ):
7483 jobs = self .jenkins .get_jobs ()
75- if not args .d :
84+ if args .a :
7685 jobs = [j for j in jobs if j .get ('color' ) != 'disabled' ]
77- jobs = sorted (jobs , key = lambda j : j .get ('name' ))
86+ # jobs = sorted(jobs, key=lambda j: j.get('name'))
7887 return jobs
7988
8089 def queue (self , args ):
@@ -83,58 +92,63 @@ def queue(self, args):
8392 for job in jobs :
8493 print ("%s %s" % (job ['task' ]['name' ], job ['why' ]))
8594 else :
86- print ("Building Queue is empty" )
95+ print (self . QUEUE_EMPTY_TEXT )
8796
8897 def _check_job (self , job_name ):
8998 job_name = self .jenkins .get_job_name (job_name )
9099 if not job_name :
91100 raise CliException ('Job name does not esist' )
92101 return job_name
93102
103+ def _get_scm_name_and_node (self , xml_root ):
104+ scm_name = 'UnknownSCM'
105+ branch_node = None
106+ try :
107+ scm = xml_root .find ('scm' )
108+ if scm .attrib ['class' ] == 'hudson.plugins.mercurial.MercurialSCM' :
109+ scm_name = 'Mercurial'
110+ branch_node = scm .find ('revision' )
111+ elif scm .attrib ['class' ] == 'hudson.plugins.git.GitSCM' :
112+ scm_name = 'Git'
113+ branch_node = scm .find ('branches' ).find ('hudson.plugins.git.BranchSpec' ).find ('name' )
114+ except AttributeError :
115+ pass
116+ return (scm_name , branch_node )
117+
94118 def info (self , args ):
95119 job_name = self ._check_job (args .job_name )
96120 job_info = self .jenkins .get_job_info (job_name , 1 )
97121 if not job_info :
98122 job_info = {}
99123 last_build = job_info .get ('lastBuild' , {})
100124 last_success_build = job_info .get ('lastSuccessfulBuild' , {})
101- #from pprint import pprint
102- #pprint(job_info)
103- info = ("Last build name: %s (result: %s)\n "
104- "Last success build name: %s\n "
105- "Build started: %s\n "
106- "Building now: %s\n "
107- "Mercurial branch set: %s" )
108125 xml = self .jenkins .get_job_config (job_name )
109126 root = ElementTree .fromstring (xml .encode ('utf-8' ))
110- rev = 'Not Known'
111- scm = root . find ( 'scm' )
112- if scm is not None :
113- revision = scm . find ( 'revision' )
114- if revision is not None :
115- rev = revision . text
116- print ( info % ( last_build .get ('fullDisplayName ' , 'Not Built' ),
117- last_build .get ('result ' , 'Not Built' ),
118- last_success_build . get ( 'fullDisplayName' , 'Not Built' ) ,
119- datetime . datetime . fromtimestamp ( last_build [ 'timestamp' ] / 1000 ) if last_build else 'Not built ' ,
120- 'Yes' if last_build . get ( 'building' ) else 'No' ,
121- rev ))
127+ scm_name , branch_node = self . _get_scm_name_and_node ( root )
128+ if branch_node is not None :
129+ branch_name = branch_node . text
130+ else :
131+ branch_name = 'Unknown branch'
132+ print ( self . INFO_TEMPLATE % ( last_build . get ( 'fullDisplayName' , 'Not Built' ),
133+ last_build .get ('result ' , 'Not Built' ),
134+ last_success_build .get ('fullDisplayName ' , 'Not Built' ),
135+ datetime . datetime . fromtimestamp ( last_build [ 'timestamp' ] / 1000 ) if last_build else 'Not Built' ,
136+ 'Yes' if last_build . get ( 'building' ) else 'No ' ,
137+ scm_name ,
138+ branch_name ))
122139
123140 def set_branch (self , args ):
124141 job_name = self ._check_job (args .job_name )
125142 xml = self .jenkins .get_job_config (job_name )
126143 root = ElementTree .fromstring (xml .encode ('utf-8' ))
127- scm = root .find ('scm' )
128- new_xml = None
129- if scm is not None :
130- revision = scm .find ('revision' )
131- if revision is not None :
132- revision .text = args .branch_name
133- new_xml = ElementTree .tostring (root )
134- self .jenkins .reconfig_job (job_name , new_xml )
135- print ('Done' )
136- if new_xml is None :
137- print ("Can not set revision info" )
144+ scm_name , branch_node = self ._get_scm_name_and_node (root )
145+ if branch_node is not None :
146+ branch_node .text = args .branch_name
147+ new_xml = ElementTree .tostring (root )
148+ self .jenkins .reconfig_job (job_name , new_xml )
149+ print ('Done' )
150+ else :
151+ print ("Can't set branch name" )
138152
139153 def start (self , args ):
140154 for job in args .job_name :
@@ -146,8 +160,11 @@ def stop(self, args):
146160 job_name = self ._check_job (args .job_name )
147161 info = self .jenkins .get_job_info (job_name )
148162 build_number = info ['lastBuild' ].get ('number' )
149- stop_status = self .jenkins .stop_build (job_name , build_number )
150- print ("%s: %s" % (job_name , 'stoped' if not stop_status else stop_status ))
163+ if build_number and info ['lastBuild' ].get ('building' ):
164+ stop_status = self .jenkins .stop_build (job_name , build_number )
165+ print ("%s: %s" % (job_name , 'stopped' if not stop_status else stop_status ))
166+ else :
167+ print ("%s job is not running" % job_name )
151168
152169 def console (self , args ):
153170 job_name = self ._check_job (args .job_name )
@@ -173,17 +190,19 @@ def console(self, args):
173190 build_info = self .jenkins .get_build_info (job_name , build_number )
174191
175192 def building (self , args ):
176- args .d = False
193+ args .a = True
177194 jobs = [j for j in self ._get_jobs (args ) if 'anime' in j ['color' ]]
178195 if jobs :
179196 for job in jobs :
180197 info = self .jenkins .get_job_info (job ['name' ])
181198 build_number = info ['lastBuild' ].get ('number' )
199+ eta = "unknown"
200+ display_name = job ['name' ]
182201 if build_number :
183202 build_info = self .jenkins .get_build_info (job ['name' ], build_number )
184- eta = (build_info ['timestamp' ] + build_info ['estimatedDuration' ]) / 1000 - time .time ()
185- print ("%s estimated time left %s" % (build_info ['fullDisplayName' ],
186- datetime .timedelta (seconds = eta )))
203+ eta = (build_info ['timestamp' ] + build_info ['estimatedDuration' ]) / 1000 - time ()
204+ eta = datetime .timedelta (seconds = eta )
205+ display_name = build_info ['fullDisplayName' ]
206+ print ("%s estimated time left %s" % (display_name , eta ))
187207 else :
188208 print ("Nothing is building now" )
189-
0 commit comments