blob: 18608e30ef71c0274ced9a6a60fe1b717b8ee3fa [file] [log] [blame]
Paul Sokolovskyb1d86572015-03-05 22:48:18 +02001#!/usr/bin/python
2
3import os
4import shutil
5import signal
6import string
7import subprocess
8import sys
9from distutils.spawn import find_executable
10
Vishal Bhojafb87362016-08-22 19:31:08 +053011
12def findparentfiles(fname):
13 filelist = []
14 newlist = []
15 args = ['grep', '-rl', '--exclude-dir=.git', fname]
16 proc = subprocess.Popen(args,
17 stdin=subprocess.PIPE,
18 stdout=subprocess.PIPE,
19 universal_newlines=False,
20 preexec_fn=lambda:
21 signal.signal(signal.SIGPIPE, signal.SIG_DFL))
22 data = proc.communicate()[0]
23 if proc.returncode != 0:
24 return filelist
25 for filename in data.splitlines():
26 if filename.endswith('.yaml') and '/' not in filename:
27 filelist.append(filename)
28 else:
29 newlist = findparentfiles(filename)
30 for tempname in newlist:
31 filelist.append(tempname)
32 return filelist
33
34
Fathi Boudra340fc632017-04-06 12:30:46 +030035jjb_cmd = find_executable('jenkins-jobs') or sys.exit('jenkins-jobs is not found.')
Paul Sokolovskyb1d86572015-03-05 22:48:18 +020036
37try:
Fathi Boudra2ad09862015-06-08 13:33:24 +030038 arguments = ['git', 'diff', '--name-only',
39 os.environ.get('GIT_PREVIOUS_COMMIT'),
40 os.environ.get('GIT_COMMIT')]
Paul Sokolovskyb1d86572015-03-05 22:48:18 +020041 proc = subprocess.Popen(arguments,
42 stdin=subprocess.PIPE,
43 stdout=subprocess.PIPE,
44 universal_newlines=False,
45 preexec_fn=lambda:
46 signal.signal(signal.SIGPIPE, signal.SIG_DFL))
47except (OSError, ValueError) as e:
48 raise ValueError("%s" % e)
49
50data = proc.communicate()[0]
51if proc.returncode != 0:
52 raise ValueError("command has failed with code '%s'" % proc.returncode)
53
Vishal Bhojafb87362016-08-22 19:31:08 +053054filelist = []
55files = []
56for filename in data.splitlines():
57 if filename.endswith('.yaml') and '/' not in filename:
58 filelist.append(filename)
59 else:
60 files = findparentfiles(filename)
61 for tempname in files:
62 filelist.append(tempname)
63
64# Remove dplicate entries in the list
65filelist = list(set(filelist))
66
67for conf_filename in filelist:
68 with open(conf_filename) as f:
69 buffer = f.read()
70 template = string.Template(buffer)
Paul Sokolovskyb1d86572015-03-05 22:48:18 +020071 buffer = template.safe_substitute(
Fathi Boudrafdd9f212015-04-18 19:28:55 +030072 AUTH_TOKEN=os.environ.get('AUTH_TOKEN'),
Paul Sokolovskyb1d86572015-03-05 22:48:18 +020073 LT_QCOM_KEY=os.environ.get('LT_QCOM_KEY'),
74 LAVA_USER=os.environ.get('LAVA_USER'),
75 LAVA_TOKEN=os.environ.get('LAVA_TOKEN'))
76 with open('template.yaml', 'w') as f:
77 f.write(buffer)
78 try:
Fathi Boudra374a8c82015-03-06 11:09:47 +020079 arguments = [jjb_cmd, 'update', 'template.yaml']
Paul Sokolovskyb1d86572015-03-05 22:48:18 +020080 # arguments = [jjb_cmd, 'test', conf_filename, '-o', 'out']
81 proc = subprocess.Popen(arguments,
82 stdin=subprocess.PIPE,
83 stdout=subprocess.PIPE,
84 universal_newlines=False,
85 preexec_fn=lambda:
86 signal.signal(signal.SIGPIPE, signal.SIG_DFL))
87 except (OSError, ValueError) as e:
88 raise ValueError("%s" % e)
89
90 data = proc.communicate()[0]
91 if proc.returncode != 0:
92 raise ValueError("command has failed with code '%s'" % proc.returncode)
93
94 os.remove('template.yaml')
95 #shutil.rmtree('out')