b878f8e4be5a6d77b545bab7f47d598bdcb1c736
[releng/builder.git] / scripts / jjb-init-project.py
1 #!/usr/bin/python
2
3 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
4 ##############################################################################
5 # Copyright (c) 2014, 2015 The Linux Foundation and others.
6 #
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Eclipse Public License v1.0
9 # which accompanies this distribution, and is available at
10 # http://www.eclipse.org/legal/epl-v10.html
11 #
12 # Contributors:
13 #   Thanh Ha (The Linux Foundation) - Initial implementation
14 ##############################################################################
15
16 from collections import OrderedDict
17 import os
18 import re
19
20 import yaml
21
22 import jjblib
23
24
25 args = jjblib.parse_jjb_args()
26
27
28 project = jjblib.Project(args.project)
29 if project.meta_project is not None:
30     project_dir = os.path.join("jjb", project.meta_project, project.project)
31     jenkins_settings = "%s-%s-settings" % (project.meta_project,
32                                            project.project)
33 else:
34     project_dir = os.path.join("jjb", project.project)
35     jenkins_settings = "%s-settings" % project.project
36
37 project_file = os.path.join(project_dir, "%s.yaml" % project)
38 dependent_jobs = ""
39 disabled = "true"   # Always disabled unless project has dependencies
40 email_prefix = "[%s]" % project
41
42
43 if not args.conf:
44     jjblib.create_template_config(project_dir, args)
45     project_conf = os.path.join(project_dir, "%s.cfg" % args.project)
46 else:
47     project_conf = args.conf
48
49 cfg = dict()  # Needed to skip missing project.cfg files
50 if os.path.isfile(project_conf):
51     stream = open(project_conf, "r")
52     cfg = yaml.load(stream)
53
54 ####################
55 # Handle Templates #
56 ####################
57 if cfg.get("JOB_TEMPLATES"):
58     templates = cfg.get("JOB_TEMPLATES")
59 else:
60     templates = ("")
61
62 ##################
63 # Handle Streams #
64 ##################
65 streams = OrderedDict()
66 if cfg.get("STREAMS"):  # this is a list of single-key dicts
67     for stream_dict in cfg.get("STREAMS"):
68         streams.update(stream_dict)
69 else:
70     streams = {"boron": jjblib.STREAM_DEFAULTS["boron"]}
71
72 first_stream = next(iter(streams))  # Keep master branch at top.
73 sonar_branch = streams[first_stream]["branch"]
74 # Create YAML to list branches to create jobs for
75 str_streams = "stream:\n"
76 for stream, options in streams.items():
77     str_streams += ("        - %s:\n"
78                     "            branch: '%s'\n" %
79                     (stream, options["branch"]))
80     str_streams += "            jdk: %s\n" % options["jdks"].split(',')[0].strip()  # noqa
81     str_streams += "            jdks:\n"
82     for jdk in options["jdks"].split(","):
83         str_streams += "                - %s\n" % jdk.strip()
84
85     # Disable autorelease validate job unless project is participating
86     # in autorelease, JJB does not allow flipping a boolean so we have to
87     # flip it here via not operator since the JJB configuration for disabling
88     # a Jenkins Job is "disabled: bool".
89     str_streams += "            disable_autorelease: %s\n" % (not options.get(
90         "autorelease", False))
91
92     # Disable the distribution-check job unless project enables it
93     str_streams += "            disable_distribution_check: %s\n" % (
94         not options.get("distribution-check", True))
95
96 ###############
97 # Handle JDKS #
98 ###############
99 if cfg.get('JDKS'):
100     jdks = cfg.get('JDKS')
101 else:
102     jdks = "openjdk7"
103 use_jdks = ""
104 for jdk in jdks.split(","):
105     use_jdks += "                - %s\n" % jdk
106
107 ##############
108 # Handle POM #
109 ##############
110 if cfg.get('POM'):
111     pom = cfg.get('POM')
112 else:
113     pom = "pom.xml"
114
115 ####################
116 # Handle MVN_GOALS #
117 ####################
118 if cfg.get('MVN_GOALS'):
119     mvn_goals = cfg.get('MVN_GOALS')
120 else:
121     mvn_goals = ("clean install "
122                  "-Dmaven.repo.local=/tmp/r "
123                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
124
125 ###################
126 # Handle MVN_OPTS #
127 ###################
128 if cfg.get('MVN_OPTS'):
129     mvn_opts = cfg.get('MVN_OPTS')
130 else:
131     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
132
133 #######################
134 # Handle DEPENDENCIES #
135 #######################
136 if cfg.get('DEPENDENCIES'):
137     dependencies = cfg.get('DEPENDENCIES')
138     if dependencies.find("odlparent") < 0:  # Add odlparent if not listed
139         dependencies = "odlparent," + dependencies
140     disabled = "false"
141 else:
142     dependencies = None
143     if project.project != "odlparent":  # Odlparent does not depend on itself
144         dependencies = "odlparent"  # All other projects depend on odlparent
145     disabled = "false"
146
147 if dependencies:
148     email_prefix = (email_prefix + " " +
149                 " ".join(['[%s]' % d for d in dependencies.split(",")]))  # noqa
150     dependent_jobs = ",".join(
151         ['%s-merge-{stream}' % d for d in dependencies.split(",")])
152
153 ############################
154 # Handle ARCHIVE_ARTIFACTS #
155 ############################
156
157 archive_artifacts = cfg.get('ARCHIVE_ARTIFACTS', '')
158
159 ##############################
160 # Create configuration start #
161 ##############################
162
163 # Create project directory if it doesn't exist
164 if not os.path.exists(project_dir):
165     os.makedirs(project_dir)
166
167 print("project: %s\n"
168       "streams: %s\n"
169       "goals: %s\n"
170       "options: %s\n"
171       "dependencies: %s\n"
172       "artifacts: %s" %
173       (project,
174        str_streams,
175        mvn_goals,
176        mvn_opts,
177        dependencies,
178        archive_artifacts,))
179
180 # Create initial project YAML file
181 use_templates = templates.split(",")
182 use_templates.insert(0, "project")
183 job_templates_yaml = ""
184 for t in use_templates:
185     if t == "project":  # This is not a job type but is used for templating
186         pass
187     elif t == "sonar":
188         job_templates_yaml = job_templates_yaml + \
189             "        - '%s-%s'\n" % (project, t)
190     else:
191         job_templates_yaml = job_templates_yaml + \
192             "        - '%s-%s-{stream}'\n" % (project, t)
193
194 with open(project_file, "w") as outfile:
195     for t in use_templates:
196         template_file = "jjb-templates/%s.yaml" % t
197         with open(template_file, "r") as infile:
198             for line in infile:
199                 if not re.match("\s*#", line):
200                     line = re.sub("JOB_TEMPLATES", job_templates_yaml, line)
201                     line = re.sub("PROJECT_SHORTNAME", project.project, line)
202                     line = re.sub("PROJECT_PATH", project.path, line)
203                     line = re.sub("JENKINS_SETTINGS", jenkins_settings, line)
204                     line = re.sub("DISABLED", disabled, line)
205                     line = re.sub("STREAMS", str_streams, line)
206                     line = re.sub("POM", pom, line)
207                     line = re.sub("MAVEN_GOALS", mvn_goals, line)
208                     line = re.sub("MAVEN_OPTS", mvn_opts, line)
209                     line = re.sub("DEPENDENCIES", dependent_jobs, line)
210                     line = re.sub("EMAIL_PREFIX", email_prefix, line)
211                     line = re.sub("SONAR_BRANCH", sonar_branch, line)
212                     line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
213                     # The previous command may have created superfluous lines.
214                     # If a line has no non-whitespace, it has to be '\n' only.
215                     line = re.sub(r'^\s+\n', "", line)
216                 outfile.write(line)
217         outfile.write("\n")