Skip to content

Commit 0a74058

Browse files
authored
Merge pull request #31 from radomirbosak/fix-console-output
Fix console output && add a --interval CLI option
2 parents b1a5965 + 91154ad commit 0a74058

3 files changed

Lines changed: 87 additions & 56 deletions

File tree

jenkins_cli/__init__.py

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,15 @@
11
from __future__ import print_function
22

3-
import argparse
43
from jenkins import JenkinsException
54

6-
from jenkins_cli.cli import JenkinsCli, CliException, get_jobs_legend
7-
from jenkins_cli.version import version
5+
from jenkins_cli.cli import JenkinsCli, CliException
6+
from jenkins_cli.cli_arguments import load_parser
87

98

109
def main():
11-
parser = argparse.ArgumentParser(prog='jenkins',
12-
description='Host, username and password may be specified either by the command line arguments '
13-
'or in the configuration file (.jenkins-cli). Command line arguments have the highest priority, '
14-
'after that the .jenkins-cli file from current folder is used. If there is no'
15-
'.jenkins-cli file in the current folder, settings will be read from .jenkins-cli located in the home'
16-
'folder')
17-
parser.add_argument('--host', metavar='jenkins-url', help='Jenkins Host', default=None)
18-
parser.add_argument('--username', metavar='username', help='Jenkins Username', default=None)
19-
parser.add_argument('--password', metavar='password', help='Jenkins Password', default=None)
20-
parser.add_argument('--version', '-v', action='version', version='jenkins-cli %s' % version)
21-
22-
subparsers = parser.add_subparsers(title='Available commands', dest='jenkins_command')
23-
24-
jobs_parser = subparsers.add_parser('jobs',
25-
help='Show all jobs and their statuses',
26-
formatter_class=argparse.RawTextHelpFormatter,
27-
description="Status description:\n\n" + "\n".join(get_jobs_legend()))
28-
jobs_parser.add_argument('-a', help='show only active jobs', default=False, action='store_true')
29-
jobs_parser.add_argument('-p', help='show only jobs in build progress', default=False, action='store_true')
30-
31-
subparsers.add_parser('queue', help='Show builds queue')
32-
33-
subparsers.add_parser('building', help='Build executor status')
34-
35-
builds_parser = subparsers.add_parser('builds', help='Show builds for the job')
36-
builds_parser.add_argument('job_name', help='Job name of the builds')
37-
38-
start_parser = subparsers.add_parser('start', help='Start job')
39-
start_parser.add_argument('job_name', help='Job to start', nargs='*')
40-
41-
start_parser = subparsers.add_parser('info', help='Job info')
42-
start_parser.add_argument('job_name', help='Job to get info for')
43-
44-
set_branch = subparsers.add_parser('setbranch', help='Set VCS branch (Mercurial or Git)')
45-
set_branch.add_argument('job_name', help='Job to set branch for')
46-
set_branch.add_argument('branch_name', help='Name of the VCS branch')
47-
48-
stop_parser = subparsers.add_parser('stop', help='Stop job')
49-
stop_parser.add_argument('job_name', help='Job to stop')
50-
51-
console_parser = subparsers.add_parser('console', help='Show console for the build')
52-
console_parser.add_argument('job_name', help='Job to show console for')
53-
console_parser.add_argument('-b', '--build', help='job build number to show console for (if omitted, last build number is used)', default='')
54-
console_parser.add_argument('-n', help='show first n lines only(if n is negative, show last n lines)', type=int)
55-
console_parser.add_argument('-i', help='interactive console', default=False, action='store_true')
56-
57-
console_parser = subparsers.add_parser('changes', help="Show build's changes")
58-
console_parser.add_argument('job_name', help='Job to show changes for')
59-
console_parser.add_argument('-b', '--build', help='job build number to show changes for (if omitted, last build number is used)', default='')
60-
10+
parser = load_parser()
6111
args = parser.parse_args()
12+
6213
try:
6314
if args.jenkins_command is None:
6415
parser.print_help()

jenkins_cli/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def console(self, args):
294294
print("Cannot show console output. %(job_name)s has no builds" % {'job_name': job_name})
295295
else:
296296
console_out = self.jenkins.get_build_console_output(job_name, build_number)
297-
console_out = console_out.split('\n')
297+
console_out = console_out.splitlines()
298298
last_line_num = len(console_out)
299299
if args.n:
300300
console_out = console_out[args.n:] if args.n < 0 else console_out[:args.n]
@@ -303,12 +303,12 @@ def console(self, args):
303303
build_info = self.jenkins.get_build_info(job_name, build_number)
304304
while build_info['building']:
305305
console_out = self.jenkins.get_build_console_output(job_name, build_number)
306-
console_out = console_out.split('\n')
306+
console_out = console_out.splitlines()
307307
new_line_num = len(console_out)
308308
if new_line_num > last_line_num:
309309
print("\n".join(console_out[last_line_num:]))
310310
last_line_num = new_line_num
311-
sleep(3)
311+
sleep(args.interval)
312312
build_info = self.jenkins.get_build_info(job_name, build_number)
313313

