Auto generate CFG file if any parameters are passed
[releng/builder.git] / scripts / jjb-autoupdate-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 os
17
18 import yaml
19
20
21 def get_autoupdate_projects(jjb_dir, projects):
22     """Get list of projects that should be auto-updated."""
23     project_list = []
24     for project in projects:
25         template = os.path.join(jjb_dir, project, "%s.yaml" % project)
26         if os.path.isfile(template):
27             with open(template, 'r') as f:
28                 first_line = f.readline()
29             if first_line.startswith("# REMOVE THIS LINE IF"):
30                 project_list.append(project)
31
32     return project_list
33
34
35 def update_templates(projects):
36     for project in projects:
37
38         # If project has customized variables
39         cfg_file = "jjb/%s/%s.cfg" % (project, project)
40         parameters = ["python scripts/jjb-init-project.py"]
41         parameters.append("-z")  # Disable CFG auto-generation
42         if os.path.isfile(cfg_file):
43             stream = open(cfg_file, "r")
44             cfg = yaml.load(stream)
45             for k, v in cfg.items():
46                 if k == "MVN_GOALS" and v is not None:
47                     parameters.append("-g '%s'" % v)
48                 elif k == "MVN_OPTS" and v is not None:
49                     parameters.append("-o '%s'" % v)
50                 elif k == "DEPENDENCIES" and v is not None:
51                     parameters.append("-d '%s'" % v)
52
53             parameters.append(project)
54             cmd = " ".join(parameters)
55             os.system(cmd)
56
57         else:
58             os.system("python scripts/jjb-init-project.py -z %s" % project)
59
60 ##############
61 # Code Start #
62 ##############
63
64 jjb_dir = "jjb"
65 all_projects = [d for d in os.listdir(jjb_dir)
66                 if os.path.isdir(os.path.join(jjb_dir, d))]
67 auto_update_projects = get_autoupdate_projects(jjb_dir, all_projects)
68 update_templates(auto_update_projects)