Commit 272a5c2c authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Move 'moreRecentFile' to D.S.Utils, make more use of it.

parent 7a973e54
......@@ -117,7 +117,8 @@ import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.BuildPaths
( autogenModulesDir )
import Distribution.Simple.Utils
( die, warn, info, setupMessage, createDirectoryIfMissingVerbose
( die, warn, info, setupMessage
, createDirectoryIfMissingVerbose, moreRecentFile
, intercalate, cabalVersion
, withFileContents, writeFileAtomic
, withTempFile )
......@@ -148,8 +149,7 @@ import Data.Maybe
import Data.Monoid
( Monoid(..) )
import System.Directory
( doesFileExist, getModificationTime,
createDirectoryIfMissing, getTemporaryDirectory )
( doesFileExist, createDirectoryIfMissing, getTemporaryDirectory )
import System.FilePath
( (</>), isAbsolute )
import qualified System.Info
......@@ -272,9 +272,7 @@ parseHeader header = case words header of
-- .cabal file.
checkPersistBuildConfigOutdated :: FilePath -> FilePath -> IO Bool
checkPersistBuildConfigOutdated distPref pkg_descr_file = do
t0 <- getModificationTime pkg_descr_file
t1 <- getModificationTime $ localBuildInfoFile distPref
return (t0 > t1)
pkg_descr_file `moreRecentFile` (localBuildInfoFile distPref)
-- |@dist\/setup-config@
localBuildInfoFile :: FilePath -> FilePath
......
......@@ -80,7 +80,7 @@ import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.BuildPaths (autogenModulesDir,cppHeaderName)
import Distribution.Simple.Utils
( createDirectoryIfMissingVerbose, withUTF8FileContents, writeUTF8File
, die, setupMessage, intercalate, copyFileVerbose
, die, setupMessage, intercalate, copyFileVerbose, moreRecentFile
, findFileWithExtension, findFileWithExtension' )
import Distribution.Simple.Program
( Program(..), ConfiguredProgram(..), programPath
......@@ -98,7 +98,7 @@ import Distribution.Verbosity
import Data.Maybe (fromMaybe)
import Data.List (nub)
import System.Directory (getModificationTime, doesFileExist)
import System.Directory (doesFileExist)
import System.Info (os, arch)
import System.FilePath (splitExtension, dropExtensions, (</>), (<.>),
takeDirectory, normalise, replaceExtension)
......@@ -298,10 +298,8 @@ preprocessFile searchLoc buildLoc forSDist baseFile verbosity builtinSuffixes ha
ppsrcFiles <- findFileWithExtension builtinSuffixes [buildLoc] baseFile
recomp <- case ppsrcFiles of
Nothing -> return True
Just ppsrcFile -> do
btime <- getModificationTime ppsrcFile
ptime <- getModificationTime psrcFile
return (btime < ptime)
Just ppsrcFile ->
psrcFile `moreRecentFile` ppsrcFile
when recomp $ do
let destDir = buildLoc </> dirName srcStem
createDirectoryIfMissingVerbose verbosity True destDir
......
......@@ -108,6 +108,9 @@ module Distribution.Simple.Utils (
parseFileGlob,
FileGlob(..),
-- * modification time
moreRecentFile,
-- * temp files and dirs
TempFileOptions(..), defaultTempFileOptions,
withTempFile, withTempFileEx,
......@@ -157,7 +160,8 @@ import qualified Data.ByteString.Lazy.Char8 as BS.Char8
import System.Directory
( Permissions(executable), getDirectoryContents, getPermissions
, doesDirectoryExist, doesFileExist, removeFile, findExecutable )
, doesDirectoryExist, doesFileExist, removeFile, findExecutable
, getModificationTime )
import System.Environment
( getProgName )
import System.Exit
......@@ -731,6 +735,23 @@ matchDirFileGlob dir filepath = case parseFileGlob filepath of
++ "' does not match any files."
matches -> return matches
--------------------
-- Modification time
-- | 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.
--
moreRecentFile :: FilePath -> FilePath -> IO Bool
moreRecentFile a b = do
exists <- doesFileExist b
if not exists
then return True
else do tb <- getModificationTime b
ta <- getModificationTime a
return (ta > tb)
----------------------------------------
-- Copying and installing files and dirs
......
......@@ -55,14 +55,14 @@ import Distribution.Text
import Distribution.Verbosity
( Verbosity, normal, lessVerbose )
import Distribution.Simple.Utils
( die, warn, info, fromUTF8, findPackageDesc )
( die, warn, info, fromUTF8, findPackageDesc, moreRecentFile )
import Data.Char (isAlphaNum)
import Data.Maybe (mapMaybe, fromMaybe)
import Data.List (isPrefixOf)
import Data.Monoid (Monoid(..))
import qualified Data.Map as Map
import Control.Monad (MonadPlus(mplus), when, unless, liftM)
import Control.Monad (MonadPlus(mplus), when, liftM)
import Control.Exception (evaluate)
import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
......@@ -78,9 +78,6 @@ import System.FilePath.Posix as FilePath.Posix
import System.IO
import System.IO.Unsafe (unsafeInterleaveIO)
import System.IO.Error (isDoesNotExistError)
import System.Directory
( getModificationTime, doesFileExist )
getInstalledPackages :: Verbosity -> Compiler
......@@ -235,15 +232,10 @@ updateRepoIndexCache verbosity repo =
indexFile = repoLocalDir repo </> "00-index.tar"
cacheFile = repoLocalDir repo </> "00-index.cache"
whenCacheOutOfDate :: FilePath-> FilePath -> IO () -> IO ()
whenCacheOutOfDate :: FilePath -> FilePath -> IO () -> IO ()
whenCacheOutOfDate origFile cacheFile action = do
exists <- doesFileExist cacheFile
if not exists
then action
else do
origTime <- getModificationTime origFile
cacheTime <- getModificationTime cacheFile
unless (cacheTime > origTime) action
cacheOutOfDate <- origFile `moreRecentFile` cacheFile
when cacheOutOfDate action
------------------------------------------------------------------------
......
......@@ -70,9 +70,9 @@ import Distribution.Simple.Setup
import Distribution.Simple.Utils
( die, debug, info, cabalVersion, findPackageDesc, comparing
, createDirectoryIfMissingVerbose, installExecutableFile
, rewriteFile, intercalate )
, moreRecentFile, rewriteFile, intercalate )
import Distribution.Client.Utils
( moreRecentFile, inDir, tryCanonicalizePath )
( inDir, tryCanonicalizePath )
import Distribution.System ( Platform(..), buildPlatform )
import Distribution.Text
( display )
......
......@@ -85,7 +85,7 @@ import qualified System.FilePath as FilePath.Native
import qualified System.FilePath.Windows as FilePath.Windows
import qualified System.FilePath.Posix as FilePath.Posix
import System.Directory
( getDirectoryContents, doesDirectoryExist, getModificationTime
( getDirectoryContents, doesDirectoryExist
, getPermissions, createDirectoryIfMissing, copyFile )
import qualified System.Directory as Permissions
( Permissions(executable) )
......
......@@ -2,7 +2,7 @@
module Distribution.Client.Utils ( MergeResult(..)
, mergeBy, duplicates, duplicatesBy
, moreRecentFile, inDir, numberOfProcessors
, inDir, numberOfProcessors
, removeExistingFile
, makeAbsoluteToCwd, filePathToByteString
, byteStringToFilePath, tryCanonicalizePath
......@@ -25,8 +25,8 @@ import Foreign.C.Types ( CInt(..) )
import qualified Control.Exception as Exception
( finally )
import System.Directory
( canonicalizePath, doesFileExist, getModificationTime
, getCurrentDirectory, removeFile, setCurrentDirectory )
( canonicalizePath, doesFileExist, getCurrentDirectory
, removeFile, setCurrentDirectory )
import System.FilePath
( (</>), isAbsolute )
import System.IO.Unsafe ( unsafePerformIO )
......@@ -65,20 +65,6 @@ duplicatesBy cmp = filter moreThanOne . groupBy eq . sortBy cmp
moreThanOne (_:_:_) = True
moreThanOne _ = False
-- | 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.
--
moreRecentFile :: FilePath -> FilePath -> IO Bool
moreRecentFile a b = do
exists <- doesFileExist b
if not exists
then return True
else do tb <- getModificationTime b
ta <- getModificationTime a
return (ta > tb)
-- | Like 'removeFile', but does not throw an exception when the file does not
-- exist.
removeExistingFile :: FilePath -> IO ()
......
......@@ -92,7 +92,6 @@ import Distribution.Client.Sandbox.PackageEnvironment
import Distribution.Client.Sandbox.Timestamp (maybeAddCompilerTimestampRecord)
import Distribution.Client.Sandbox.Types (UseSandbox(..), whenUsingSandbox)
import Distribution.Client.Init (initCabal)
import Distribution.Client.Utils (moreRecentFile)
import qualified Distribution.Client.Win32SelfUpgrade as Win32SelfUpgrade
import Distribution.Simple.Command
......@@ -108,7 +107,7 @@ import qualified Distribution.Simple.LocalBuildInfo as LBI
import Distribution.Simple.Program (defaultProgramConfiguration)
import qualified Distribution.Simple.Setup as Cabal
import Distribution.Simple.Utils
( cabalVersion, die, notice, info, topHandler )
( cabalVersion, die, notice, info, moreRecentFile, topHandler )
import Distribution.Text
( display )
import Distribution.Verbosity as Verbosity
......@@ -118,7 +117,8 @@ import qualified Paths_cabal_install (version)
import System.Environment (getArgs, getProgName)
import System.Exit (exitFailure)
import System.FilePath (splitExtension, takeExtension)
import System.IO (BufferMode(LineBuffering), hSetBuffering, stdout)
import System.IO (BufferMode(LineBuffering),
hSetBuffering, stdout)
import System.Directory (doesFileExist)
import Data.List (intercalate)
import Data.Monoid (Monoid(..))
......
Supports Markdown
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