Merge "Add provide-maven-settings prebuilder to all templates"
[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 = "verify,merge,daily,distribution,integration,sonar"
61 templates += ",clm"  # ensure we always create a clm job for all projects
62
63 ##################
64 # Handle Streams #
65 ##################
66 streams = OrderedDict()
67 if cfg.get("STREAMS"):  # this is a list of single-key dicts
68     for stream_dict in cfg.get("STREAMS"):
69         streams.update(stream_dict)
70 else:
71     streams = {"beryllium": jjblib.STREAM_DEFAULTS["beryllium"]}
72
73 first_stream = streams.iterkeys().next()  # Keep master branch at top.
74 sonar_branch = streams[first_stream]["branch"]
75 # Create YAML to list branches to create jobs for
76 str_streams = "stream:\n"
77 for stream, options in streams.items():
78     str_streams += ("        - %s:\n"
79                     "            branch: '%s'\n" %
80                     (stream, options["branch"]))
81     str_streams += "            jdk: %s\n" % options["jdks"].split(',')[0].strip()  # noqa
82     str_streams += "            jdks:\n"
83     for jdk in options["jdks"].split(","):
84         str_streams += "                - %s\n" % jdk.strip()
85
86 ###############
87 # Handle JDKS #
88 ###############
89 if cfg.get('JDKS'):
90     jdks = cfg.get('JDKS')
91 else:
92     jdks = "openjdk7"
93 use_jdks = ""
94 for jdk in jdks.split(","):
95     use_jdks += "                - %s\n" % jdk
96
97 ##############
98 # Handle POM #
99 ##############
100 if cfg.get('POM'):
101     pom = cfg.get('POM')
102 else:
103     pom = "pom.xml"
104
105 ####################
106 # Handle MVN_GOALS #
107 ####################
108 if cfg.get('MVN_GOALS'):
109     mvn_goals = cfg.get('MVN_GOALS')
110 else:
111     mvn_goals = ("clean install "
112                  "-V "  # Show Maven / Java version before building
113                  "-Dmaven.repo.local=/tmp/r "
114                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
115
116 ###################
117 # Handle MVN_OPTS #
118 ###################
119 if cfg.get('MVN_OPTS'):
120     mvn_opts = cfg.get('MVN_OPTS')
121 else:
122     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
123
124 #######################
125 # Handle DEPENDENCIES #
126 #######################
127 if cfg.get('DEPENDENCIES'):
128     dependencies = cfg.get('DEPENDENCIES')
129     if dependencies.find("odlparent") < 0:  # Add odlparent if not listed
130         dependencies = "odlparent," + dependencies
131     disabled = "false"
132 else:
133     dependencies = "odlparent"  # All projects depend on odlparent
134     disabled = "false"
135
136 email_prefix = (email_prefix + " " +
137                 " ".join(['[%s]' % d for d in dependencies.split(",")]))  # noqa
138 dependent_jobs = ",".join(
139     ['%s-merge-{stream}' % d for d in dependencies.split(",")])
140
141 ############################
142 # Handle ARCHIVE_ARTIFACTS #
143 ############################
144 if cfg.get('ARCHIVE_ARTIFACTS'):
145     archive_artifacts = cfg.get('ARCHIVE_ARTIFACTS')
146     archive_artifacts = ("- archive-artifacts:\n"
147                          "            artifacts: '%s'" % archive_artifacts)
148 else:
149     archive_artifacts = ""
150
151
152 ##############################
153 # Create configuration start #
154 ##############################
155
156 # Create project directory if it doesn't exist
157 if not os.path.exists(project_dir):
158     os.makedirs(project_dir)
159
160 print("project: %s\n"
161       "streams: %s\n"
162       "goals: %s\n"
163       "options: %s\n"
164       "dependencies: %s\n"
165       "artifacts: %s" %
166       (project,
167        str_streams,
168        mvn_goals,
169        mvn_opts,
170        dependencies,
171        archive_artifacts,))
172
173 # Create initial project YAML file
174 use_templates = templates.split(",")
175 use_templates.insert(0, "project")
176 job_templates_yaml = ""
177 for t in use_templates:
178     if t == "project":  # This is not a job type but is used for templating
179         pass
180     elif t == "sonar":
181         job_templates_yaml = job_templates_yaml + \
182             "        - '%s-%s'\n" % (project, t)
183     else:
184         job_templates_yaml = job_templates_yaml + \
185             "        - '%s-%s-{stream}'\n" % (project, t)
186
187 with open(project_file, "w") as outfile:
188     for t in use_templates:
189         template_file = "jjb-templates/%s.yaml" % t
190         with open(template_file, "r") as infile:
191             for line in infile:
192                 if not re.match("\s*#", line):
193                     line = re.sub("JOB_TEMPLATES", job_templates_yaml, line)
194                     line = re.sub("PROJECT_SHORTNAME", project.project, line)
195                     line = re.sub("PROJECT_PATH", project.path, line)
196                     line = re.sub("JENKINS_SETTINGS", jenkins_settings, line)
197                     line = re.sub("DISABLED", disabled, line)
198                     line = re.sub("STREAMS", str_streams, line)
199                     line = re.sub("POM", pom, line)
200                     line = re.sub("MAVEN_GOALS", mvn_goals, line)
201                     line = re.sub("MAVEN_OPTS", mvn_opts, line)
202                     line = re.sub("DEPENDENCIES", dependent_jobs, line)
203                     line = re.sub("EMAIL_PREFIX", email_prefix, line)
204                     line = re.sub("SONAR_BRANCH", sonar_branch, line)
205                     line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
206                     # The previous command may have created superfluous lines.
207                     # If a line has no non-whitespace, it has to be '\n' only.
208                     line = re.sub(r'^\s+\n', "", line)
209                 outfile.write(line)
210         outfile.write("\n")