Fix various small issues left behind branch cut
[releng/builder.git] / scripts / jjb-init-project.py
index 391ba72eba89a148803ef8a21e161ee19336e595..417049d3b4b505ffc2d7fc6dd50549ba2c1ab11b 100644 (file)
@@ -2,7 +2,7 @@
 
 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
 ##############################################################################
-# Copyright (c) 2014 The Linux Foundation and others.
+# Copyright (c) 2014, 2015 The Linux Foundation and others.
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Eclipse Public License v1.0
 #   Thanh Ha (The Linux Foundation) - Initial implementation
 ##############################################################################
 
-import argparse
+from collections import OrderedDict
 import os
 import re
 
-parser = argparse.ArgumentParser()
-parser.add_argument("project", help="project")
-parser.add_argument("-d", "--dependencies",
-                    help=("Project dependencies\n\n"
-                          "A comma-seperated (no spaces) list of projects "
-                          "your project depends on. "
-                          "This is used to create an integration job that "
-                          "will trigger when a dependent project-merge job "
-                          "is built successfully.\n\n"
-                          "Example: aaa,controller,yangtools"))
-parser.add_argument("-t", "--templates", help="Job templates to use")
-parser.add_argument("-b", "--branches", help="Git Branches to build")
-parser.add_argument("-j", "--jdks", help="JDKs to build against (for verify jobs)")  # noqa
-parser.add_argument("-p", "--pom", help="Path to pom.xml to use in Maven build"
-                                        "(Default: pom.xml")
-parser.add_argument("-g", "--mvn-goals", help="Maven Goals")
-parser.add_argument("-o", "--mvn-opts", help="Maven Options")
-parser.add_argument("-a", "--archive-artifacts",
-                    help="Comma-seperated list of patterns of artifacts to "
-                         "archive on build completion. "
-                         "See: http://ant.apache.org/manual/Types/fileset.html")  # noqa
-parser.add_argument("-z", "--no-cfg", action="store_true",
-                    help=("Disable initializing the project.cfg file."))
-args = parser.parse_args()
-
-project = args.project
-project_dir = os.path.join("jjb", project)
+import yaml
+
+import jjblib
+
+
+args = jjblib.parse_jjb_args()
+
+
+project = jjblib.Project(args.project)
+if project.meta_project is not None:
+    project_dir = os.path.join("jjb", project.meta_project, project.project)
+    jenkins_settings = "%s-%s-settings" % (project.meta_project,
+                                           project.project)
+else:
+    project_dir = os.path.join("jjb", project.project)
+    jenkins_settings = "%s-settings" % project.project
+
 project_file = os.path.join(project_dir, "%s.yaml" % project)
-templates = args.templates  # Defaults to all templates
-branches = args.branches    # Defaults to "master,stable/helium" if not passed
-jdks = args.jdks            # Defaults to openjdk7
-pom = args.pom              # Defaults to pom.xml
-mvn_goals = args.mvn_goals  # Defaults to "clean install" if not passsed
-mvn_opts = args.mvn_opts    # Defaults to blank if not passed
-dependencies = args.dependencies
 dependent_jobs = ""
 disabled = "true"   # Always disabled unless project has dependencies
 email_prefix = "[%s]" % project
-archive_artifacts = args.archive_artifacts
 
-# The below 2 variables are used to determine if we should generate a CFG file
-# for a project automatically.
-#
-# no_cfg - is a commandline parameter that can be used by scripts such as the
-#          jjb-autoupdate-project script to explicitly disable generating CFG
-#          files.
-# make_cfg - is a internal variable used to decide if we should try to
-#            auto generate the CFG file for a project based on optional
-#            variables passed by the user on the commandline.
-no_cfg = args.no_cfg
-make_cfg = False  # Set to true if we need to generate initial CFG file
-cfg_string = []
-
-if not templates:
-    templates = "verify,merge,daily,integration,sonar"
+
+if not args.conf:
+    jjblib.create_template_config(project_dir, args)
+    project_conf = os.path.join(project_dir, "%s.cfg" % args.project)
 else:
