Merge "Only create master jobs for reservation"
[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("-b", "--branches", help="Git Branches to build")
31 parser.add_argument("-g", "--mvn-goals", help="Maven Goals")
32 parser.add_argument("-o", "--mvn-opts", help="Maven Options")
33 parser.add_argument("-a", "--archive-artifacts",
34                     help="Comma-seperated list of patterns of artifacts to "
35                          "archive on build completion. "
36                          "See: http://ant.apache.org/manual/Types/fileset.html")  # noqa
37 parser.add_argument("-z", "--no-cfg", action="store_true",
38                     help=("Disable initializing the project.cfg file."))
39 args = parser.parse_args()
40
41 project = args.project
42 project_dir = os.path.join("jjb", project)
43 project_file = os.path.join(project_dir, "%s.yaml" % project)
44 branches = args.branches    # Defaults to "master,stable/helium" if not passed
45 mvn_goals = args.mvn_goals  # Defaults to "clean install" if not passsed
46 mvn_opts = args.mvn_opts    # Defaults to blank if not passed
47 dependencies = args.dependencies
48 dependent_jobs = ""
49 disabled = "true"   # Always disabled unless project has dependencies
50 email_prefix = "[%s]" % project
51 archive_artifacts = args.archive_artifacts
52
53 template_file = os.path.join("jjb", "job.yaml.template")
54
55 # The below 2 variables are used to determine if we should generate a CFG file
56 # for a project automatically.
57 #
58 # no_cfg - is a commandline parameter that can be used by scripts such as the
59 #          jjb-autoupdate-project script to explicitly disable generating CFG
60 #          files.
61 # make_cfg - is a internal variable used to decide if we should try to
62 #            auto generate the CFG file for a project based on optional
63 #            variables passed by the user on the commandline.
64 no_cfg = args.no_cfg
65 make_cfg = False  # Set to true if we need to generate initial CFG file
66 cfg_string = []
67
68 if not branches:
69     branches = "master,stable/helium"
70     sonar_branch = "master"
71 else:
72     make_cfg = True
73     cfg_string.append("BRANCHES: %s" % branches)
74     # For projects who use a different development branch than master
75     sonar_branch = branches.split(",")[0]
76 # Create YAML to list branches to create jobs for
77 streams = "stream:\n"
78 for branch in branches.split(","):
79     streams = streams + ("        - %s:\n"
80                          "            branch: '%s'\n" %
81                          (branch.replace('/', '-'),
82                           branch))
83
84 if not mvn_goals:
85     mvn_goals = ("clean install "
86                  "-V "  # Show Maven / Java version before building
87                  "-Dmaven.repo.local=/tmp/r "
88                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
89 else:  # User explicitly set MAVEN_OPTS so create CFG
90     make_cfg = True
91     cfg_string.append("MAVEN_GOALS: %s" % mvn_goals)
92
93 if not mvn_opts:
94     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
95 else:  # User explicitly set MAVEN_OPTS so create CFG
96     make_cfg = True
97     cfg_string.append("MAVEN_OPTS: %s" % mvn_opts)
98
99 if not dependencies:
100     dependencies = "odlparent"  # All projects depend on odlparent
101 if dependencies:
102     if dependencies.find("odlparent") < 0:  # If odlparent is not listed add it
103         dependencies = "odlparent," + dependencies
104     make_cfg = True
105     disabled = "false"
106     email_prefix = (email_prefix + " " +
107                     " ".join(['[%s]' % d for d in dependencies.split(",")]))
108     dependent_jobs = ",".join(
109         ['%s-merge-{stream}' % d for d in dependencies.split(",")])
110     cfg_string.append("DEPENDENCIES: %s" % dependencies)
111
112 if not archive_artifacts:
113     archive_artifacts = ""
114 else:
115     cfg_string.append("ARCHIVE: %s" % archive_artifacts)
116     archive_artifacts = ("- archive-artifacts:\n"
117                          "            artifacts: '%s'" % archive_artifacts)
118
119 ##############################
120 # Create configuration start #
121 ##############################
122
123 # Create project directory if it doesn't exist
124 if not os.path.exists(project_dir):
125     os.makedirs(project_dir)
126
127 print("project: %s\n"
128       "branches: %s\n"
129       "goals: %s\n"
130       "options: %s\n"
131       "dependencies: %s\n"
132       "artifacts: %s" %
133       (project,
134        branches,
135        mvn_goals,
136        mvn_opts,
137        dependencies,
138        archive_artifacts,))
139
140 # Create initial project CFG file
141 if not no_cfg and make_cfg:
142     print("Creating %s.cfg file" % project)
143     cfg_file = os.path.join(project_dir, "%s.cfg" % project)
144     with open(cfg_file, "w") as outstream:
145         cfg = "\n".join(cfg_string)
146         outstream.write(cfg)
147
148 # Create initial project YAML file
149 with open(template_file, "r") as infile:
150     with open(project_file, "w") as outfile:
151         for line in infile:
152             if not re.match("\s*#", line):
153                 line = re.sub("PROJECT", project, line)
154                 line = re.sub("DISABLED", disabled, line)
155                 line = re.sub("STREAMS", streams, line)
156                 line = re.sub("MAVEN_GOALS", mvn_goals, line)
157                 line = re.sub("MAVEN_OPTS", mvn_opts, line)
158                 line = re.sub("DEPENDENCIES", dependent_jobs, line)
159                 line = re.sub("EMAIL_PREFIX", email_prefix, line)
160                 line = re.sub("SONAR_BRANCH", sonar_branch, line)
161                 line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
162             outfile.write(line)