314314
def building(self, args):

jenkins_cli/cli_arguments.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import argparse
2+
3+
from jenkins_cli.cli import get_jobs_legend
4+
from jenkins_cli.version import version
5+
6+
7+
def load_parser():
8+
"""
9+
Create a parser and load it with CLI arguments
10+
11+
Returns: ArgumentParser instance
12+
"""
13+
parser = argparse.ArgumentParser(prog='jenkins',
14+
description='Host, username and password may be specified either by the command line arguments '
15+
'or in the configuration file (.jenkins-cli). Command line arguments have the highest priority, '
16+
'after that the .jenkins-cli file from current folder is used. If there is no'
17+
'.jenkins-cli file in the current folder, settings will be read from .jenkins-cli located in the home'
18+
'folder')
19+
parser.add_argument('--host', metavar='jenkins-url', help='Jenkins Host', default=None)
20+
parser.add_argument('--username', metavar='username', help='Jenkins Username', default=None)
21+
parser.add_argument('--password', metavar='password', help='Jenkins Password', default=None)
22+
parser.add_argument('--version', '-v', action='version', version='jenkins-cli %s' % version)
23+
24+
subparsers = parser.add_subparsers(title='Available commands', dest='jenkins_command')
25+
26+
jobs_parser = subparsers.add_parser('jobs',
27+
help='Show all jobs and their statuses',
28+
formatter_class=argparse.RawTextHelpFormatter,
29+
description="Status description:\n\n" + "\n".join(get_jobs_legend()))
30+
jobs_parser.add_argument('-a', help='show only active jobs', default=False, action='store_true')
31+
jobs_parser.add_argument('-p', help='show only jobs in build progress', default=False, action='store_true')
32+
33+
subparsers.add_parser('queue', help='Show builds queue')
34+
35+
subparsers.add_parser('building', help='Build executor status')
36+
37+
builds_parser = subparsers.add_parser('builds', help='Show builds for the job')
38+
builds_parser.add_argument('job_name', help='Job name of the builds')
39+
40+
start_parser = subparsers.add_parser('start', help='Start job')
41+
start_parser.add_argument('job_name', help='Job to start', nargs='*')
42+
43+
start_parser = subparsers.add_parser('info', help='Job info')
44+
start_parser.add_argument('job_name', help='Job to get info for')
45+
46+
set_branch = subparsers.add_parser('setbranch', help='Set VCS branch (Mercurial or Git)')
47+
set_branch.add_argument('job_name', help='Job to set branch for')
48+
set_branch.add_argument('branch_name', help='Name of the VCS branch')
49+
50+
stop_parser = subparsers.add_parser('stop', help='Stop job')
51+
stop_parser.add_argument('job_name', help='Job to stop')
52+
53+
console_parser = subparsers.add_parser('console', help='Show console for the build')
54+
console_parser.add_argument('job_name', help='Job to show console for')
55+
console_parser.add_argument('-b', '--build', help='job build number to show console for (if omitted, last build number is used)', default='')
56+
console_parser.add_argument('-n', help='show first n lines only(if n is negative, show last n lines)', type=int)
57+
console_parser.add_argument('-i', help='interactive console', default=False, action='store_true')
58+
console_parser.add_argument('-t', '--interval', help='refresh interval in seconds (in case of interactive console -i)', default=3, type=check_nonnegative)
59+
60+
console_parser = subparsers.add_parser('changes', help="Show build's changes")
61+
console_parser.add_argument('job_name', help='Job to show changes for')
62+
console_parser.add_argument('-b', '--build', help='job build number to show changes for (if omitted, last build number is used)', default='')
63+
64+
return parser
65+
66+
67+
def check_nonnegative(value):
68+
"""
69+
Checks if (possibly string) value is non-negative integer and returns it.
70+
71+
Raise:
72+
ArgumentTypeError: if value is not a non-negative integer
73+
"""
74+
try:
75+
ivalue = int(value)
76+
if ivalue < 0:
77+
raise ValueError()
78+
except:
79+
raise argparse.ArgumentTypeError("Value must be a non-negative integer: %s" % value)
80+
return ivalue

0 commit comments

Comments
 (0)