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