-
Notifications
You must be signed in to change notification settings - Fork 841
/
Copy pathPackageDump.hs
329 lines (298 loc) · 11.8 KB
/
PackageDump.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module Stack.PackageDump
( Line
, eachSection
, eachPair
, DumpPackage (..)
, conduitDumpPackage
, ghcPkgDump
, ghcPkgDescribe
, sinkMatching
, pruneDeps
) where
import Stack.Prelude
import Data.Attoparsec.Args
import Data.Attoparsec.Text as P
import Data.Conduit
import qualified Data.Conduit.List as CL
import qualified Data.Conduit.Text as CT
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified RIO.Text as T
import qualified Distribution.Text as C
import Path.Extra (toFilePathNoTrailingSep)
import Stack.GhcPkg
import Stack.Types.Config (HasCompiler (..), GhcPkgExe (..), DumpPackage (..))
import Stack.Types.GhcPkgId
import RIO.Process hiding (readProcess)
-- | Call ghc-pkg dump with appropriate flags and stream to the given @Sink@, for a single database
ghcPkgDump
:: (HasProcessContext env, HasLogFunc env)
=> GhcPkgExe
-> [Path Abs Dir] -- ^ if empty, use global
-> ConduitM Text Void (RIO env) a
-> RIO env a
ghcPkgDump pkgexe = ghcPkgCmdArgs pkgexe ["dump"]
-- | Call ghc-pkg describe with appropriate flags and stream to the given @Sink@, for a single database
ghcPkgDescribe
:: (HasProcessContext env, HasLogFunc env, HasCompiler env)
=> GhcPkgExe
-> PackageName
-> [Path Abs Dir] -- ^ if empty, use global
-> ConduitM Text Void (RIO env) a
-> RIO env a
ghcPkgDescribe pkgexe pkgName' = ghcPkgCmdArgs pkgexe ["describe", "--simple-output", packageNameString pkgName']
-- | Call ghc-pkg and stream to the given @Sink@, for a single database
ghcPkgCmdArgs
:: (HasProcessContext env, HasLogFunc env)
=> GhcPkgExe
-> [String]
-> [Path Abs Dir] -- ^ if empty, use global
-> ConduitM Text Void (RIO env) a
-> RIO env a
ghcPkgCmdArgs pkgexe@(GhcPkgExe pkgPath) cmd mpkgDbs sink = do
logInfo "ghcPkgCmdArgs (1)"
logInfo $ "args: " <> displayShow args
case reverse mpkgDbs of
(pkgDb:_) -> createDatabase pkgexe pkgDb -- TODO maybe use some retry logic instead?
_ -> return ()
logInfo "ghcPkgCmdArgs (2)"
(_,res) <- sinkProcessStderrStdout (toFilePath pkgPath) args CL.sinkNull sink'
pure res
where
args = concat
[ case mpkgDbs of
[] -> ["--global", "--no-user-package-db"]
_ -> ["--user", "--no-user-package-db"] ++
concatMap (\pkgDb -> ["--package-db", toFilePathNoTrailingSep pkgDb]) mpkgDbs
, cmd
, ["--expand-pkgroot"]
]
sink' = CT.decodeUtf8 .| sink
-- | Prune a list of possible packages down to those whose dependencies are met.
--
-- * id uniquely identifies an item
--
-- * There can be multiple items per name
pruneDeps
:: (Ord name, Ord id)
=> (id -> name) -- ^ extract the name from an id
-> (item -> id) -- ^ the id of an item
-> (item -> [id]) -- ^ get the dependencies of an item
-> (item -> item -> item) -- ^ choose the desired of two possible items
-> [item] -- ^ input items
-> Map name item
pruneDeps getName getId getDepends chooseBest =
Map.fromList
. fmap (getName . getId &&& id)
. loop Set.empty Set.empty []
where
loop foundIds usedNames foundItems dps =
case partitionEithers $ map depsMet dps of
([], _) -> foundItems
(s', dps') ->
let foundIds' = Map.fromListWith chooseBest s'
foundIds'' = Set.fromList $ map getId $ Map.elems foundIds'
usedNames' = Map.keysSet foundIds'
foundItems' = Map.elems foundIds'
in loop
(Set.union foundIds foundIds'')
(Set.union usedNames usedNames')
(foundItems ++ foundItems')
(catMaybes dps')
where
depsMet dp
| name `Set.member` usedNames = Right Nothing
| all (`Set.member` foundIds) (getDepends dp) = Left (name, dp)
| otherwise = Right $ Just dp
where
id' = getId dp
name = getName id'
-- | Find the package IDs matching the given constraints with all dependencies installed.
-- Packages not mentioned in the provided @Map@ are allowed to be present too.
sinkMatching :: Monad m
=> Map PackageName Version -- ^ allowed versions
-> ConduitM DumpPackage o m (Map PackageName DumpPackage)
sinkMatching allowed =
Map.fromList
. map (pkgName . dpPackageIdent &&& id)
. Map.elems
. pruneDeps
id
dpGhcPkgId
dpDepends
const -- Could consider a better comparison in the future
<$> (CL.filter (isAllowed . dpPackageIdent) .| CL.consume)
where
isAllowed (PackageIdentifier name version) =
case Map.lookup name allowed of
Just version' | version /= version' -> False
_ -> True
data PackageDumpException
= MissingSingleField Text (Map Text [Line])
| Couldn'tParseField Text [Line]
deriving Typeable
instance Exception PackageDumpException
instance Show PackageDumpException where
show (MissingSingleField name values) = unlines $
return (concat
[ "Expected single value for field name "
, show name
, " when parsing ghc-pkg dump output:"
]) ++ map (\(k, v) -> " " ++ show (k, v)) (Map.toList values)
show (Couldn'tParseField name ls) =
"Couldn't parse the field " ++ show name ++ " from lines: " ++ show ls
-- | Convert a stream of bytes into a stream of @DumpPackage@s
conduitDumpPackage :: MonadThrow m
=> ConduitM Text DumpPackage m ()
conduitDumpPackage = (.| CL.catMaybes) $ eachSection $ do
pairs <- eachPair (\k -> (k, ) <$> CL.consume) .| CL.consume
let m = Map.fromList pairs
let parseS k =
case Map.lookup k m of
Just [v] -> return v
_ -> throwM $ MissingSingleField k m
-- Can't fail: if not found, same as an empty list. See:
-- https://github.com/fpco/stack/issues/182
parseM k = Map.findWithDefault [] k m
parseDepend :: MonadThrow m => Text -> m (Maybe GhcPkgId)
parseDepend "builtin_rts" = return Nothing
parseDepend bs =
liftM Just $ parseGhcPkgId bs'
where
(bs', _builtinRts) =
case stripSuffixText " builtin_rts" bs of
Nothing ->
case stripPrefixText "builtin_rts " bs of
Nothing -> (bs, False)
Just x -> (x, True)
Just x -> (x, True)
case Map.lookup "id" m of
Just ["builtin_rts"] -> return Nothing
_ -> do
name <- parseS "name" >>= parsePackageNameThrowing . T.unpack
version <- parseS "version" >>= parseVersionThrowing . T.unpack
ghcPkgId <- parseS "id" >>= parseGhcPkgId
-- if a package has no modules, these won't exist
let libDirKey = "library-dirs"
libraries = parseM "hs-libraries"
exposedModules = parseM "exposed-modules"
exposed = parseM "exposed"
license =
case parseM "license" of
[licenseText] -> C.simpleParse (T.unpack licenseText)
_ -> Nothing
depends <- mapMaybeM parseDepend $ concatMap T.words $ parseM "depends"
-- Handle sublibs by recording the name of the parent library
-- If name of parent library is missing, this is not a sublib.
let mkParentLib n = PackageIdentifier n version
parentLib = mkParentLib <$> (parseS "package-name" >>=
parsePackageNameThrowing . T.unpack)
let parseQuoted key =
case mapM (P.parseOnly (argsParser NoEscaping)) val of
Left{} -> throwM (Couldn'tParseField key val)
Right dirs -> return (concat dirs)
where
val = parseM key
libDirPaths <- parseQuoted libDirKey
haddockInterfaces <- parseQuoted "haddock-interfaces"
haddockHtml <- parseQuoted "haddock-html"
return $ Just DumpPackage
{ dpGhcPkgId = ghcPkgId
, dpPackageIdent = PackageIdentifier name version
, dpParentLibIdent = parentLib
, dpLicense = license
, dpLibDirs = libDirPaths
, dpLibraries = T.words $ T.unwords libraries
, dpHasExposedModules = not (null libraries || null exposedModules)
-- Strip trailing commas from ghc package exposed-modules (looks buggy to me...).
-- Then try to parse the module names.
, dpExposedModules =
Set.fromList
$ mapMaybe (C.simpleParse . T.unpack . T.dropSuffix ",")
$ T.words
$ T.unwords exposedModules
, dpDepends = depends
, dpHaddockInterfaces = haddockInterfaces
, dpHaddockHtml = listToMaybe haddockHtml
, dpIsExposed = exposed == ["True"]
}
stripPrefixText :: Text -> Text -> Maybe Text
stripPrefixText x y
| x `T.isPrefixOf` y = Just $ T.drop (T.length x) y
| otherwise = Nothing
stripSuffixText :: Text -> Text -> Maybe Text
stripSuffixText x y
| x `T.isSuffixOf` y = Just $ T.take (T.length y - T.length x) y
| otherwise = Nothing
-- | A single line of input, not including line endings
type Line = Text
-- | Apply the given Sink to each section of output, broken by a single line containing ---
eachSection :: Monad m
=> ConduitM Line Void m a
-> ConduitM Text a m ()
eachSection inner =
CL.map (T.filter (/= '\r')) .| CT.lines .| start
where
peekText = await >>= maybe (return Nothing) (\bs ->
if T.null bs
then peekText
else leftover bs >> return (Just bs))
start = peekText >>= maybe (return ()) (const go)
go = do
x <- toConsumer $ takeWhileC (/= "---") .| inner
yield x
CL.drop 1
start
-- | Grab each key/value pair
eachPair :: Monad m
=> (Text -> ConduitM Line Void m a)
-> ConduitM Line a m ()
eachPair inner =
start
where
start = await >>= maybe (return ()) start'
start' bs1 =
toConsumer (valSrc .| inner key) >>= yield >> start
where
(key, bs2) = T.break (== ':') bs1
(spaces, bs3) = T.span (== ' ') $ T.drop 1 bs2
indent = T.length key + 1 + T.length spaces
valSrc
| T.null bs3 = noIndent
| otherwise = yield bs3 >> loopIndent indent
noIndent = do
mx <- await
case mx of
Nothing -> return ()
Just bs -> do
let (spaces, val) = T.span (== ' ') bs
if T.length spaces == 0
then leftover val
else do
yield val
loopIndent (T.length spaces)
loopIndent i =
loop
where
loop = await >>= maybe (return ()) go
go bs
| T.length spaces == i && T.all (== ' ') spaces =
yield val >> loop
| otherwise = leftover bs
where
(spaces, val) = T.splitAt i bs
-- | General purpose utility
takeWhileC :: Monad m => (a -> Bool) -> ConduitM a a m ()
takeWhileC f =
loop
where
loop = await >>= maybe (return ()) go
go x
| f x = yield x >> loop
| otherwise = leftover x