51701c90faacd87106c0c81fa718f02395e31bc5
[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         if os.path.isfile(cfg_file):
42             stream = open(cfg_file, "r")
43             cfg = yaml.load(stream)
44             for k, v in cfg.items():
45                 if k == "MVN_GOALS" and v is not None:
46                     parameters.append("-g '%s'" % v)
47                 elif k == "MVN_OPTS" and v is not None:
48                     parameters.append("-o '%s'" % v)
49
50             parameters.append(project)
51             cmd = " ".join(parameters)
52             os.system(cmd)
53
54         else:
55             os.system("python scripts/jjb-init-project.py %s" % project)
56
57 ##############
58 # Code Start #
59 ##############
60
61 jjb_dir = "jjb"
62 all_projects = [d for d in os.listdir(jjb_dir)
63                 if os.path.isdir(os.path.join(jjb_dir, d))]
64 auto_update_projects = get_autoupdate_projects(jjb_dir, all_projects)
65 update_templates(auto_update_projects)