From b8b197969411b94d9cad847562773a2aafd910bf Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Wed, 17 Nov 2021 23:09:58 +0100 Subject: [PATCH] versions() fix monterey python2 issue (mac OS) --- lib/osinfo.js | 19 ++++++++++++------- lib/util.js | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/osinfo.js b/lib/osinfo.js index b144b02..17757cb 100644 --- a/lib/osinfo.js +++ b/lib/osinfo.js @@ -595,7 +595,7 @@ function versions(apps, callback) { } if ({}.hasOwnProperty.call(appsObj.versions, 'git')) { if (_darwin) { - const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/git'); + const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/git') || fs.existsSync('/opt/homebrew/bin/git'); if (util.darwinXcodeExists() || gitHomebrewExists) { exec('git --version', function (error, stdout) { if (!error) { @@ -780,9 +780,14 @@ function versions(apps, callback) { } if ({}.hasOwnProperty.call(appsObj.versions, 'python')) { if (_darwin) { - const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/python'); - if (util.darwinXcodeExists() || gitHomebrewExists) { - exec('python -V 2>&1', function (error, stdout) { + const stdout = execSync('sw_vers'); + const lines = stdout.toString().split('\n'); + const osVersion = util.getValue(lines, 'ProductVersion', ':'); + const gitHomebrewExists1 = fs.existsSync('/usr/local/Cellar/python'); + const gitHomebrewExists2 = fs.existsSync('/opt/homebrew/bin/python'); + if ((util.darwinXcodeExists() && util.semverCompare('12.0.1', osVersion) < 0) || gitHomebrewExists1 || gitHomebrewExists2) { + const cmd = gitHomebrewExists1 ? '/usr/local/Cellar/python -V 2>&1' : (gitHomebrewExists2 ? '/opt/homebrew/bin/python -V 2>&1' : 'python -V 2>&1') + exec(cmd, function (error, stdout) { if (!error) { const python = stdout.toString().split('\n')[0] || ''; appsObj.versions.python = python.toLowerCase().replace('python', '').trim(); @@ -804,7 +809,7 @@ function versions(apps, callback) { } if ({}.hasOwnProperty.call(appsObj.versions, 'python3')) { if (_darwin) { - const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/python3'); + const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/python3') || fs.existsSync('/opt/homebrew/bin/python3'); if (util.darwinXcodeExists() || gitHomebrewExists) { exec('python3 -V 2>&1', function (error, stdout) { if (!error) { @@ -828,7 +833,7 @@ function versions(apps, callback) { } if ({}.hasOwnProperty.call(appsObj.versions, 'pip')) { if (_darwin) { - const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/pip'); + const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/pip') || fs.existsSync('/opt/homebrew/bin/pip'); if (util.darwinXcodeExists() || gitHomebrewExists) { exec('pip -V 2>&1', function (error, stdout) { if (!error) { @@ -854,7 +859,7 @@ function versions(apps, callback) { } if ({}.hasOwnProperty.call(appsObj.versions, 'pip3')) { if (_darwin) { - const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/pip3'); + const gitHomebrewExists = fs.existsSync('/usr/local/Cellar/pip3') || fs.existsSync('/opt/homebrew/bin/pip3'); if (util.darwinXcodeExists() || gitHomebrewExists) { exec('pip3 -V 2>&1', function (error, stdout) { if (!error) { diff --git a/lib/util.js b/lib/util.js index 7bdce76..5f0d80d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1089,6 +1089,27 @@ function plistParser(xmlStr) { } } +function semverCompare(v1, v2) { + let res = 0; + const parts1 = v1.split('.'); + const parts2 = v2.split('.'); + if (parts1[0] < parts2[0]) { res = 1; } + else if (parts1[0] > parts2[0]) { res = -1; } + else if (parts1[0] === parts2[0] && parts1.length >= 2 && parts2.length >= 2) { + if (parts1[1] < parts2[1]) { res = 1; } + else if (parts1[1] > parts2[1]) { res = -1; } + else if (parts1[1] === parts2[1]) { + if (parts1.length >= 3 && parts2.length >= 3) { + if (parts1[2] < parts2[2]) { res = 1; } + else if (parts1[2] > parts2[2]) { res = -1; } + } else if (parts2.length >= 3) { + res = 1; + } + } + } + return res; +} + function noop() { } exports.toInt = toInt; @@ -1134,3 +1155,4 @@ exports.stringStartWith = stringStartWith; exports.mathMin = mathMin; exports.WINDIR = WINDIR; exports.getFilesInPath = getFilesInPath; +exports.semverCompare = semverCompare;