Add netconf user feature test jobs to csit-list
[releng/builder.git] / scripts / jjblib.py
1 import argparse
2 import collections
3 import os
4
5 import yaml
6
7
8 def parse_jjb_args():
9     parser = argparse.ArgumentParser()
10     parser.add_argument("project", help="project")
11     parser.add_argument("-c", "--conf", help="Config file")
12     parser.add_argument("-d", "--dependencies",
13                         help=("Project dependencies\n\n"
14                               "A comma-seperated (no spaces) list of projects "
15                               "your project depends on. "
16                               "This is used to create an integration job that "
17                               "will trigger when a dependent project-merge "
18                               "job is built successfully.\n\n"
19                               "Example: aaa,controller,yangtools"))
20     parser.add_argument("-t", "--templates", help="Job templates to use")
21     parser.add_argument("-s", "--streams",
22                         help="Release streams to fill with default options")
23     parser.add_argument("-p", "--pom", help="Path to pom.xml to use in Maven "
24                                             "build (Default: pom.xml")
25     parser.add_argument("-g", "--mvn-goals", help="Maven Goals")
26     parser.add_argument("-o", "--mvn-opts", help="Maven Options")
27     parser.add_argument("-a", "--archive-artifacts",
28                         help="Comma-seperated list of patterns of artifacts "
29                              "to archive on build completion. "
30                              "See: http://ant.apache.org/manual/Types/fileset.html")  # noqa
31     return parser.parse_args()
32
33
34 STREAM_DEFAULTS = collections.OrderedDict([
35     ("boron", {"branch": "master", "jdks": "openjdk8"}),
36     ("beryllium", {"branch": "stable/beryllium", "jdks": "openjdk8"}),
37     ("stable-lithium", {"branch": "stable/lithium", "jdks": "openjdk7"}),
38     ("stable-helium", {"branch": "stable/helium", "jdks": "openjdk7"}),
39 ])
40
41
42 def create_template_config(project_dir, args):
43     cfg_data = dict()
44
45     if args.templates:
46         cfg_data["JOB_TEMPLATES"] = args.templates
47
48     if args.streams:
49         stream_list = list()
50         for stream in args.streams.split(","):
51             stream_list.append({stream: STREAM_DEFAULTS[stream]})
52         cfg_data["STREAMS"] = stream_list
53
54     if args.pom:
55         cfg_data["POM"] = args.pom
56
57     if args.mvn_goals:
58         cfg_data["MAVEN_GOALS"] = args.mvn_goals
59
60     if args.mvn_opts:
61         cfg_data["MAVEN_OPTS"] = args.mvn_opts
62
63     if args.dependencies:
64         cfg_data["DEPENDENCIES"] = args.dependencies
65
66     if args.archive_artifacts:
67         cfg_data["ARCHIVE"] = args.archive_artifacts
68
69     if cfg_data:
70         # Create project directory if it doesn't exist
71         if not os.path.exists(project_dir):
72             os.makedirs(project_dir)
73
74         print("Creating %s.cfg file" % args.project)
75         cfg_file = os.path.join(project_dir, "%s.cfg" % args.project)
76
77         with open(cfg_file, "w") as outstream:
78             outstream.write(yaml.dump(cfg_data, default_flow_style=False))
79
80
81 class Project:
82     """Represents a Gerrit Project
83
84     Attributes:
85         path(str): The full Gerrit path to a project
86         meta_project(str): The top-level project name in Gerrit
87         project(str): The subproject name or project shortname
88     """
89
90     def __init__(self, project):
91         self.path = project
92         self.meta_project = None
93         self.project = project
94
95         if project.find('/') >= 0:
96             s = project.rsplit('/', 1)
97             self.meta_project = s[0]
98             self.project = s[1]
99
100     def __str__(self):
101         return self.project