Skip to content

Commit

Permalink
Rewrite which function to use pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanwweber committed Apr 12, 2021
1 parent e0327a1 commit 44e2d29
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
2 changes: 1 addition & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ program main
write(*,'(a)') 'Hello, world!'
end program main
'''
if which(compiler) is not None:
if which(compiler):
env['F77'] = env['F90'] = env['F95'] = env['F03'] = env['FORTRAN'] = compiler
success, output = conf.TryRun(hello_world, '.f90')
if success and 'Hello, world!' in output:
Expand Down
26 changes: 8 additions & 18 deletions site_scons/buildutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,25 +526,15 @@ def mglob(env, subdir, *args):
return matches


def which(program):
""" Replicates the functionality of the 'which' shell command """
def is_exe(fpath):
for ext in ('', '.exe', '.bat'):
if os.path.exists(fpath + ext):
return os.access(fpath + ext, os.X_OK)
return False

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
def which(program: str) -> bool:
"""Replicates the functionality of the 'which' shell command."""
for ext in ("", ".exe", ".bat"):
fpath = Path(program + ext)
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file

return None
exe_file = Path(path).joinpath(fpath)
if exe_file.exists() and os.access(exe_file, os.X_OK):
return True
return False

optionWrapper = textwrap.TextWrapper(initial_indent=' ',
subsequent_indent=' ',
Expand Down

0 comments on commit 44e2d29

Please sign in to comment.