Portability | portable |
---|---|
Maintainer | cabal-devel@haskell.org |
Safe Haskell | None |
A large and somewhat miscellaneous collection of utility functions used
throughout the rest of the Cabal lib and in other tools that use the Cabal
lib like cabal-install
. It has a very simple set of logging actions. It
has low level functions for running programs, a bunch of wrappers for
various directory and file functions that do extra logging.
- cabalVersion :: Version
- die :: String -> IO a
- dieWithLocation :: FilePath -> Maybe Int -> String -> IO a
- topHandler :: IO a -> IO a
- topHandlerWith :: forall a. (SomeException -> IO a) -> IO a -> IO a
- warn :: Verbosity -> String -> IO ()
- notice :: Verbosity -> String -> IO ()
- setupMessage :: Verbosity -> String -> PackageIdentifier -> IO ()
- info :: Verbosity -> String -> IO ()
- debug :: Verbosity -> String -> IO ()
- debugNoWrap :: Verbosity -> String -> IO ()
- chattyTry :: String -> IO () -> IO ()
- printRawCommandAndArgs :: Verbosity -> FilePath -> [String] -> IO ()
- printRawCommandAndArgsAndEnv :: Verbosity -> FilePath -> [String] -> Maybe [(String, String)] -> IO ()
- handleDoesNotExist :: a -> IO a -> IO a
- rawSystemExit :: Verbosity -> FilePath -> [String] -> IO ()
- rawSystemExitCode :: Verbosity -> FilePath -> [String] -> IO ExitCode
- rawSystemExitWithEnv :: Verbosity -> FilePath -> [String] -> [(String, String)] -> IO ()
- rawSystemStdout :: Verbosity -> FilePath -> [String] -> IO String
- rawSystemStdInOut :: Verbosity -> FilePath -> [String] -> Maybe FilePath -> Maybe [(String, String)] -> Maybe (String, Bool) -> Bool -> IO (String, String, ExitCode)
- rawSystemIOWithEnv :: Verbosity -> FilePath -> [String] -> Maybe FilePath -> Maybe [(String, String)] -> Maybe Handle -> Maybe Handle -> Maybe Handle -> IO ExitCode
- createProcessWithEnv :: Verbosity -> FilePath -> [String] -> Maybe FilePath -> Maybe [(String, String)] -> StdStream -> StdStream -> StdStream -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
- maybeExit :: IO ExitCode -> IO ()
- xargs :: Int -> ([String] -> IO ()) -> [String] -> [String] -> IO ()
- findProgramLocation :: Verbosity -> FilePath -> IO (Maybe FilePath)
- findProgramVersion :: String -> (String -> String) -> Verbosity -> FilePath -> IO (Maybe Version)
- smartCopySources :: Verbosity -> [FilePath] -> FilePath -> [ModuleName] -> [String] -> IO ()
- createDirectoryIfMissingVerbose :: Verbosity -> Bool -> FilePath -> IO ()
- copyFileVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
- copyDirectoryRecursiveVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
- copyFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
- copyFileTo :: Verbosity -> FilePath -> FilePath -> IO ()
- installOrdinaryFile :: Verbosity -> FilePath -> FilePath -> IO ()
- installExecutableFile :: Verbosity -> FilePath -> FilePath -> IO ()
- installMaybeExecutableFile :: Verbosity -> FilePath -> FilePath -> IO ()
- installOrdinaryFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
- installExecutableFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
- installMaybeExecutableFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
- installDirectoryContents :: Verbosity -> FilePath -> FilePath -> IO ()
- copyDirectoryRecursive :: Verbosity -> FilePath -> FilePath -> IO ()
- doesExecutableExist :: FilePath -> IO Bool
- setFileOrdinary :: FilePath -> IO ()
- setFileExecutable :: FilePath -> IO ()
- currentDir :: FilePath
- shortRelativePath :: FilePath -> FilePath -> FilePath
- dropExeExtension :: FilePath -> FilePath
- exeExtensions :: [String]
- findFile :: [FilePath] -> FilePath -> IO FilePath
- findFirstFile :: (a -> FilePath) -> [a] -> IO (Maybe a)
- findFileWithExtension :: [String] -> [FilePath] -> FilePath -> IO (Maybe FilePath)
- findFileWithExtension' :: [String] -> [FilePath] -> FilePath -> IO (Maybe (FilePath, FilePath))
- findAllFilesWithExtension :: [String] -> [FilePath] -> FilePath -> IO [FilePath]
- findModuleFile :: [FilePath] -> [String] -> ModuleName -> IO (FilePath, FilePath)
- findModuleFiles :: [FilePath] -> [String] -> [ModuleName] -> IO [(FilePath, FilePath)]
- getDirectoryContentsRecursive :: FilePath -> IO [FilePath]
- isInSearchPath :: FilePath -> IO Bool
- addLibraryPath :: OS -> [FilePath] -> [(String, String)] -> [(String, String)]
- matchFileGlob :: FilePath -> IO [FilePath]
- matchDirFileGlob :: FilePath -> FilePath -> IO [FilePath]
- parseFileGlob :: FilePath -> Maybe FileGlob
- data FileGlob
- moreRecentFile :: FilePath -> FilePath -> IO Bool
- existsAndIsMoreRecentThan :: FilePath -> FilePath -> IO Bool
- data TempFileOptions = TempFileOptions {
- optKeepTempFiles :: Bool
- defaultTempFileOptions :: TempFileOptions
- withTempFile :: FilePath -> String -> (FilePath -> Handle -> IO a) -> IO a
- withTempFileEx :: TempFileOptions -> FilePath -> String -> (FilePath -> Handle -> IO a) -> IO a
- withTempDirectory :: Verbosity -> FilePath -> String -> (FilePath -> IO a) -> IO a
- withTempDirectoryEx :: Verbosity -> TempFileOptions -> FilePath -> String -> (FilePath -> IO a) -> IO a
- defaultPackageDesc :: Verbosity -> IO FilePath
- findPackageDesc :: FilePath -> IO (Either String FilePath)
- tryFindPackageDesc :: FilePath -> IO FilePath
- defaultHookedPackageDesc :: IO (Maybe FilePath)
- findHookedPackageDesc :: FilePath -> IO (Maybe FilePath)
- withFileContents :: FilePath -> (String -> IO a) -> IO a
- writeFileAtomic :: FilePath -> ByteString -> IO ()
- rewriteFile :: FilePath -> String -> IO ()
- fromUTF8 :: String -> String
- toUTF8 :: String -> String
- readUTF8File :: FilePath -> IO String
- withUTF8FileContents :: FilePath -> (String -> IO a) -> IO a
- writeUTF8File :: FilePath -> String -> IO ()
- normaliseLineEndings :: String -> String
- startsWithBOM :: String -> Bool
- fileHasBOM :: FilePath -> IO Bool
- ignoreBOM :: String -> String
- dropWhileEndLE :: (a -> Bool) -> [a] -> [a]
- takeWhileEndLE :: (a -> Bool) -> [a] -> [a]
- equating :: Eq a => (b -> a) -> b -> b -> Bool
- comparing :: Ord a => (b -> a) -> b -> b -> Ordering
- isInfixOf :: Eq a => [a] -> [a] -> Bool
- intercalate :: [a] -> [[a]] -> [a]
- lowercase :: String -> String
- listUnion :: Ord a => [a] -> [a] -> [a]
- listUnionRight :: Ord a => [a] -> [a] -> [a]
- ordNub :: Ord a => [a] -> [a]
- ordNubRight :: Ord a => [a] -> [a]
- safeTail :: [a] -> [a]
- wrapText :: String -> String
- wrapLine :: Int -> [String] -> [[String]]
Documentation
logging and errors
die :: String -> IO a
dieWithLocation :: FilePath -> Maybe Int -> String -> IO a
topHandler :: IO a -> IO a
topHandlerWith :: forall a. (SomeException -> IO a) -> IO a -> IO a
warn :: Verbosity -> String -> IO ()
Non fatal conditions that may be indicative of an error or problem.
We display these at the normal
verbosity level.
notice :: Verbosity -> String -> IO ()
Useful status messages.
We display these at the normal
verbosity level.
This is for the ordinary helpful status messages that users see. Just enough information to know that things are working but not floods of detail.
setupMessage :: Verbosity -> String -> PackageIdentifier -> IO ()
info :: Verbosity -> String -> IO ()
More detail on the operation of some action.
We display these messages when the verbosity level is verbose
debug :: Verbosity -> String -> IO ()
Detailed internal debugging information
We display these messages when the verbosity level is deafening
debugNoWrap :: Verbosity -> String -> IO ()
A variant of debug
that doesn't perform the automatic line
wrapping. Produces better output in some cases.
:: String | a description of the action we were attempting |
-> IO () | the action itself |
-> IO () |
Perform an IO action, catching any IO exceptions and printing an error if one occurs.
printRawCommandAndArgs :: Verbosity -> FilePath -> [String] -> IO ()
printRawCommandAndArgsAndEnv :: Verbosity -> FilePath -> [String] -> Maybe [(String, String)] -> IO ()
exceptions
handleDoesNotExist :: a -> IO a -> IO a
Run an IO computation, returning e
if it raises a file
does not exist error.
running programs
rawSystemExit :: Verbosity -> FilePath -> [String] -> IO ()
rawSystemExitCode :: Verbosity -> FilePath -> [String] -> IO ExitCode
rawSystemExitWithEnv :: Verbosity -> FilePath -> [String] -> [(String, String)] -> IO ()
rawSystemStdout :: Verbosity -> FilePath -> [String] -> IO String
Run a command and return its output.
The output is assumed to be text in the locale encoding.
:: Verbosity | |
-> FilePath | Program location |
-> [String] | Arguments |
-> Maybe FilePath | New working dir or inherit |
-> Maybe [(String, String)] | New environment or inherit |
-> Maybe (String, Bool) | input text and binary mode |
-> Bool | output in binary mode |
-> IO (String, String, ExitCode) | output, errors, exit |
Run a command and return its output, errors and exit status. Optionally also supply some input. Also provides control over whether the binary/text mode of the input and output.
:: Verbosity | |
-> FilePath | |
-> [String] | |
-> Maybe FilePath | New working dir or inherit |
-> Maybe [(String, String)] | New environment or inherit |
-> Maybe Handle | stdin |
-> Maybe Handle | stdout |
-> Maybe Handle | stderr |
-> IO ExitCode |
:: Verbosity | |
-> FilePath | |
-> [String] | |
-> Maybe FilePath | New working dir or inherit |
-> Maybe [(String, String)] | New environment or inherit |
-> StdStream | stdin |
-> StdStream | stdout |
-> StdStream | stderr |
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) | Any handles created for stdin, stdout, or stderr
with |
maybeExit :: IO ExitCode -> IO ()
xargs :: Int -> ([String] -> IO ()) -> [String] -> [String] -> IO ()
Like the Unix xargs program. Useful for when we've got very long command lines that might overflow an OS limit on command line length and so you need to invoke a command multiple times to get all the args in.
Use it with either of the rawSystem variants above. For example:
xargs (32*1024) (rawSystemExit verbosity) prog fixedArgs bigArgs
findProgramLocation :: Verbosity -> FilePath -> IO (Maybe FilePath)
Deprecated: No longer used within Cabal, try findProgramOnSearchPath
Look for a program on the path.
:: String | version args |
-> (String -> String) | function to select version number from program output |
-> Verbosity | |
-> FilePath | location |
-> IO (Maybe Version) |
Look for a program and try to find it's version number. It can accept either an absolute path or the name of a program binary, in which case we will look for the program on the path.
copying files
smartCopySources :: Verbosity -> [FilePath] -> FilePath -> [ModuleName] -> [String] -> IO ()
Deprecated: Use findModuleFiles and copyFiles or installOrdinaryFiles
createDirectoryIfMissingVerbose
:: Verbosity | |
-> Bool | Create its parents too? |
-> FilePath | |
-> IO () |
Same as createDirectoryIfMissing
but logs at higher verbosity levels.
copyFileVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
Copies a file without copying file permissions. The target file is created with default permissions. Any existing target file is replaced.
At higher verbosity levels it logs an info message.
copyDirectoryRecursiveVerbose :: Verbosity -> FilePath -> FilePath -> IO ()
Deprecated: You probably want installDirectoryContents instead
copyFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
Copies a bunch of files to a target directory, preserving the directory structure in the target location. The target directories are created if they do not exist.
The files are identified by a pair of base directory and a path relative to that base. It is only the relative part that is preserved in the destination.
For example:
copyFiles normal "dist/src" [("", "src/Foo.hs"), ("dist/build/", "src/Bar.hs")]
This would copy "src/Foo.hs" to "dist/src/src/Foo.hs" and copy "dist/build/src/Bar.hs" to "dist/src/src/Bar.hs".
This operation is not atomic. Any IO failure during the copy (including any missing source files) leaves the target in an unknown state so it is best to use it with a freshly created directory so that it can be simply deleted if anything goes wrong.
copyFileTo :: Verbosity -> FilePath -> FilePath -> IO ()
Given a relative path to a file, copy it to the given directory, preserving the relative path and creating the parent directories if needed.
installing files
installOrdinaryFile :: Verbosity -> FilePath -> FilePath -> IO ()
Install an ordinary file. This is like a file copy but the permissions are set appropriately for an installed file. On Unix it is "-rw-r--r--" while on Windows it uses the default permissions for the target directory.
installExecutableFile :: Verbosity -> FilePath -> FilePath -> IO ()
Install an executable file. This is like a file copy but the permissions are set appropriately for an installed file. On Unix it is "-rwxr-xr-x" while on Windows it uses the default permissions for the target directory.
installMaybeExecutableFile :: Verbosity -> FilePath -> FilePath -> IO ()
Install a file that may or not be executable, preserving permissions.
installOrdinaryFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
This is like copyFiles
but uses installOrdinaryFile
.
installExecutableFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
This is like copyFiles
but uses installExecutableFile
.
installMaybeExecutableFiles :: Verbosity -> FilePath -> [(FilePath, FilePath)] -> IO ()
This is like copyFiles
but uses installMaybeExecutableFile
.
installDirectoryContents :: Verbosity -> FilePath -> FilePath -> IO ()
This installs all the files in a directory to a target location, preserving the directory layout. All the files are assumed to be ordinary rather than executable files.
copyDirectoryRecursive :: Verbosity -> FilePath -> FilePath -> IO ()
Recursively copy the contents of one directory to another path.
File permissions
doesExecutableExist :: FilePath -> IO Bool
Like doesFileExist
, but also checks that the file is executable.
setFileOrdinary :: FilePath -> IO ()
setFileExecutable :: FilePath -> IO ()
file names
currentDir :: FilePath
The path name that represents the current directory.
In Unix, it's "."
, but this is system-specific.
(E.g. AmigaOS uses the empty string ""
for the current directory.)
shortRelativePath :: FilePath -> FilePath -> FilePath
dropExeExtension :: FilePath -> FilePath
Drop the extension if it's one of exeExtensions
, or return the path
unchanged.
exeExtensions :: [String]
List of possible executable file extensions on the current platform.
finding files
:: [FilePath] | search locations |
-> FilePath | File Name |
-> IO FilePath |
Find a file by looking in a search path. The file path must match exactly.
findFirstFile :: (a -> FilePath) -> [a] -> IO (Maybe a)
findFileWithExtension :: [String] -> [FilePath] -> FilePath -> IO (Maybe FilePath)
Find a file by looking in a search path with one of a list of possible file extensions. The file base name should be given and it will be tried with each of the extensions in each element of the search path.
findFileWithExtension' :: [String] -> [FilePath] -> FilePath -> IO (Maybe (FilePath, FilePath))
Like findFileWithExtension
but returns which element of the search path
the file was found in, and the file path relative to that base directory.
findAllFilesWithExtension :: [String] -> [FilePath] -> FilePath -> IO [FilePath]
:: [FilePath] | build prefix (location of objects) |
-> [String] | search suffixes |
-> ModuleName | module |
-> IO (FilePath, FilePath) |
Find the file corresponding to a Haskell module name.
This is similar to findFileWithExtension'
but specialised to a module
name. The function fails if the file corresponding to the module is missing.
:: [FilePath] | build prefix (location of objects) |
-> [String] | search suffixes |
-> [ModuleName] | modules |
-> IO [(FilePath, FilePath)] |
Finds the files corresponding to a list of Haskell module names.
As findModuleFile
but for a list of module names.
getDirectoryContentsRecursive :: FilePath -> IO [FilePath]
List all the files in a directory and all subdirectories.
The order places files in sub-directories after all the files in their parent directories. The list is generated lazily so is not well defined if the source directory structure changes before the list is used.
environment variables
isInSearchPath :: FilePath -> IO Bool
Is this directory in the system search path?
addLibraryPath :: OS -> [FilePath] -> [(String, String)] -> [(String, String)]
simple file globbing
matchFileGlob :: FilePath -> IO [FilePath]
matchDirFileGlob :: FilePath -> FilePath -> IO [FilePath]
parseFileGlob :: FilePath -> Maybe FileGlob
data FileGlob
modification time
moreRecentFile :: FilePath -> FilePath -> IO Bool
Compare the modification times of two files to see if the first is newer than the second. The first file must exist but the second need not. The expected use case is when the second file is generated using the first. In this use case, if the result is True then the second file is out of date.
existsAndIsMoreRecentThan :: FilePath -> FilePath -> IO Bool
Like moreRecentFile
, but also checks that the first file exists.
temp files and dirs
data TempFileOptions
Advanced options for withTempFile
and withTempDirectory
.
TempFileOptions | |
|
:: FilePath | Temp dir to create the file in |
-> String | File name template. See |
-> (FilePath -> Handle -> IO a) | |
-> IO a |
Use a temporary filename that doesn't already exist.
:: TempFileOptions | |
-> FilePath | Temp dir to create the file in |
-> String | File name template. See |
-> (FilePath -> Handle -> IO a) | |
-> IO a |
A version of withTempFile
that additionally takes a TempFileOptions
argument.
withTempDirectory :: Verbosity -> FilePath -> String -> (FilePath -> IO a) -> IO a
Create and use a temporary directory.
Creates a new temporary directory inside the given directory, making use of the template. The temp directory is deleted after use. For example:
withTempDirectory verbosity "src" "sdist." $ \tmpDir -> do ...
The tmpDir
will be a new subdirectory of the given directory, e.g.
src/sdist.342
.
withTempDirectoryEx :: Verbosity -> TempFileOptions -> FilePath -> String -> (FilePath -> IO a) -> IO a
A version of withTempDirectory
that additionally takes a
TempFileOptions
argument.
.cabal and .buildinfo files
defaultPackageDesc :: Verbosity -> IO FilePath
Package description file (pkgname.cabal
)
:: FilePath | Where to look |
-> IO (Either String FilePath) | pkgname.cabal |
Find a package description file in the given directory. Looks for
.cabal
files.
tryFindPackageDesc :: FilePath -> IO FilePath
Like findPackageDesc
, but calls die
in case of error.
defaultHookedPackageDesc :: IO (Maybe FilePath)
Optional auxiliary package information file (pkgname.buildinfo
)
:: FilePath | Directory to search |
-> IO (Maybe FilePath) | dir |
Find auxiliary package information in the given directory.
Looks for .buildinfo
files.
reading and writing files safely
withFileContents :: FilePath -> (String -> IO a) -> IO a
Gets the contents of a file, but guarantee that it gets closed.
The file is read lazily but if it is not fully consumed by the action then the remaining input is truncated and the file is closed.
writeFileAtomic :: FilePath -> ByteString -> IO ()
Writes a file atomically.
The file is either written successfully or an IO exception is raised and the original file is left unchanged.
On windows it is not possible to delete a file that is open by a process. This case will give an IO exception but the atomic property is not affected.
rewriteFile :: FilePath -> String -> IO ()
Write a file but only if it would have new content. If we would be writing the same as the existing content then leave the file as is so that we do not update the file's modification time.
NB: the file is assumed to be ASCII-encoded.
Unicode
fromUTF8 :: String -> String
toUTF8 :: String -> String
readUTF8File :: FilePath -> IO String
Reads a UTF8 encoded text file as a Unicode String
Reads lazily using ordinary readFile
.
withUTF8FileContents :: FilePath -> (String -> IO a) -> IO a
Reads a UTF8 encoded text file as a Unicode String
Same behaviour as withFileContents
.
writeUTF8File :: FilePath -> String -> IO ()
Writes a Unicode String as a UTF8 encoded text file.
Uses writeFileAtomic
, so provides the same guarantees.
normaliseLineEndings :: String -> String
Fix different systems silly line ending conventions
BOM
startsWithBOM :: String -> Bool
Whether BOM is at the beginning of the input
fileHasBOM :: FilePath -> IO Bool
Check whether a file has Unicode byte order mark (BOM).
ignoreBOM :: String -> String
Ignore a Unicode byte order mark (BOM) at the beginning of the input
generic utils
dropWhileEndLE :: (a -> Bool) -> [a] -> [a]
takeWhileEndLE :: (a -> Bool) -> [a] -> [a]
takeWhileEndLE p
is equivalent to reverse . takeWhile p . reverse
, but
is usually faster (as well as being easier to read).
equating :: Eq a => (b -> a) -> b -> b -> Bool
comparing :: Ord a => (b -> a) -> b -> b -> Ordering
isInfixOf :: Eq a => [a] -> [a] -> Bool
intercalate :: [a] -> [[a]] -> [a]
lowercase :: String -> String
listUnion :: Ord a => [a] -> [a] -> [a]
Like Data.List.union, but has O(n log n)
complexity instead of
O(n^2)
.
listUnionRight :: Ord a => [a] -> [a] -> [a]
ordNub :: Ord a => [a] -> [a]
Like Data.List.nub, but has O(n log n)
complexity instead of
O(n^2)
. Code for ordNub
and listUnion
taken from Niklas Hambüchen's
ordnub package.
ordNubRight :: Ord a => [a] -> [a]
safeTail :: [a] -> [a]
A total variant of tail
.
wrapText :: String -> String
Wraps text to the default line width. Existing newlines are preserved.
wrapLine :: Int -> [String] -> [[String]]
Wraps a list of words to a list of lines of words of a particular width.