Skip to content

Commit 96ae673

Browse files
jstuder-ghmprobst
authored andcommitted
feat: default to python3 in check-clang-format
Python 2 is no longer supported and some distributions (such as Ubuntu 20.04) ship without a working Python 2 interpreter. The Python git-clang-format script works with both Python 2 and Python 3, so this changes the check-clang-format.js script to test for Python availability and choose Python 3 if available.
1 parent 02ecb76 commit 96ae673

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

bin/check-clang-format.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
const spawn = require('child_process').spawnSync;
1414
const path = require('path');
1515

16+
function getPythonInterpreter() {
17+
const interpreterOptions = ['python3', 'python', 'python2'];
18+
for (const opt of interpreterOptions) {
19+
const result = spawn(opt, ['--version'], {encoding: 'utf-8'});
20+
if (result.status === 0) {
21+
return opt;
22+
}
23+
}
24+
return null;
25+
}
26+
1627
function checkGitConfig() {
1728
const spawn_opts = {encoding: 'utf-8', stdio: ['pipe', 'pipe', 'inherit']};
1829
const binary = spawn('git', ['config', '--get', 'clangFormat.binary'], spawn_opts).stdout.trim();
@@ -46,6 +57,12 @@ function main(args) {
4657
let clangFormatPath;
4758
let configCheck;
4859

60+
const interpreter = getPythonInterpreter();
61+
if (interpreter === null) {
62+
console.error('No python interpreter found');
63+
return 2;
64+
}
65+
4966
try {
5067
clangFormatPath = path.dirname(require.resolve('clang-format'));
5168
configCheck = checkGitConfig();
@@ -60,7 +77,7 @@ function main(args) {
6077
}
6178

6279
const gitClangFormatPath = path.join(clangFormatPath, 'bin/git-clang-format');
63-
const result = spawn('python', [gitClangFormatPath, '--diff'], {encoding: 'utf-8'});
80+
const result = spawn(interpreter, [gitClangFormatPath, '--diff'], {encoding: 'utf-8'});
6481

6582
if (result.error) {
6683
console.error('Error running git-clang-format:', result.error);

0 commit comments

Comments
 (0)