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