| 1 |
#!/usr/bin/env python |
| 2 |
# Copyright (C) 2018 Sorin Sbarnea |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
import logging |
| 16 |
import sys |
| 17 |
|
| 18 |
from jenkins_jobs.builder import JenkinsManager |
| 19 |
import jenkins_jobs.cli.subcommand.base as base |
| 20 |
import jenkins_jobs.utils as utils |
| 21 |
|
| 22 |
|
| 23 |
def list_duplicates(seq): |
| 24 |
seen = set() |
| 25 |
return set(x for x in seq if x in seen or seen.add(x)) |
| 26 |
|
| 27 |
|
| 28 |
class ListSubCommand(base.JobsSubCommand): |
| 29 |
def parse_args(self, subparser): |
| 30 |
list = subparser.add_parser("list", help="List jobs") |
| 31 |
|
| 32 |
self.parse_option_recursive_exclude(list) |
| 33 |
|
| 34 |
list.add_argument("names", help="name(s) of job(s)", nargs="*", default=None) |
| 35 |
list.add_argument( |
| 36 |
"-p", "--path", default=None, help="path to YAML file or directory" |
| 37 |
) |
| 38 |
|
| 39 |
def execute(self, options, jjb_config): |
| 40 |
jobs = self.get_jobs(jjb_config, options.path, options.names) |
| 41 |
|
| 42 |
logging.info("Matching jobs: %d", len(jobs)) |
| 43 |
stdout = utils.wrap_stream(sys.stdout) |
| 44 |
|
| 45 |
for job in jobs: |
| 46 |
stdout.write((job + "\n").encode("utf-8")) |
| 47 |
|
| 48 |
def get_jobs(self, jjb_config, path_list, glob_list): |
| 49 |
if path_list: |
| 50 |
roots = self.load_roots(jjb_config, path_list) |
| 51 |
jobs = base.filter_matching(roots.generate_jobs(), glob_list) |
| 52 |
job_names = [j.name for j in jobs] |
| 53 |
else: |
| 54 |
jenkins = JenkinsManager(jjb_config) |
| 55 |
job_names = [ |
| 56 |
j["fullname"] |
| 57 |
for j in jenkins.get_jobs() |
| 58 |
if not glob_list or base.matches(j["fullname"], glob_list) |
| 59 |
] |
| 60 |
|
| 61 |
job_names = sorted(job_names) |
| 62 |
for duplicate in list_duplicates(job_names): |
| 63 |
logging.warning("Found duplicate job name '%s', likely bug.", duplicate) |
| 64 |
|
| 65 |
logging.debug("Builder.get_jobs: returning %r", job_names) |
| 66 |
|
| 67 |
return job_names |