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