Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Romitkumar/prefer py3 over py2 #831

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions LCM/dsc/engine/ConsistencyInvoker/ConsistencyInvoker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 25 additions & 24 deletions LCM/dsc/engine/ca/CAInfrastructure/WebPullClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion LCM/scripts/InstallModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 13 additions & 9 deletions LCM/scripts/calcPythonPath.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 16 additions & 15 deletions Providers/PythonProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
60 changes: 30 additions & 30 deletions installbuilder/datafiles/Base_DSC.data
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down