Debian Codegraph @

jenkins_jobs/cli/subcommand/get_plugins_info.py

raw
1 #!/usr/bin/env python
2 # Copyright (C) 2017 Thanh Ha
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 logging
17
18 import yaml
19
20 from jenkins_jobs.builder import JenkinsManager
21 import jenkins_jobs.cli.subcommand.base as base
22
23
24 logger = logging.getLogger(__name__)
25
26
27 class GetPluginsInfoSubCommand(base.BaseSubCommand):
28 def parse_args(self, subparser):
29 plugins_info = subparser.add_parser(
30 "get-plugins-info", help="get plugins info yaml by querying Jenkins server."
31 )
32
33 plugins_info.add_argument(
34 "-o",
35 "--output-file",
36 default="plugins_info.yaml",
37 dest="plugins_info_file",
38 help="file to save output to.",
39 )
40
41 def execute(self, options, jjb_config):
42 builder = JenkinsManager(jjb_config)
43 plugin_data = builder.jenkins.get_plugins_info()
44 plugins_info = []
45 for plugin in plugin_data:
46 info = {
47 "longName": str(plugin["longName"]),
48 "shortName": str(plugin["shortName"]),
49 "version": str(plugin["version"]),
50 }
51 plugins_info.append(info)
52
53 if options.plugins_info_file:
54 with open(options.plugins_info_file, "w") as outfile:
55 outfile.write(yaml.dump(plugins_info))
56 logger.info("Generated {} file".format(options.plugins_info_file))
57 else:
58 print(yaml.dump(plugins_info))