Skip to content

Commit c84f12a

Browse files
authored
Remove utils.which in favor of shutil.which. NFC (#19627)
1 parent 3b0eb96 commit c84f12a

File tree

8 files changed

+22
-93
lines changed

8 files changed

+22
-93
lines changed

emcmake.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# found in the LICENSE file.
66

77
import os
8+
import shutil
89
import sys
910
from tools import shared
1011
from tools import config
@@ -47,9 +48,9 @@ def has_substr(args, substr):
4748
# toolchain was specified, to keep CMake from pulling in a native Visual
4849
# Studio, or Unix Makefiles.
4950
if utils.WINDOWS and not any(arg.startswith('-G') for arg in args):
50-
if utils.which('mingw32-make'):
51+
if shutil.which('mingw32-make'):
5152
args += ['-G', 'MinGW Makefiles']
52-
elif utils.which('ninja'):
53+
elif shutil.which('ninja'):
5354
args += ['-G', 'Ninja']
5455
else:
5556
print('emcmake: no compatible cmake generator found; Please install ninja or mingw32-make, or specify a generator explicitly using -G', file=sys.stderr)

emmake.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
generate JavaScript.
2222
"""
2323

24+
import shutil
2425
import sys
2526
from tools import building
2627
from tools import shared
@@ -48,7 +49,7 @@ def run():
4849
# On Windows prefer building with mingw32-make instead of make, if it exists.
4950
if utils.WINDOWS:
5051
if args[0] == 'make':
51-
mingw32_make = utils.which('mingw32-make')
52+
mingw32_make = shutil.which('mingw32-make')
5253
if mingw32_make:
5354
args[0] = mingw32_make
5455

test/emmake/make.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,13 @@
44
# found in the LICENSE file.
55

66
import os
7-
8-
9-
def which(program):
10-
def is_exe(fpath):
11-
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
12-
13-
fpath, fname = os.path.split(program)
14-
if fpath:
15-
if is_exe(program):
16-
return program
17-
else:
18-
for path in os.getenv("PATH", "").split(os.pathsep):
19-
exe_file = os.path.join(path, program)
20-
if is_exe(exe_file):
21-
return exe_file
22-
raise Exception('that is very bad')
7+
import shutil
238

249

2510
def test(var):
2611
val = os.getenv(var)
2712
print('%s=%s' % (var, val))
28-
print(which(val))
13+
print(shutil.which(val))
2914

3015

3116
def check_ar():

test/test_interactive.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from common import parameterized
1414
from common import BrowserCore, test_file, create_file, also_with_minimal_runtime
1515
from tools.shared import WINDOWS
16-
from tools.utils import which
1716

1817

1918
class interactive(BrowserCore):
@@ -186,7 +185,7 @@ def test_openal_capture(self):
186185

187186
def get_freealut_library(self):
188187
self.emcc_args += ['-Wno-pointer-sign']
189-
if WINDOWS and which('cmake'):
188+
if WINDOWS and shutil.which('cmake'):
190189
return self.get_library(os.path.join('third_party', 'freealut'), 'libalut.a', configure=['cmake', '.'], configure_args=['-DBUILD_TESTS=ON'])
191190
else:
192191
return self.get_library(os.path.join('third_party', 'freealut'), os.path.join('src', '.libs', 'libalut.a'), configure_args=['--disable-shared'])

test/test_other.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from tools import line_endings
4545
from tools import webassembly
4646

47-
scons_path = utils.which('scons')
47+
scons_path = shutil.which('scons')
4848
emmake = shared.bat_suffix(path_from_root('emmake'))
4949
emconfig = shared.bat_suffix(path_from_root('em-config'))
5050
emsize = shared.bat_suffix(path_from_root('emsize'))
@@ -126,7 +126,7 @@ def requires_ninja(func):
126126

127127
@wraps(func)
128128
def decorated(self, *args, **kwargs):
129-
if not utils.which('ninja'):
129+
if not shutil.which('ninja'):
130130
self.fail('test requires ninja to be installed (available in PATH)')
131131
return func(self, *args, **kwargs)
132132

@@ -138,7 +138,7 @@ def requires_scons(func):
138138

139139
@wraps(func)
140140
def decorated(self, *args, **kwargs):
141-
if not utils.which('scons'):
141+
if not shutil.which('scons'):
142142
if 'EMTEST_SKIP_SCONS' in os.environ:
143143
self.skipTest('test requires scons and EMTEST_SKIP_SCONS is set')
144144
else:
@@ -153,7 +153,7 @@ def requires_pkg_config(func):
153153

154154
@wraps(func)
155155
def decorated(self, *args, **kwargs):
156-
if not utils.which('pkg-config'):
156+
if not shutil.which('pkg-config'):
157157
if 'EMTEST_SKIP_PKG_CONFIG' in os.environ:
158158
self.skipTest('test requires pkg-config and EMTEST_SKIP_PKG_CONFIG is set')
159159
else:
@@ -758,7 +758,7 @@ def test_cmake(self, test_dir, output_file, cmake_args):
758758
for generator in generators:
759759
conf = configurations[generator]
760760

761-
if not utils.which(conf['build'][0]):
761+
if not shutil.which(conf['build'][0]):
762762
# Use simple test if applicable
763763
print('Skipping %s test for CMake support; build tool found found: %s.' % (generator, conf['build'][0]))
764764
continue

tools/config.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
# found in the LICENSE file.
55

66
import os
7+
import shutil
78
import sys
89
import logging
910
from typing import List, Optional
1011

1112
from . import utils, diagnostics
12-
from .utils import path_from_root, exit_with_error, __rootpath__, which
13+
from .utils import path_from_root, exit_with_error, __rootpath__
1314

1415
logger = logging.getLogger('config')
1516

@@ -102,7 +103,7 @@ def normalize_config_settings():
102103
def set_config_from_tool_location(config_key, tool_binary, f):
103104
val = globals()[config_key]
104105
if val is None:
105-
path = utils.which(tool_binary)
106+
path = shutil.which(tool_binary)
106107
if not path:
107108
if not os.path.exists(EM_CONFIG):
108109
diagnostics.warn('config file not found: %s. You can create one by hand or run `emcc --generate-config`', EM_CONFIG)
@@ -194,13 +195,13 @@ def generate_config(path):
194195
config_data = config_data.splitlines()[3:] # remove the initial comment
195196
config_data = '\n'.join(config_data)
196197
# autodetect some default paths
197-
llvm_root = os.path.dirname(which('wasm-ld') or '/usr/bin/wasm-ld')
198+
llvm_root = os.path.dirname(shutil.which('wasm-ld') or '/usr/bin/wasm-ld')
198199
config_data = config_data.replace('\'{{{ LLVM_ROOT }}}\'', repr(llvm_root))
199200

200-
binaryen_root = os.path.dirname(os.path.dirname(which('wasm-opt') or '/usr/local/bin/wasm-opt'))
201+
binaryen_root = os.path.dirname(os.path.dirname(shutil.which('wasm-opt') or '/usr/local/bin/wasm-opt'))
201202
config_data = config_data.replace('\'{{{ BINARYEN_ROOT }}}\'', repr(binaryen_root))
202203

203-
node = which('node') or which('nodejs') or 'node'
204+
node = shutil.which('node') or shutil.which('nodejs') or 'node'
204205
config_data = config_data.replace('\'{{{ NODE }}}\'', repr(node))
205206

206207
# write

tools/emdump.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import argparse
1313
import os
1414
import re
15+
import shutil
1516
import subprocess
1617
import sys
1718
from pathlib import Path
@@ -24,33 +25,6 @@
2425
options = None
2526

2627

27-
# Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'.
28-
def which(program):
29-
def is_exe(fpath):
30-
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
31-
32-
fpath, fname = os.path.split(program)
33-
if fpath:
34-
if is_exe(program):
35-
return program
36-
else:
37-
for path in os.environ["PATH"].split(os.pathsep):
38-
path = path.strip('"')
39-
exe_file = os.path.join(path, program)
40-
if is_exe(exe_file):
41-
return exe_file
42-
43-
if os.name == 'nt' and '.' not in fname:
44-
if is_exe(exe_file + '.exe'):
45-
return exe_file + '.exe'
46-
if is_exe(exe_file + '.cmd'):
47-
return exe_file + '.cmd'
48-
if is_exe(exe_file + '.bat'):
49-
return exe_file + '.bat'
50-
51-
return None
52-
53-
5428
# Given a string s and an index i, counts how many times character ch is repeated looking backwards at s[i], s[i-1], s[i-2], s[i-3], ...
5529
def rcount(s, ch, i):
5630
j = i
@@ -152,10 +126,10 @@ def is_javascript_symbol_char(ch):
152126

153127

154128
def cxxfilt():
155-
filt = which('llvm-cxxfilt')
129+
filt = shutil.which('llvm-cxxfilt')
156130
if filt:
157131
return filt
158-
return which('c++filt')
132+
return shutil.which('c++filt')
159133

160134

161135
# Runs the given symbols list through c++filt to demangle.

tools/utils.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,6 @@ def chdir(dir):
4040
os.chdir(orig_cwd)
4141

4242

43-
# Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'.
44-
def which(program):
45-
def is_exe(fpath):
46-
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
47-
48-
if os.path.isabs(program):
49-
if os.path.isfile(program):
50-
return program
51-
52-
if WINDOWS:
53-
for suffix in ['.exe', '.cmd', '.bat']:
54-
if is_exe(program + suffix):
55-
return program + suffix
56-
57-
fpath, fname = os.path.split(program)
58-
if fpath:
59-
if is_exe(program):
60-
return program
61-
else:
62-
for path in os.environ["PATH"].split(os.pathsep):
63-
path = path.strip('"')
64-
exe_file = os.path.join(path, program)
65-
if is_exe(exe_file):
66-
return exe_file
67-
if WINDOWS:
68-
for suffix in ('.exe', '.cmd', '.bat'):
69-
if is_exe(exe_file + suffix):
70-
return exe_file + suffix
71-
72-
return None
73-
74-
7543
def read_file(file_path):
7644
"""Read from a file opened in text mode"""
7745
with open(file_path, encoding='utf-8') as fh:

0 commit comments

Comments
 (0)