#!/usr/bin/env python3 import sys import os import subprocess import socket, stat import paramiko from os.path import expanduser from . import util_functions def install_binary(json_bbparams_data, buildsys, installos, installfolder, installform): script_tc_res = {} thereturncode = 0 installname = '' output = '' dir_list = util_functions.get_filepaths(installfolder) if installos.lower() in util_functions.WinPlatforms: for f in dir_list: #print ('f = {}'.format(f)) if f.endswith(".msi"): installname = f try: textout = '' texterr = '' if 'install' == installform.lower(): print ('installing {}'.format(installname)) test = subprocess.Popen(['msiexec.exe', '/c', '/package', installname, '/quiet'], shell=True, universal_newlines=True) (textout, texterr) = test.communicate() if textout is not None: output = '{}{}\n'.format(output, textout) if texterr is not None: output = '{} \n{}'.format(output, texterr) else: print ('uninstalling {}'.format(installname)) test = subprocess.Popen(['msiexec.exe', '/c', '/uninstall', installname, '/quiet'], shell=True, universal_newlines=True) (textout, texterr) = test.communicate() if textout is not None: output = '{}{}\n'.format(output, textout) if texterr is not None: output = '{}\n{}'.format(output, texterr) except Exception as e: script_tc_res.update({'installname':'msiexec.exe failed on {}'.format(installname)}) json_bbparams_data.append(script_tc_res) print('msiexec.exe failed on {}'.format(installname)) print('Popen Exception: {}'.format(e)) print ('Windows File {} : {}'.format(installname,output)) if len(installname) == 0: thereturncode = 255 script_tc_res.update({'installname':'cannot find *.msi file'}) else: script_tc_res.update({'installname':installname}) json_bbparams_data.append(script_tc_res) else: if 'install' == installform.lower(): if 'autotools' in buildsys: for f in dir_list: #print ('f = {}'.format(f)) textout = '' texterr = '' if f.endswith("deploy"): parts = [] installname = f currdir=os.getcwd() #### try: parts = os.path.split(f) print ('redeploy {} with {} - {}'.format(installname, parts[0], parts[1])) os.chdir(parts[0]) test = subprocess.Popen(['./'+parts[1], '-force'], shell=False, universal_newlines=True) (textout, texterr) = test.communicate() thereturncode = test.returncode if textout is not None: output = '{}{}\n'.format(output, textout) if texterr is not None: output = '{}\n{}'.format(output, texterr) except Exception as e: script_tc_res.update({'installname':'deploy failed on '+installname}) json_bbparams_data.append(script_tc_res) print('Popen Exception: {}'.format(e)) os.chdir(currdir) #### print ('Unix Deploy File {} : {}'.format(installname,output)) if len(installname) == 0: thereturncode = 255 script_tc_res.update({'installname':'cannot find redeploy script file'}) else: script_tc_res.update({'installname':installname}) print ('File {} : {}'.format(installname, output)) json_bbparams_data.append(script_tc_res) else: for f in dir_list: textout = '' texterr = '' if f.endswith(".sh"): installname = f print ('installing {}'.format(installname)) try: test = subprocess.Popen([installname, '--exclude-subdir', '--skip-license'], shell=False, universal_newlines=True) (textout, texterr) = test.communicate() thereturncode = test.returncode if textout is not None: output = '{}{}\n'.format(output, textout) if texterr is not None: output = '{}\n{}'.format(output, texterr) except Exception as e: script_tc_res.update({'installname':'install script failed on '+installname}) json_bbparams_data.append(script_tc_res) print(e) print ('Unix File {} : {}'.format(installname,output)) if len(installname) == 0: thereturncode = 255 script_tc_res.update({'installname':'cannot find *.sh file'}) else: script_tc_res.update({'installname':installname}) json_bbparams_data.append(script_tc_res) else: print ('Uninstall unneeded') return thereturncode def ftp_uncompress(json_bbparams_data, HOST, DIRN, theplatform, toolset, ts_path, install_folder, target_binaries): script_tc_res = {} thereturncode = 0 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: for binary_dict in target_binaries: for qatestpath, binary_fname in binary_dict.items(): if toolset != 'default': binary_fname = '{}-{}'.format(binary_fname, ts_path) binary_file = binary_fname + util_functions.get_binary_ext(theplatform) print ('pull {} from {}'.format(binary_file, qatestpath)) try: sftpclient.chdir('{}{}'.format(DIRN,qatestpath)) except Exception as e: thereturncode = 255 print ('ERROR: cannot CD to "{}{}"'.format(DIRN,qatestpath)) if sftpclient is not None: sftpclient.close() print ('*** Changed to folder: "{}{}"'.format(DIRN,qatestpath)) currdir=os.getcwd() #### print ('current={}'.format(currdir)) filename = '' try: print ('install_folder={}'.format(install_folder)) install_path = os.path.join(currdir, install_folder) if not os.path.exists('{}'.format(install_path)): os.makedirs('{}'.format(install_path)) os.chdir('{}'.format(install_folder)) except Exception as e: thereturncode = 255 print ('ERROR: cannot create install folders {}:{}'.format(install_folder, e)) try: filename = '{}'.format(binary_file) print ('Getting {}'.format(filename)) sftpclient.get(filename, filename) print ('File {} downloaded'.format(filename)) except Exception as e: thereturncode = 255 print ('ERROR: cannot read file {}'.format(filename)) try: print ('Extracting {}'.format(binary_file)) util_functions.extract_binary(binary_file) except Exception as e: script_tc_res = {'ftp_uncompress-'+toolset: 'FAILED'} json_bbparams_data.append(script_tc_res) print ('ERROR: cannot extract file {}'.format(binary_file)) os.chdir(currdir) #### sftpclient.close() return thereturncode def ftp_uncompress_split(json_bbparams_data, HOST, DIRN, theplatform, toolset, ts_path, install_folder, target_binaries): script_tc_res = {} thereturncode = 0 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: for i,binary_dict in enumerate(target_binaries): for qatestpath, binary_fname in binary_dict.items(): if toolset != 'default': binary_fname = '{}-{}'.format(binary_fname, ts_path) binary_file = binary_fname + util_functions.get_binary_ext(theplatform) print ('pull {} from {}'.format(binary_file, qatestpath)) try: sftpclient.chdir('{}{}'.format(DIRN,qatestpath)) except Exception as e: thereturncode = 255 print ('ERROR: cannot CD to "{}{}"'.format(DIRN,qatestpath)) if sftpclient is not None: sftpclient.close() print ('*** Changed to folder: "{}{}"'.format(DIRN,qatestpath)) currdir=os.getcwd() #### print ('current={}'.format(currdir)) filename = '' try: print ('install_folder={}'.format(install_folder[i])) install_path = os.path.join(currdir, install_folder[i]) if not os.path.exists('{}'.format(install_path)): os.makedirs('{}'.format(install_path)) os.chdir('{}'.format(install_folder[i])) except Exception as e: thereturncode = 255 print ('ERROR: cannot create install folders {}:{}'.format(install_folder[i], e)) try: filename = '{}'.format(binary_file) print ('Getting {}'.format(filename)) sftpclient.get(filename, filename) print ('File {} downloaded'.format(filename)) except Exception as e: thereturncode = 255 print ('ERROR: cannot read file {}'.format(filename)) try: print ('Extracting {}'.format(binary_file)) util_functions.extract_binary(binary_file) except Exception as e: script_tc_res = {'ftp_uncompress-'+toolset: 'FAILED'} json_bbparams_data.append(script_tc_res) print ('ERROR: cannot extract file {}'.format(binary_file)) os.chdir(currdir) #### sftpclient.close() return thereturncode def ftp_upload(json_bbparams_data, HOST, DIRN, theplatform, toolset, ts_path, binary_dict): script_tc_res = {} thereturncode = 0 for qatestpath, binary_fname in binary_dict.items(): if toolset != 'default': binary_fname = '{}-{}'.format(binary_fname, ts_path) binary_file = binary_fname + util_functions.get_binary_ext(theplatform) print ('push {} to {}'.format(binary_fname, qatestpath)) print ('publish {} to {}'.format(binary_file, qatestpath)) currdir=os.getcwd() os.chdir(ts_path) #### if os.path.exists(binary_file): print ('upload {}'.format(binary_file)) filesize_b = os.path.getsize(binary_file) if filesize_b > 4096: binary_sha256 = '{}.sha256'.format(binary_file) binary_sha256_file = open(binary_sha256,'w') binary_sha256_file.write(util_functions.checksum_sha256(binary_file)) binary_sha256_file.write(' {}\n'.format(binary_file)) binary_sha256_file.close() 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: try: sftpclient.chdir( '{}{}'.format(DIRN, qatestpath)) print ('*** Changed to folder: "{}{}"'.format(DIRN, qatestpath)) except Exception as e: thereturncode = 255 print ('ERROR: cannot CD to "{}{}"'.format(DIRN, qatestpath)) if sftpclient is not None: sftpclient.close() currdir=os.getcwd() #### print ('current2={}'.format(currdir)) try: filename = '{}'.format(binary_file) print ('Putting {}'.format(filename)) sftpclient.put(filename, filename) print ('File {} uploaded'.format(filename)) except Exception as e: thereturncode = 255 print ('ERROR: cannot write file {}'.format(filename)) try: sftpclient.chmod(filename, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH) except Exception as e: print ('ERROR: cannot stat file {}'.format(filename)) try: filename = '{}'.format(binary_sha256) print ('Putting {}'.format(filename)) sftpclient.put(filename, filename) print ('File {} uploaded'.format(filename)) except Exception as e: thereturncode = 255 print ('ERROR: cannot write file {}'.format(filename)) try: sftpclient.chmod(filename, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH) except Exception as e: print ('ERROR: cannot stat file {}'.format(filename)) os.chdir(currdir) #### sftpclient.close() else: print ('Binary file {} size is too small'.format(binary_file)) thereturncode = 1 os.chdir(currdir) #### break return thereturncode