/
Misc.hs
233 lines (199 loc) · 7.34 KB
/
Misc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
{-# LANGUAGE FlexibleContexts #-}
{-
- Copyright 2011-2014 Per Magnus Therning
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-}
module Util.Misc where
-- {{{1 imports
import qualified PkgDB as DB
import Util.Dist
import Control.Exception (onException)
import Control.Monad
import Control.Monad.Trans.Except
import Control.Monad.Reader
import Data.Either
import Data.Version
import Distribution.Compiler
import Distribution.PackageDescription
import Distribution.PackageDescription.Configuration
import Distribution.System
import Distribution.Text
import Options.Applicative
import Options.Applicative.Types
import Safe (lastMay)
import System.Exit
import System.IO
import System.Posix.Files
import System.Process
import System.Unix.Directory
import Text.ParserCombinators.ReadP hiding (many)
-- {{{ print functions
printUnSat (n, ds) = do
putStrLn $ "Failed to satisfy the following dependencies for " ++ n ++ ":"
mapM_ (putStrLn . (" " ++) . display) ds
printBrksOth ((n, v), brks) = do
putStrLn $ "Adding " ++ n ++ " " ++ display v ++ " would break:"
mapM_ (\ (bN, Just bD) -> putStrLn $ " " ++ bN ++ " : " ++ display bD) brks
-- {{{1 program variables
progName = "cblrepo"
dbName = progName ++ ".db"
ghcDefVersion = Version [7, 10, 1] []
ghcVersionDep :: Version -> Int -> String
ghcVersionDep ghcVer ghcRel = "ghc=" ++ display ghcVer ++ "-" ++ show ghcRel
indexUrl = "http://hackage.haskell.org/packages/index.tar.gz"
indexFileName = "index.tar.gz"
-- {{{1 command line parser helpers
readPkgNVersion :: ReadP (String, Version)
readPkgNVersion = do
n <- many (satisfy (/= ','))
char ','
v <- parseVersion
return (n, v)
readFlag :: ReadP (FlagName, Bool)
readFlag = readNegFlag <++ readPosFlag
where
readNegFlag = do
char '-'
n <- many (satisfy (/= ','))
return (FlagName n, False)
readPosFlag = do
n0 <- get
n <- many (satisfy (/= ','))
return (FlagName (n0 : n), True)
ghcVersionArgReader :: ReadM Version
ghcVersionArgReader = do
arg <- readerAsk
case lastMay $ readP_to_S parseVersion arg of
Just (v, "") -> return v
_ -> fail $ "cannot parse value `" ++ arg ++ "`"
pkgNVersionArgReader :: ReadM (String, Version)
pkgNVersionArgReader = do
s <- readerAsk
case lastMay (readP_to_S readPkgNVersion s) of
Just (r, "") -> return r
_ -> fail $ "Cannot parse '" ++ s ++ "' as PKG,VER"
ghcPkgArgReader :: ReadM (String, Version)
ghcPkgArgReader = pkgNVersionArgReader
distroPkgArgReader :: ReadM (String, Version, Int)
distroPkgArgReader = let
readDistroPkg = do
(n, v) <- readPkgNVersion
char ','
r <- many (satisfy (/= ','))
return (n, v, read r)
in do
s <- readerAsk
case lastMay (readP_to_S readDistroPkg s) of
Just (r, "") -> return r
_ -> fail $ "Cannot parse '" ++ s ++ "' as PKG,VER,REL"
strCblFileArgReader :: ReadM (FilePath, FlagAssignment)
strCblFileArgReader = let
readWithFlags = do
fn <- many $ satisfy (/= ':')
char ':'
fas <- sepBy readFlag (char ',')
return (fn, fas)
readWithoutFlags = do
fn <- many $ satisfy (/= ':')
return (fn, [])
in do
s <-readerAsk
case lastMay (readP_to_S (readWithFlags <++ readWithoutFlags) s) of
Just (r, "") -> return r
_ -> fail $ "Cannot parse '" ++ s ++ "' as FILE[:FLAG,-FLAG,..]"
strCblPkgArgReader :: ReadM (String, Version, FlagAssignment)
strCblPkgArgReader = let
readWithFlags = do
(n, v) <- readPkgNVersion
char ':'
fas <- sepBy readFlag (char ',')
return (n, v, fas)
readWithoutFlags = do
(n, v) <- readPkgNVersion
return (n, v, [])
in do
s <- readerAsk
case lastMay (readP_to_S (readWithFlags <++ readWithoutFlags) s) of
Just (r, "") -> return r
_ -> fail $ "Cannot parse: " ++ s
-- {{{1 command line argument type
data Cmds
= CmdAdd
{ patchDir :: FilePath, ghcVer :: Version, cmdAddGhcPkgs :: [(String, Version)]
, cmdAddDistroPkgs :: [(String, Version, Int)], cmdAddFileCbls :: [(FilePath, FlagAssignment)]
, cmdAddCbls :: [(String, Version, FlagAssignment)] }
| CmdBuildPkgs { pkgs :: [String] }
| CmdBumpPkgs { inclusive :: Bool, pkgs :: [String] }
| CmdSync { unused :: Bool }
| CmdVersions { latest :: Bool, pkgs :: [String] }
| CmdListPkgs
{ listGhc :: Bool, listDistro :: Bool, noListRepo :: Bool
, hackageFmt :: Bool, pkgs :: [String] }
| CmdUpdates { idxStyle :: Bool }
| CmdPkgBuild { ghcVer :: Version, ghcRel :: Int, patchDir :: FilePath, pkgs :: [String] }
| CmdConvertDb { inDbFile :: FilePath, outDbFile :: FilePath }
| CmdRemovePkg { pkgs :: [String] }
| CmdExtract { cmdExtractPkgs :: [(String, Version)] }
deriving (Show)
data Opts = Opts
{ appDir :: FilePath
, dbFile :: FilePath
, dryRun :: Bool
, optsCmd :: Cmds
} deriving (Show)
-- {{{1 getFromURL
getFromURL url fn = do
(ec, _, er) <- readProcessWithExitCode "curl" ["-f", "-L", "-o", fn, url] ""
case ec of
ExitSuccess -> return ()
ExitFailure _ -> do
hPutStrLn stderr ("Failed downloading " ++ url)
hPutStrLn stderr er
exitFailure
-- {{{1 applyPatchIfExist
applyPatch origFilename patchFilename = do
(ec, _, _) <- liftIO $ readProcessWithExitCode "patch" [origFilename, patchFilename] ""
case ec of
ExitSuccess -> return ()
ExitFailure _ ->
throwE ("Failed patching " ++ origFilename ++ " with " ++ patchFilename)
applyPatchIfExist origFilename patchFilename =
liftIO (fileExist patchFilename) >>= flip when (applyPatch origFilename patchFilename)
-- {{{1 finalising package descriptions
finalizePkg ghcVersion db fa gpd = finalizePackageDescription
fa
(DB.checkAgainstDb db n)
(Platform X86_64 buildOS) -- platform
(unknownCompilerInfo (CompilerId GHC ghcVersion) NoAbiTag) -- compiler version
[] -- no additional constraints
gpd
where
n = pkgNameStr gpd
-- {{{1 Command type
type Command = ReaderT Opts IO
runCommand cmds func = runReaderT func cmds
-- {{{1 ExceptT
reThrowE :: Monad m => Either a b -> ExceptT a m b
reThrowE = either throwE return
withTempDirExceptT :: MonadIO m => FilePath -> (FilePath -> ExceptT e IO b) -> ExceptT e m b
withTempDirExceptT fp func = do
r <- liftIO $ withTemporaryDirectory fp (runExceptT . func)
reThrowE r
exitOnAnyLefts vs = let
es = lefts vs
in
if not $ null $ lefts vs
then liftIO $ mapM_ (hPutStrLn stderr) es >> exitFailure
else return (rights vs)
exitOnException msg a = onException a $ hPutStrLn stderr msg >> exitFailure