Skip to content

Commit

Permalink
Remove utils.which in favor of shutil.which. NFC (#19627)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Jun 14, 2023
1 parent 3b0eb96 commit c84f12a
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 93 deletions.
5 changes: 3 additions & 2 deletions emcmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# found in the LICENSE file.

import os
import shutil
import sys
from tools import shared
from tools import config
Expand Down Expand Up @@ -47,9 +48,9 @@ def has_substr(args, substr):
# toolchain was specified, to keep CMake from pulling in a native Visual
# Studio, or Unix Makefiles.
if utils.WINDOWS and not any(arg.startswith('-G') for arg in args):
if utils.which('mingw32-make'):
if shutil.which('mingw32-make'):
args += ['-G', 'MinGW Makefiles']
elif utils.which('ninja'):
elif shutil.which('ninja'):
args += ['-G', 'Ninja']
else:
print('emcmake: no compatible cmake generator found; Please install ninja or mingw32-make, or specify a generator explicitly using -G', file=sys.stderr)
Expand Down
3 changes: 2 additions & 1 deletion emmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
generate JavaScript.
"""

import shutil
import sys
from tools import building
from tools import shared
Expand Down Expand Up @@ -48,7 +49,7 @@ def run():
# On Windows prefer building with mingw32-make instead of make, if it exists.
if utils.WINDOWS:
if args[0] == 'make':
mingw32_make = utils.which('mingw32-make')
mingw32_make = shutil.which('mingw32-make')
if mingw32_make:
args[0] = mingw32_make

Expand Down
19 changes: 2 additions & 17 deletions test/emmake/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,13 @@
# found in the LICENSE file.

import os


def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.getenv("PATH", "").split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
raise Exception('that is very bad')
import shutil


def test(var):
val = os.getenv(var)
print('%s=%s' % (var, val))
print(which(val))
print(shutil.which(val))


def check_ar():
Expand Down
3 changes: 1 addition & 2 deletions test/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from common import parameterized
from common import BrowserCore, test_file, create_file, also_with_minimal_runtime
from tools.shared import WINDOWS
from tools.utils import which


class interactive(BrowserCore):
Expand Down Expand Up @@ -186,7 +185,7 @@ def test_openal_capture(self):

def get_freealut_library(self):
self.emcc_args += ['-Wno-pointer-sign']
if WINDOWS and which('cmake'):
if WINDOWS and shutil.which('cmake'):
return self.get_library(os.path.join('third_party', 'freealut'), 'libalut.a', configure=['cmake', '.'], configure_args=['-DBUILD_TESTS=ON'])
else:
return self.get_library(os.path.join('third_party', 'freealut'), os.path.join('src', '.libs', 'libalut.a'), configure_args=['--disable-shared'])
Expand Down
10 changes: 5 additions & 5 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from tools import line_endings
from tools import webassembly

scons_path = utils.which('scons')
scons_path = shutil.which('scons')
emmake = shared.bat_suffix(path_from_root('emmake'))
emconfig = shared.bat_suffix(path_from_root('em-config'))
emsize = shared.bat_suffix(path_from_root('emsize'))
Expand Down Expand Up @@ -126,7 +126,7 @@ def requires_ninja(func):

@wraps(func)
def decorated(self, *args, **kwargs):
if not utils.which('ninja'):
if not shutil.which('ninja'):
self.fail('test requires ninja to be installed (available in PATH)')
return func(self, *args, **kwargs)

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

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

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

if not utils.which(conf['build'][0]):
if not shutil.which(conf['build'][0]):
# Use simple test if applicable
print('Skipping %s test for CMake support; build tool found found: %s.' % (generator, conf['build'][0]))
continue
Expand Down
11 changes: 6 additions & 5 deletions tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
# found in the LICENSE file.

import os
import shutil
import sys
import logging
from typing import List, Optional

from . import utils, diagnostics
from .utils import path_from_root, exit_with_error, __rootpath__, which
from .utils import path_from_root, exit_with_error, __rootpath__

logger = logging.getLogger('config')

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

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

node = which('node') or which('nodejs') or 'node'
node = shutil.which('node') or shutil.which('nodejs') or 'node'
config_data = config_data.replace('\'{{{ NODE }}}\'', repr(node))

# write
Expand Down
32 changes: 3 additions & 29 deletions tools/emdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import argparse
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
Expand All @@ -24,33 +25,6 @@
options = None


# Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'.
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file

if os.name == 'nt' and '.' not in fname:
if is_exe(exe_file + '.exe'):
return exe_file + '.exe'
if is_exe(exe_file + '.cmd'):
return exe_file + '.cmd'
if is_exe(exe_file + '.bat'):
return exe_file + '.bat'

return None


# 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], ...
def rcount(s, ch, i):
j = i
Expand Down Expand Up @@ -152,10 +126,10 @@ def is_javascript_symbol_char(ch):


def cxxfilt():
filt = which('llvm-cxxfilt')
filt = shutil.which('llvm-cxxfilt')
if filt:
return filt
return which('c++filt')
return shutil.which('c++filt')


# Runs the given symbols list through c++filt to demangle.
Expand Down
32 changes: 0 additions & 32 deletions tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,6 @@ def chdir(dir):
os.chdir(orig_cwd)


# Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'.
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

if os.path.isabs(program):
if os.path.isfile(program):
return program

if WINDOWS:
for suffix in ['.exe', '.cmd', '.bat']:
if is_exe(program + suffix):
return program + suffix

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
if WINDOWS:
for suffix in ('.exe', '.cmd', '.bat'):
if is_exe(exe_file + suffix):
return exe_file + suffix

return None


def read_file(file_path):
"""Read from a file opened in text mode"""
with open(file_path, encoding='utf-8') as fh:
Expand Down

0 comments on commit c84f12a

Please sign in to comment.