-    make_cfg = True
-    cfg_string.append("JOB_TEMPLATES: %s" % templates)
+    project_conf = args.conf
+
+cfg = dict()  # Needed to skip missing project.cfg files
+if os.path.isfile(project_conf):
+    stream = open(project_conf, "r")
+    cfg = yaml.load(stream)
+
+####################
+# Handle Templates #
+####################
+if cfg.get("JOB_TEMPLATES"):
+    templates = cfg.get("JOB_TEMPLATES")
+else:
+    templates = "verify,merge,periodic,distribution,integration,sonar"
 templates += ",clm"  # ensure we always create a clm job for all projects
-
-if not branches:
-    branches = "master,stable/helium"
-    sonar_branch = "master"
+templates += ",validate-autorelease"  # Autorelease validate template
+
+##################
+# Handle Streams #
+##################
+streams = OrderedDict()
+if cfg.get("STREAMS"):  # this is a list of single-key dicts
+    for stream_dict in cfg.get("STREAMS"):
+        streams.update(stream_dict)
 else:
-    make_cfg = True
-    cfg_string.append("BRANCHES: %s" % branches)
-    # For projects who use a different development branch than master
-    sonar_branch = branches.split(",")[0]
+    streams = {"boron": jjblib.STREAM_DEFAULTS["boron"]}
+
+first_stream = streams.iterkeys().next()  # Keep master branch at top.
+sonar_branch = streams[first_stream]["branch"]
 # Create YAML to list branches to create jobs for
-streams = "stream:\n"
-for branch in branches.split(","):
-    streams = streams + ("        - %s:\n"
-                         "            branch: '%s'\n" %
-                         (branch.replace('/', '-'),
-                          branch))
-
-if not jdks:
-    jdks = "openjdk7"
+str_streams = "stream:\n"
+for stream, options in streams.items():
+    str_streams += ("        - %s:\n"
+                    "            branch: '%s'\n" %
+                    (stream, options["branch"]))
+    str_streams += "            jdk: %s\n" % options["jdks"].split(',')[0].strip()  # noqa
+    str_streams += "            jdks:\n"
+    for jdk in options["jdks"].split(","):
+        str_streams += "                - %s\n" % jdk.strip()
+
+    # Disable autorelease validate job unless project is participating
+    # in autorelease, JJB does not allow flipping a boolean so we have to
+    # flip it here via not operator since the JJB configuration for disabling
+    # a Jenkins Job is "disabled: bool".
+    str_streams += "            disable_autorelease: %s\n" % (not options.get(
+        "autorelease", False))
+
+###############
+# Handle JDKS #
+###############
+if cfg.get('JDKS'):
+    jdks = cfg.get('JDKS')
 else:
-    make_cfg = True
-    cfg_string.append("JDKS: %s" % jdks)
+    jdks = "openjdk7"
 use_jdks = ""
 for jdk in jdks.split(","):
     use_jdks += "                - %s\n" % jdk
 
-if not pom:
-    pom = "pom.xml"
+##############
+# Handle POM #
+##############
+if cfg.get('POM'):
+    pom = cfg.get('POM')
 else:
-    make_cfg = True
-    cfg_string.append("POM: %s" % pom)
+    pom = "pom.xml"
 
-if not mvn_goals:
+####################
+# Handle MVN_GOALS #
+####################
+if cfg.get('MVN_GOALS'):
+    mvn_goals = cfg.get('MVN_GOALS')
+else:
     mvn_goals = ("clean install "
                  "-V "  # Show Maven / Java version before building
                  "-Dmaven.repo.local=/tmp/r "
                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
-else:  # User explicitly set MAVEN_OPTS so create CFG
-    make_cfg = True
-    cfg_string.append("MAVEN_GOALS: %s" % mvn_goals)
 
-if not mvn_opts:
+###################
+# Handle MVN_OPTS #
+###################
+if cfg.get('MVN_OPTS'):
+    mvn_opts = cfg.get('MVN_OPTS')
+else:
     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
-else:  # User explicitly set MAVEN_OPTS so create CFG
-    make_cfg = True
-    cfg_string.append("MAVEN_OPTS: %s" % mvn_opts)
 
-if not dependencies:
-    dependencies = "odlparent"  # All projects depend on odlparent
-if dependencies:
-    if dependencies.find("odlparent") < 0:  # If odlparent is not listed add it
+#######################
+# Handle DEPENDENCIES #
+#######################
+if cfg.get('DEPENDENCIES'):
+    dependencies = cfg.get('DEPENDENCIES')
+    if dependencies.find("odlparent") < 0:  # Add odlparent if not listed
         dependencies = "odlparent," + dependencies
-    make_cfg = True
     disabled = "false"
+else:
+    dependencies = None
+    if project.project != "odlparent":  # Odlparent does not depend on itself
+        dependencies = "odlparent"  # All other projects depend on odlparent
+    disabled = "false"
+
+if dependencies:
     email_prefix = (email_prefix + " " +
-                    " ".join(['[%s]' % d for d in dependencies.split(",")]))
+                " ".join(['[%s]' % d for d in dependencies.split(",")]))  # noqa
     dependent_jobs = ",".join(
         ['%s-merge-{stream}' % d for d in dependencies.split(",")])
