Debian Codegraph @

jenkins_jobs/cli/parser.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 argparse
17 import os
18
19 import jenkins_jobs.version
20
21 from stevedore import extension
22
23
24 def __version__():
25 return (
26 "Jenkins Job Builder version: %s"
27 % jenkins_jobs.version.version_info.version_string()
28 )
29
30
31 def create_parser():
32 """Create an ArgumentParser object usable by JenkinsJobs."""
33 parser = argparse.ArgumentParser()
34 parser.add_argument(
35 "--conf",
36 dest="conf",
37 default=os.environ.get("JJB_CONF", None),
38 help="configuration file [JJB_CONF]",
39 )
40 parser.add_argument(
41 "-l",
42 "--log_level",
43 dest="log_level",
44 default=os.environ.get("JJB_LOG_LEVEL", "info"),
45 help="log level (default: %(default)s) [JJB_LOG_LEVEL]",
46 )
47 parser.add_argument(
48 "--ignore-cache",
49 action="store_true",
50 dest="ignore_cache",
51 default=None,
52 help="ignore the cache and update the jobs anyhow (that will "
53 "only flush the specified jobs cache)",
54 )
55 parser.add_argument(
56 "--flush-cache",
57 action="store_true",
58 dest="flush_cache",
59 default=None,
60 help="flush all the cache entries before updating",
61 )
62 parser.add_argument(
63 "--version",
64 dest="version",
65 action="version",
66 version=__version__(),
67 help="show version",
68 )
69 parser.add_argument(
70 "--allow-empty-variables",
71 action="store_true",
72 dest="allow_empty_variables",
73 default=None,
74 help="Don't fail if any of the variables inside any string are "
75 "not defined, replace with empty string instead.",
76 )
77 parser.add_argument(
78 "--server",
79 "-s",
80 dest="section",
81 default=os.environ.get("JJB_SECTION", "jenkins"),
82 help="The Jenkins server ini section to use. Defaults to 'jenkins' "
83 "[JJB_SECTION]",
84 )
85 parser.add_argument(
86 "--user",
87 "-u",
88 default=os.environ.get("JJB_USER", None),
89 help="The Jenkins user to use for authentication. This overrides "
90 "the user specified in the configuration file. [JJB_USER]",
91 )
92 parser.add_argument(
93 "--password",
94 "-p",
95 default=os.environ.get("JJB_PASSWORD", None),
96 help="Password or API token to use for authenticating towards Jenkins."
97 " This overrides the password specified in the configuration file."
98 " [JJB_PASSWORD]",
99 )
100
101 subparser = parser.add_subparsers(
102 dest="command", help="update, test, list or delete job"
103 )
104
105 extension_manager = extension.ExtensionManager(
106 namespace="jjb.cli.subcommands", invoke_on_load=True
107 )
108
109 def parse_subcommand_args(ext, subparser):
110 ext.obj.parse_args(subparser)
111
112 extension_manager.map(parse_subcommand_args, subparser)
113
114 return parser