Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import shutil |
| 5 | import signal |
| 6 | import string |
| 7 | import subprocess |
| 8 | import sys |
| 9 | from distutils.spawn import find_executable |
| 10 | |
Vishal Bhoj | afb8736 | 2016-08-22 19:31:08 +0530 | [diff] [blame] | 11 | |
| 12 | def 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 Boudra | 340fc63 | 2017-04-06 12:30:46 +0300 | [diff] [blame^] | 35 | jjb_cmd = find_executable('jenkins-jobs') or sys.exit('jenkins-jobs is not found.') |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 36 | |
| 37 | try: |
Fathi Boudra | 2ad0986 | 2015-06-08 13:33:24 +0300 | [diff] [blame] | 38 | arguments = ['git', 'diff', '--name-only', |
| 39 | os.environ.get('GIT_PREVIOUS_COMMIT'), |
| 40 | os.environ.get('GIT_COMMIT')] |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 41 | 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)) |
| 47 | except (OSError, ValueError) as e: |
| 48 | raise ValueError("%s" % e) |
| 49 | |
| 50 | data = proc.communicate()[0] |
| 51 | if proc.returncode != 0: |
| 52 | raise ValueError("command has failed with code '%s'" % proc.returncode) |
| 53 | |
Vishal Bhoj | afb8736 | 2016-08-22 19:31:08 +0530 | [diff] [blame] | 54 | filelist = [] |
| 55 | files = [] |
| 56 | for 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 |
| 65 | filelist = list(set(filelist)) |
| 66 | |
| 67 | for conf_filename in filelist: |
| 68 | with open(conf_filename) as f: |
| 69 | buffer = f.read() |
| 70 | template = string.Template(buffer) |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 71 | buffer = template.safe_substitute( |
Fathi Boudra | fdd9f21 | 2015-04-18 19:28:55 +0300 | [diff] [blame] | 72 | AUTH_TOKEN=os.environ.get('AUTH_TOKEN'), |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 73 | 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 Boudra | 374a8c8 | 2015-03-06 11:09:47 +0200 | [diff] [blame] | 79 | arguments = [jjb_cmd, 'update', 'template.yaml'] |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 80 | # 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') |