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