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