-    cfg_string.append("DEPENDENCIES: %s" % dependencies)
 
-if not archive_artifacts:
-    archive_artifacts = ""
-else:
-    cfg_string.append("ARCHIVE: %s" % archive_artifacts)
-    archive_artifacts = ("- archive-artifacts:\n"
-                         "            artifacts: '%s'" % archive_artifacts)
+############################
+# Handle ARCHIVE_ARTIFACTS #
+############################
+
+always_archive = "**/target/surefire-reports/*-output.txt"
+
+archive_artifacts = cfg.get('ARCHIVE_ARTIFACTS', '')
+archive_artifacts = ("- archive-artifacts:\n"
+                     "            artifacts: '%s, %s'" %
+                     (always_archive, archive_artifacts))
 
 ##############################
 # Create configuration start #
@@ -152,26 +169,18 @@ if not os.path.exists(project_dir):
     os.makedirs(project_dir)
 
 print("project: %s\n"
-      "branches: %s\n"
+      "streams: %s\n"
       "goals: %s\n"
       "options: %s\n"
       "dependencies: %s\n"
       "artifacts: %s" %
       (project,
-       branches,
+       str_streams,
        mvn_goals,
        mvn_opts,
        dependencies,
        archive_artifacts,))
 
-# Create initial project CFG file
-if not no_cfg and make_cfg:
-    print("Creating %s.cfg file" % project)
-    cfg_file = os.path.join(project_dir, "%s.cfg" % project)
-    with open(cfg_file, "w") as outstream:
-        cfg = "\n".join(cfg_string)
-        outstream.write(cfg)
-
 # Create initial project YAML file
 use_templates = templates.split(",")
 use_templates.insert(0, "project")
@@ -179,7 +188,7 @@ job_templates_yaml = ""
 for t in use_templates:
     if t == "project":  # This is not a job type but is used for templating
         pass
-    elif t == "sonar" or t == "clm":
+    elif t == "sonar":
         job_templates_yaml = job_templates_yaml + \
             "        - '%s-%s'\n" % (project, t)
     else:
@@ -193,10 +202,11 @@ with open(project_file, "w") as outfile:
             for line in infile:
                 if not re.match("\s*#", line):
                     line = re.sub("JOB_TEMPLATES", job_templates_yaml, line)
-                    line = re.sub("PROJECT", project, line)
+                    line = re.sub("PROJECT_SHORTNAME", project.project, line)
+                    line = re.sub("PROJECT_PATH", project.path, line)
+                    line = re.sub("JENKINS_SETTINGS", jenkins_settings, line)
                     line = re.sub("DISABLED", disabled, line)
-                    line = re.sub("STREAMS", streams, line)
-                    line = re.sub("JDKS", use_jdks, line)
+                    line = re.sub("STREAMS", str_streams, line)
                     line = re.sub("POM", pom, line)
                     line = re.sub("MAVEN_GOALS", mvn_goals, line)
                     line = re.sub("MAVEN_OPTS", mvn_opts, line)
@@ -204,5 +214,8 @@ with open(project_file, "w") as outfile:
                     line = re.sub("EMAIL_PREFIX", email_prefix, line)
                     line = re.sub("SONAR_BRANCH", sonar_branch, line)
                     line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
+                    # The previous command may have created superfluous lines.
+                    # If a line has no non-whitespace, it has to be '\n' only.
+                    line = re.sub(r'^\s+\n', "", line)
                 outfile.write(line)
         outfile.write("\n")