Add support for selecting JDKs in verify builds
[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 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 import argparse
17 import os
18 import re
19
20 parser = argparse.ArgumentParser()
21 parser.add_argument("project", help="project")
22 parser.add_argument("-d", "--dependencies",
23                     help=("Project dependencies\n\n"
24                           "A comma-seperated (no spaces) list of projects "
25                           "your project depends on. "
26                           "This is used to create an integration job that "
27                           "will trigger when a dependent project-merge job "
28                           "is built successfully.\n\n"
29                           "Example: aaa,controller,yangtools"))
30 parser.add_argument("-t", "--templates", help="Job templates to use")
31 parser.add_argument("-b", "--branches", help="Git Branches to build")
32 parser.add_argument("-j", "--jdks", help="JDKs to build against (for verify jobs)")  # noqa
33 parser.add_argument("-g", "--mvn-goals", help="Maven Goals")
34 parser.add_argument("-o", "--mvn-opts", help="Maven Options")
35 parser.add_argument("-a", "--archive-artifacts",
36                     help="Comma-seperated list of patterns of artifacts to "
37                          "archive on build completion. "
38                          "See: http://ant.apache.org/manual/Types/fileset.html")  # noqa
39 parser.add_argument("-z", "--no-cfg", action="store_true",
40                     help=("Disable initializing the project.cfg file."))
41 args = parser.parse_args()
42
43 project = args.project
44 project_dir = os.path.join("jjb", project)
45 project_file = os.path.join(project_dir, "%s.yaml" % project)
46 templates = args.templates  # Defaults to all templates
47 branches = args.branches    # Defaults to "master,stable/helium" if not passed
48 jdks = args.jdks            # Defaults to openjdk7
49 mvn_goals = args.mvn_goals  # Defaults to "clean install" if not passsed
50 mvn_opts = args.mvn_opts    # Defaults to blank if not passed
51 dependencies = args.dependencies
52 dependent_jobs = ""
53 disabled = "true"   # Always disabled unless project has dependencies
54 email_prefix = "[%s]" % project
55 archive_artifacts = args.archive_artifacts
56
57 # The below 2 variables are used to determine if we should generate a CFG file
58 # for a project automatically.
59 #
60 # no_cfg - is a commandline parameter that can be used by scripts such as the
61 #          jjb-autoupdate-project script to explicitly disable generating CFG
62 #          files.
63 # make_cfg - is a internal variable used to decide if we should try to
64 #            auto generate the CFG file for a project based on optional
65 #            variables passed by the user on the commandline.
66 no_cfg = args.no_cfg
67 make_cfg = False  # Set to true if we need to generate initial CFG file
68 cfg_string = []
69
70 if not templates:
71     templates = "verify,merge,daily,integration,sonar"
72 else:
73     make_cfg = True
74     cfg_string.append("JOB_TEMPLATES: %s" % templates)
75
76 if not branches:
77     branches = "master,stable/helium"
78     sonar_branch = "master"
79 else:
80     make_cfg = True
81     cfg_string.append("BRANCHES: %s" % branches)
82     # For projects who use a different development branch than master
83     sonar_branch = branches.split(",")[0]
84 # Create YAML to list branches to create jobs for
85 streams = "stream:\n"
86 for branch in branches.split(","):
87     streams = streams + ("        - %s:\n"
88                          "            branch: '%s'\n" %
89                          (branch.replace('/', '-'),
90                           branch))
91
92 if not jdks:
93     jdks = "openjdk7"
94 else:
95     make_cfg = True
96     cfg_string.append("JDKS: %s" % jdks)
97 use_jdks = ""
98 for jdk in jdks.split(","):
99     use_jdks += "                - %s\n" % jdk
100
101 if not mvn_goals:
102     mvn_goals = ("clean install "
103                  "-V "  # Show Maven / Java version before building
104                  "-Dmaven.repo.local=/tmp/r "
105                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
106 else:  # User explicitly set MAVEN_OPTS so create CFG
107     make_cfg = True
108     cfg_string.append("MAVEN_GOALS: %s" % mvn_goals)
109
110 if not mvn_opts:
111     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
112 else:  # User explicitly set MAVEN_OPTS so create CFG
113     make_cfg = True
114     cfg_string.append("MAVEN_OPTS: %s" % mvn_opts)
115
116 if not dependencies:
117     dependencies = "odlparent"  # All projects depend on odlparent
118 if dependencies:
119     if dependencies.find("odlparent") < 0:  # If odlparent is not listed add it
120         dependencies = "odlparent," + dependencies
121     make_cfg = True
122     disabled = "false"
123     email_prefix = (email_prefix + " " +
124                     " ".join(['[%s]' % d for d in dependencies.split(",")]))
125     dependent_jobs = ",".join(
126         ['%s-merge-{stream}' % d for d in dependencies.split(",")])
127     cfg_string.append("DEPENDENCIES: %s" % dependencies)
128
129 if not archive_artifacts:
130     archive_artifacts = ""
131 else:
132     cfg_string.append("ARCHIVE: %s" % archive_artifacts)
133     archive_artifacts = ("- archive-artifacts:\n"
134                          "            artifacts: '%s'" % archive_artifacts)
135
136 ##############################
137 # Create configuration start #
138 ##############################
139
140 # Create project directory if it doesn't exist
141 if not os.path.exists(project_dir):
142     os.makedirs(project_dir)
143
144 print("project: %s\n"
145       "branches: %s\n"
146       "goals: %s\n"
147       "options: %s\n"
148       "dependencies: %s\n"
149       "artifacts: %s" %
150       (project,
151        branches,
152        mvn_goals,
153        mvn_opts,
154        dependencies,
155        archive_artifacts,))
156
157 # Create initial project CFG file
158 if not no_cfg and make_cfg:
159     print("Creating %s.cfg file" % project)
160     cfg_file = os.path.join(project_dir, "%s.cfg" % project)
161     with open(cfg_file, "w") as outstream:
162         cfg = "\n".join(cfg_string)
163         outstream.write(cfg)
164
165 # Create initial project YAML file
166 use_templates = templates.split(",")
167 use_templates.insert(0, "project")
168 job_templates_yaml = ""
169 for t in use_templates:
170     if t == "project":  # This is not a job type but is used for templating
171         pass
172     elif t == "sonar":
173         job_templates_yaml = job_templates_yaml + \
174             "        - '%s-%s'\n" % (project, t)
175     else:
176         job_templates_yaml = job_templates_yaml + \
177             "        - '%s-%s-{stream}'\n" % (project, t)
178
179 with open(project_file, "w") as outfile:
180     for t in use_templates:
181         template_file = "jjb-templates/%s.yaml" % t
182         with open(template_file, "r") as infile:
183             for line in infile:
184                 if not re.match("\s*#", line):
185                     line = re.sub("JOB_TEMPLATES", job_templates_yaml, line)
186                     line = re.sub("PROJECT", project, line)
187                     line = re.sub("DISABLED", disabled, line)
188                     line = re.sub("STREAMS", streams, line)
189                     line = re.sub("JDKS", use_jdks, line)
190                     line = re.sub("MAVEN_GOALS", mvn_goals, line)
191                     line = re.sub("MAVEN_OPTS", mvn_opts, line)
192                     line = re.sub("DEPENDENCIES", dependent_jobs, line)
193                     line = re.sub("EMAIL_PREFIX", email_prefix, line)
194                     line = re.sub("SONAR_BRANCH", sonar_branch, line)
195                     line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
196                 outfile.write(line)
197         outfile.write("\n")