Commit 3929d248 authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Add a helper for checking whether a given executable exists.

parent d7bbef5b
...@@ -61,7 +61,7 @@ import Distribution.Simple.Program.Find ...@@ -61,7 +61,7 @@ import Distribution.Simple.Program.Find
import Distribution.Simple.Program.Builtin import Distribution.Simple.Program.Builtin
( builtinPrograms ) ( builtinPrograms )
import Distribution.Simple.Utils import Distribution.Simple.Utils
( die ) ( die, doesExecutableExist )
import Distribution.Version import Distribution.Version
( Version, VersionRange, isAnyVersion, withinRange ) ( Version, VersionRange, isAnyVersion, withinRange )
import Distribution.Text import Distribution.Text
...@@ -76,9 +76,6 @@ import Data.Maybe ...@@ -76,9 +76,6 @@ import Data.Maybe
import qualified Data.Map as Map import qualified Data.Map as Map
import Control.Monad import Control.Monad
( join, foldM ) ( join, foldM )
import System.Directory
( doesFileExist )
-- ------------------------------------------------------------ -- ------------------------------------------------------------
-- * Programs database -- * Programs database
...@@ -300,7 +297,7 @@ configureProgram verbosity prog conf = do ...@@ -300,7 +297,7 @@ configureProgram verbosity prog conf = do
Nothing -> programFindLocation prog verbosity (progSearchPath conf) Nothing -> programFindLocation prog verbosity (progSearchPath conf)
>>= return . fmap FoundOnSystem >>= return . fmap FoundOnSystem
Just path -> do Just path -> do
absolute <- doesFileExist path absolute <- doesExecutableExist path
if absolute if absolute
then return (Just (UserSpecified path)) then return (Just (UserSpecified path))
else findProgramOnSearchPath verbosity (progSearchPath conf) path else findProgramOnSearchPath verbosity (progSearchPath conf) path
......
...@@ -31,11 +31,11 @@ module Distribution.Simple.Program.Find ( ...@@ -31,11 +31,11 @@ module Distribution.Simple.Program.Find (
import Distribution.Verbosity import Distribution.Verbosity
( Verbosity ) ( Verbosity )
import Distribution.Simple.Utils import Distribution.Simple.Utils
( debug ) ( debug, doesExecutableExist )
import Distribution.System import Distribution.System
( OS(..), buildOS ) ( OS(..), buildOS )
import System.Directory import System.Directory
( findExecutable, doesFileExist, Permissions(..), getPermissions ) ( findExecutable )
import Distribution.Compat.Environment import Distribution.Compat.Environment
( getEnvironment ) ( getEnvironment )
import System.FilePath import System.FilePath
...@@ -97,26 +97,20 @@ findProgramOnSearchPath verbosity searchpath prog = do ...@@ -97,26 +97,20 @@ findProgramOnSearchPath verbosity searchpath prog = do
-- https://ghc.haskell.org/trac/ghc/ticket/2184 -- https://ghc.haskell.org/trac/ghc/ticket/2184
mExe <- findExecutable prog mExe <- findExecutable prog
case mExe of case mExe of
Just exe -> do isExe <- checkExe exe Just exe -> do
if isExe exeExists <- doesExecutableExist exe
then return mExe if exeExists
else return Nothing then return mExe
else return Nothing
_ -> return mExe _ -> return mExe
findFirstExe [] = return Nothing findFirstExe [] = return Nothing
findFirstExe (f:fs) = do findFirstExe (f:fs) = do
isExe <- checkExe f isExe <- doesExecutableExist f
if isExe if isExe
then return (Just f) then return (Just f)
else findFirstExe fs else findFirstExe fs
checkExe f = do
exists <- doesFileExist f
if exists
then do perms <- getPermissions f
return (executable perms)
else return False
-- | Interpret a 'ProgramSearchPath' to construct a new @$PATH@ env var. -- | Interpret a 'ProgramSearchPath' to construct a new @$PATH@ env var.
-- Note that this is close but not perfect because on Windows the search -- Note that this is close but not perfect because on Windows the search
-- algorithm looks at more than just the @%PATH%@. -- algorithm looks at more than just the @%PATH%@.
......
...@@ -84,6 +84,7 @@ module Distribution.Simple.Utils ( ...@@ -84,6 +84,7 @@ module Distribution.Simple.Utils (
installDirectoryContents, installDirectoryContents,
-- * File permissions -- * File permissions
doesExecutableExist,
setFileOrdinary, setFileOrdinary,
setFileExecutable, setFileExecutable,
...@@ -980,6 +981,18 @@ installDirectoryContents verbosity srcDir destDir = do ...@@ -980,6 +981,18 @@ installDirectoryContents verbosity srcDir destDir = do
srcFiles <- getDirectoryContentsRecursive srcDir srcFiles <- getDirectoryContentsRecursive srcDir
installOrdinaryFiles verbosity destDir [ (srcDir, f) | f <- srcFiles ] installOrdinaryFiles verbosity destDir [ (srcDir, f) | f <- srcFiles ]
-------------------
-- File permissions
-- | Like 'doesFileExist', but also checks that the file is executable.
doesExecutableExist :: FilePath -> IO Bool
doesExecutableExist f = do
exists <- doesFileExist f
if exists
then do perms <- getPermissions f
return (executable perms)
else return False
--------------------------------- ---------------------------------
-- Deprecated file copy functions -- Deprecated file copy functions
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment