diff --git a/LCM/dsc/engine/ConsistencyInvoker/ConsistencyInvoker.c b/LCM/dsc/engine/ConsistencyInvoker/ConsistencyInvoker.c index 5df34cad0..d56c7bae3 100644 --- a/LCM/dsc/engine/ConsistencyInvoker/ConsistencyInvoker.c +++ b/LCM/dsc/engine/ConsistencyInvoker/ConsistencyInvoker.c @@ -61,35 +61,35 @@ char* getPythonProvider() char* result = malloc(1); *result = 0; - FILE* pipe = popen("python2 --version 2>&1", "r"); + FILE* pipe = popen("python3 -V 2>&1 | grep -Po '(?<=Python )(.+)'", "r"); if(!pipe) { - printf("Cant start command."); + printf("Cant start command."); } while(fgets(buffer, 128, pipe) != NULL) { result = realloc(result, (result ? strlen(result) : 0) + buffer_length ); strcat(result,buffer); } - // If python2 --version does not contain 'not found' return python2 - if(strstr(result, "not found") == NULL) { - return PYTHON2_COMMAND; + // Checking if Python version starts with 3.*.* + if(*result != '\0' && result[0] == '3') { + return PYTHON3_COMMAND; } - // Look for python3 + // Look for python2 result = malloc(1); *result = 0; - pipe = popen("python3 --version 2>&1", "r"); + pipe = popen("python2 -V 2>&1 | grep -Po '(?<=Python )(.+)'", "r"); if(!pipe) { - printf("Cant start command."); + printf("Cant start command."); } while(fgets(buffer, 128, pipe) != NULL) { result = realloc(result, (result ? strlen(result) : 0) + buffer_length ); strcat(result,buffer); } - // If python3 --version does not contain 'not found' return python3 - if(strstr(result, "not found") == NULL) { - return PYTHON3_COMMAND; + // Checking if Python version starts with 2.*.* + if(*result != '\0' && result[0] == '2') { + return PYTHON2_COMMAND; } return PYTHON_COMMAND; diff --git a/LCM/dsc/engine/ca/CAInfrastructure/WebPullClient.c b/LCM/dsc/engine/ca/CAInfrastructure/WebPullClient.c index 2cd52e9b6..f51e3838a 100755 --- a/LCM/dsc/engine/ca/CAInfrastructure/WebPullClient.c +++ b/LCM/dsc/engine/ca/CAInfrastructure/WebPullClient.c @@ -2073,38 +2073,39 @@ MI_Result MI_CALL Pull_GetModules(_Out_ MI_Uint32 * numModulesInstalled, } // Determine python version char data[BUFSIZ]; - int isPython2 = 1; + int isPython3 = 0; DSC_LOG_INFO("Assuming python2 in WebPullClient\n"); - // Look for python2 - FILE * pipe = popen("python2 --version 2>&1", "r"); + // Look for python3 + FILE * pipe = popen("python3 -V 2>&1 | grep -Po '(?<=Python )(.+)'", "r"); fgets(data, BUFSIZ, pipe); - if (!strstr(data, "not found")) + if (data[0] == '3') { - DSC_LOG_INFO("Found python2 in WebPullClient.\n"); - isPython2 = 1; - } + DSC_LOG_INFO("Found python3 in WebPullClient.\n"); + isPython3 = 1; + } else { - // If python2 does not exist, look for python3 + // If python3 does not exist, look for python2 memset(&data[0], 0, sizeof(data)); - pipe = popen("python3 --version 2>&1", "r"); - fgets(data, BUFSIZ, pipe); - if (!strstr(data, "not found")) { - DSC_LOG_INFO("Found python3 in WebPullClient.\n"); - isPython2 = 0; - } + pipe = popen("python2 -V 2>&1 | grep -Po '(?<=Python )(.+)'", "r"); + fgets(data, BUFSIZ, pipe); + if (data[0] == '2') + { + DSC_LOG_INFO("Found python2 in WebPullClient.\n"); + isPython3 = 0; + } } - if (isPython2 == 1) + if (isPython3 == 1) { - DSC_LOG_INFO("Calling InstallModule with python2"); - Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s %s", DSC_SCRIPT_PATH "/InstallModule.py", zipPath, verifyFlag); + DSC_LOG_INFO("Calling InstallModule with python3"); + Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s %s %s", "/usr/bin/python3 " DSC_SCRIPT_PATH "/python3/InstallModule.py", zipPath, verifyFlag, " 2>&1"); } else { - DSC_LOG_INFO("Calling InstallModule with python3"); - Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s %s %s", "/usr/bin/python3 " DSC_SCRIPT_PATH "/python3/InstallModule.py", zipPath, verifyFlag, " 2>&1"); + DSC_LOG_INFO("Calling InstallModule with python2"); + Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s %s", DSC_SCRIPT_PATH "/InstallModule.py", zipPath, verifyFlag); } DSC_LOG_INFO("executing '%T'\n", stringBuffer); retval = system(stringBuffer); @@ -2119,15 +2120,15 @@ MI_Result MI_CALL Pull_GetModules(_Out_ MI_Uint32 * numModulesInstalled, else { // Attempt to remove the module as a last resort. If it fails too, a reinstall may be necessary. - if (isPython2 == 1) + if (isPython3 == 1) { - DSC_LOG_INFO("Calling RemoveModule with python2"); - Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s", DSC_SCRIPT_PATH "/RemoveModule.py", current->moduleName); + DSC_LOG_INFO("Calling RemoveModule with python3"); + Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s %s", "/usr/bin/python3 " DSC_SCRIPT_PATH "/python3/RemoveModule.py", current->moduleName, " 2>&1"); } else { - DSC_LOG_INFO("Calling RemoveModule with python3"); - Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s %s", "/usr/bin/python3 " DSC_SCRIPT_PATH "/python3/RemoveModule.py", current->moduleName, " 2>&1"); + DSC_LOG_INFO("Calling RemoveModule with python2"); + Snprintf(stringBuffer, MAX_URL_LENGTH, "%s %s", DSC_SCRIPT_PATH "/RemoveModule.py", current->moduleName); } retval = system(stringBuffer); diff --git a/LCM/scripts/InstallModule.py b/LCM/scripts/InstallModule.py index 03f72c483..b00217900 100755 --- a/LCM/scripts/InstallModule.py +++ b/LCM/scripts/InstallModule.py @@ -66,7 +66,7 @@ def getPlatformArchitectureFolderName(): def regenerateDscPythonScriptInitFiles(): regenerateInitFilesScriptPath = join(helperlib.DSC_SCRIPT_PATH, 'RegenerateInitFiles.py') - regenerateInitFilesResult = subprocess.call("(python " + regenerateInitFilesScriptPath + ")", shell=True) + regenerateInitFilesResult = subprocess.call("(python2 " + regenerateInitFilesScriptPath + ")", shell=True) if regenerateInitFilesResult != 0: exitWithError("Failed to regenerate the DSC __init__.py files with the result code", regenerateInitFilesResult) diff --git a/LCM/scripts/calcPythonPath.sh b/LCM/scripts/calcPythonPath.sh index 6b91dd1b1..ea0a0553a 100755 --- a/LCM/scripts/calcPythonPath.sh +++ b/LCM/scripts/calcPythonPath.sh @@ -4,17 +4,21 @@ if [ "$#" -ne 0 ] then # Checking which python version is available # python3 - if [[ -z $(python2 --version 2>&1 | grep 'not found') ]] - then - #echo "python exists"; - pythonVer="python2" - elif [[ -z $(python3 --version 2>&1 | grep 'not found') ]] + version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') + if [ ! -z "$version" ] then #echo "python3 exists"; - pythonVer="python3" - else - echo "'python2' or 'python3' not found on this machine. Please install python." - exit 1 + pythonVer="python3" + else + version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') + if [ ! -z "$version" ] + then + #echo "python exists"; + pythonVer="python2" + else + echo "'python2' or 'python3' not found on this machine. Please install python." + exit 1 + fi fi # Detemine file location diff --git a/Providers/PythonProvider.cpp b/Providers/PythonProvider.cpp index 4b0706832..e580741bc 100755 --- a/Providers/PythonProvider.cpp +++ b/Providers/PythonProvider.cpp @@ -58,40 +58,41 @@ std::string determinePythonVersion(){ char* result = (char*)malloc(1); *result = 0; - // Check for python2 - FILE* pipe = popen("python2 --version 2>&1", "r"); + // Check for python3 + FILE* pipe = popen("python3 -V 2>&1 | grep -Po '(?<=Python )(.+)'", "r"); if(!pipe) { - std::cout << "Couldn't start command." << std::endl; + std::cout << "Couldn't start command." << std::endl; } - while(fgets(buffer, 128, pipe) != NULL) { result = (char*)realloc(result, (result ? strlen(result) : 0) + buffer_length ); strcat(result,buffer); } - // If python2 --version does not contain 'not found' return python2 - if(strstr(result, "not found") == NULL) { - std::cout << "Found python2." << std::endl; - return "python2"; + // Checking if Python version starts with 3.*.* + if(*result != '\0' && result[0] == '3') { + std::cout << "Found python3." << std::endl; + return "python3"; } - // Look for python3 + // Look for python2 result = (char*)malloc(1); *result = 0; - pipe = popen("python3 --version 2>&1", "r"); + pipe = popen("python2 -V 2>&1 | grep -Po '(?<=Python )(.+)'", "r"); if(!pipe) { - std::cout << "Couldn't start command." << std::endl; + std::cout << "Couldn't start command." << std::endl; } + while(fgets(buffer, 128, pipe) != NULL) { result = (char*)realloc(result, (result ? strlen(result) : 0) + buffer_length ); strcat(result,buffer); } - // If python3 --version does not contain 'not found' return python3 - if(strstr(result, "not found") == NULL) { - std::cout << "Found python3." << std::endl; - return "python3"; + // Checking if Python version starts with 2.*.* + if(*result != '\0' && result[0] == '2') { + std::cout << "Found python2." << std::endl; + return "python2"; } + return "python"; } diff --git a/installbuilder/datafiles/Base_DSC.data b/installbuilder/datafiles/Base_DSC.data index 68638c5c2..9b3d24d30 100755 --- a/installbuilder/datafiles/Base_DSC.data +++ b/installbuilder/datafiles/Base_DSC.data @@ -214,15 +214,15 @@ fi %Preinstall_200 # pythonVersion check must be repeated for each section -version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') +version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python2" - pythonVersion="python2" + echo "Using python3" + pythonVersion="python3" else - version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') + version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python3" - pythonVersion="python3" + echo "Using python2" + pythonVersion="python2" else echo "Python not found." fi @@ -374,15 +374,15 @@ chmod 1775 /opt/microsoft/omsconfig/Scripts # pythonVersion check must be repeated for each section -version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') +version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python2" - pythonVersion="python2" + echo "Using python3" + pythonVersion="python3" else - version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') + version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python3" - pythonVersion="python3" + echo "Using python2" + pythonVersion="python2" else echo "Python not found." fi @@ -466,15 +466,15 @@ if [ -d "/etc/opt/omi/conf/omsconfig" ]; then chown -R omsagent:omiusers /etc/op #else # pythonVersion check must be repeated for each section -version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') +version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python2" - pythonVersion="python2" + echo "Using python3" + pythonVersion="python3" else - version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') + version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python3" - pythonVersion="python3" + echo "Using python2" + pythonVersion="python2" else echo "Python not found." fi @@ -577,15 +577,15 @@ mv /etc/crontabtmp /etc/crontab #if BUILD_OMS == 1 # pythonVersion check must be repeated for each section -version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') +version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python2" - pythonVersion="python2" + echo "Using python3" + pythonVersion="python3" else - version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') + version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python3" - pythonVersion="python3" + echo "Using python2" + pythonVersion="python2" else echo "Python not found." fi @@ -623,15 +623,15 @@ echo "Cleaned up existing dsc_hosts before uninstall..." %Preuninstall_999 # pythonVersion check must be repeated for each section -version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') +version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python2" - pythonVersion="python2" + echo "Using python3" + pythonVersion="python3" else - version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') + version=$(python2 -V 2>&1 | grep -Po '(?<=Python )(.+)') if [ ! -z "$version" ]; then - echo "Using python3" - pythonVersion="python3" + echo "Using python2" + pythonVersion="python2" else echo "Python not found." fi