#!/usr/bin/env python import sys, json, yaml import os import subprocess from os.path import expanduser from util_functions import util_functions # doyDistributeGet arguments: # 3. os of slave # 4. platform of slave # 5. configuration # 6. ctparams # 7. build system # 8. config section name # 9. tconf file def main(argv): print ('{}:{}:{}:{}:{}:{}:{}'.format(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5],sys.argv[6],sys.argv[7])) HOST = sys.argv[1] DIRN = sys.argv[2] slaveos = sys.argv[3] slaveplatform = sys.argv[4] buildconfig = sys.argv[5] configname = sys.argv[6] configfile = sys.argv[7] buildsys = 'build' script_path, script_name = os.path.split(sys.argv[0]) userhome = expanduser("~") script_res = {configname+'-'+script_name: 'SUCCESS'} print('currdir='+os.getcwd()) scriptdir=os.getcwd() #### jsparams_file = '../bb_params.json' if scriptdir.split(os.sep)[-1] == buildsys: jsparams_file = 'bb_params.json' with open(jsparams_file, 'r') as json_bbparams_file: json_bbparams_data = json.load(json_bbparams_file) distributeStatus = 1 patchfile = '' if not os.path.exists(configfile): distributeStatus = 1 script_res = {configname+'-'+script_name: 'WARNINGS'} json_bbparams_data.append(script_res) with open(jsparams_file, 'w') as json_bbparams_file: json.dump(json_bbparams_data, json_bbparams_file) raise Exception('Build System Inactive: ', distributeStatus) else: with open(configfile, 'r') as yaml_file: yaml_data = yaml.safe_load(yaml_file) #print(yaml_data) currdir=os.getcwd() #### for cfgparam in yaml_data['configfile'][buildconfig]: if 'bbparams' in cfgparam: for cfgs in yaml_data['configfile'][buildconfig]['bbparams']: if 'distribute' in cfgs: for sdistribute in yaml_data['configfile'][buildconfig]['bbparams']['distribute']: if 'status' == sdistribute.lower(): if yaml_data['configfile'][buildconfig]['bbparams']['distribute'][sdistribute]: distributeStatus = 0 else: distributeStatus = 1 script_res = {configname+'-'+script_name: 'WARNINGS'} json_bbparams_data.append(script_res) with open(jsparams_file, 'w') as json_bbparams_file: json.dump(json_bbparams_data, json_bbparams_file) raise Exception('Release Inactive: ', distributeStatus) if 'srcpatch' == sdistribute.lower(): patchfile = yaml_data['configfile'][buildconfig]['bbparams']['distribute']['srcpatch'] if patchfile: currdir=os.getcwd() script_res.update({'patchfile':patchfile}) install_folder = 'hdfsrc' # pset = patch.fromfile(patchfile) # pset.apply(True, root=install_folder) if 'bbparams' != configname.lower(): try: filename = '{}'.format(patchfile) print ('Getting {}'.format(filename)) sftpclient = util_functions.create_sftp_client(HOST, 22, 'hdftest', None, os.path.expanduser(os.path.join("~", ".ssh", "bitbidkey")), 'RSA', True) if sftpclient is not None: sftpclient.chdir('{}{}'.format(DIRN, configname)) sftpclient.get(filename, filename) print ('File downloaded as: {}'.format(patchfile)) sftpclient.close() except Exception as e: thereturncode = 255 print ('ERROR: cannot read file {}'.format(filename)) if sftpclient is not None: sftpclient.close() script_res = {script_name: 'FAILURE'} json_bbparams_data.append(script_res) with open(jsparams_file, 'w') as json_bbparams_file: json.dump(json_bbparams_data, json_bbparams_file) raise Exception('patch file failure: %s' % e, -1) # if os.path.isdir(install_folder): try: output = '' print ('command=git apply --patch:') test = subprocess.Popen(['git', 'apply', '--directory={}'.format(install_folder), '--whitespace=fix', patchfile], shell=False, stderr=subprocess.PIPE) (textout, texterr) = test.communicate() if textout is not None: output = '{}{}\n'.format(output, textout) if texterr is not None: output = '{} \n{}'.format(output, texterr) print ('---- git apply is {} with error: {}'.format(output)) thereturncode = test.returncode except Exception as e: thereturncode = 255 print('Patch file error {} - {}'.format(thereturncode, e)) script_res = {script_name: 'FAILURE'} os.chdir(currdir) os.chdir(currdir) #### json_bbparams_data.append(script_res) with open(jsparams_file, 'w') as json_bbparams_file: json.dump(json_bbparams_data, json_bbparams_file) return distributeStatus if __name__ == '__main__': main(sys.argv)