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