Debian Codegraph @

jenkins_jobs/cli/subcommand/base.py

raw
1 #!/usr/bin/env python
2 # Copyright (C) 2015 Wayne Warren
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
16 import abc
17 import fnmatch
18 import logging
19 import time
20
21 from jenkins_jobs.builder import JenkinsManager
22 from jenkins_jobs.registry import ModuleRegistry
23 from jenkins_jobs.roots import Roots
24 from jenkins_jobs.xml_config import XmlJobGenerator
25 from jenkins_jobs.xml_config import XmlViewGenerator
26 from jenkins_jobs.loader import load_files
27
28
29 logger = logging.getLogger(__name__)
30
31
32 def matches(name, glob_list):
33 """
34 Checks if the given string, ``name``, matches any of the glob patterns in
35 the iterable, ``glob_list``
36
37 :arg str name: String (job or view name) to test if it matches a pattern
38 :arg iterable glob_list: glob patterns to match (list, tuple, set, etc.)
39 """
40 return any(fnmatch.fnmatch(name, glob) for glob in glob_list)
41
42
43 def filter_matching(item_list, glob_list):
44 if not glob_list:
45 return item_list
46 return [item for item in item_list if matches(item.name, glob_list)]
47
48
49 class BaseSubCommand(metaclass=abc.ABCMeta):
50 """Base class for Jenkins Job Builder subcommands, intended to allow
51 subcommands to be loaded as stevedore extensions by third party users.
52 """
53
54 def __init__(self):
55 pass
56
57 @abc.abstractmethod
58 def parse_args(self, subparsers, recursive_parser):
59 """Define subcommand arguments.
60
61 :param subparsers
62 A sub parser object. Implementations of this method should
63 create a new subcommand parser by calling
64 parser = subparsers.add_parser('command-name', ...)
65 This will return a new ArgumentParser object; all other arguments to
66 this method will be passed to the argparse.ArgumentParser constructor
67 for the returned object.
68 """
69
70 @abc.abstractmethod
71 def execute(self, config):
72 """Execute subcommand behavior.
73
74 :param config
75 JJBConfig object containing final configuration from config files,
76 command line arguments, and environment variables.
77 """
78
79 @staticmethod
80 def parse_option_recursive_exclude(parser):
81 """Add '--recursive' and '--exclude' arguments to given parser."""
82 parser.add_argument(
83 "-r",
84 "--recursive",
85 action="store_true",
86 dest="recursive",
87 default=False,
88 help="look for yaml files recursively",
89 )
90
91 parser.add_argument(
92 "-x",
93 "--exclude",
94 dest="exclude",
95 action="append",
96 default=[],
97 help="paths to exclude when using recursive search, "
98 "uses standard globbing.",
99 )
100
101
102 class JobsSubCommand(BaseSubCommand):
103 """Base class for Jenkins Job Builder subcommands which generates jobs."""
104
105 def load_roots(self, jjb_config, path_list):
106 roots = Roots(jjb_config)
107 load_files(jjb_config, roots, path_list)
108 return roots
109
110 def make_jobs_and_views_xml(self, jjb_config, path_list, glob_list):
111 logger.info("Updating jobs in {0} ({1})".format(path_list, glob_list))
112 orig = time.time()
113
114 roots = self.load_roots(jjb_config, path_list)
115
116 builder = JenkinsManager(jjb_config)
117
118 registry = ModuleRegistry(jjb_config, builder.plugins_list)
119 registry.set_macros(roots.macros)
120
121 jobs = filter_matching(roots.generate_jobs(), glob_list)
122 views = filter_matching(roots.generate_views(), glob_list)
123
124 registry.amend_job_dicts(jobs)
125
126 xml_job_generator = XmlJobGenerator(registry)
127 xml_view_generator = XmlViewGenerator(registry)
128
129 xml_jobs = xml_job_generator.generateXML(jobs)
130 xml_views = xml_view_generator.generateXML(views)
131
132 step = time.time()
133 logging.debug("%d XML files generated in %ss", len(jobs), str(step - orig))
134
135 return builder, xml_jobs, xml_views