1{-# LANGUAGE ScopedTypeVariables #-}
2module Stackage.LoadDatabase where
4import qualified Codec.Archive.Tar as Tar
5import qualified Codec.Compression.GZip as GZip
6import Control.Exception (IOException, handle)
7import Control.Monad (guard, foldM)
8import qualified Data.ByteString.Lazy as L
9import qualified Data.ByteString.Lazy.Char8 as L8
10import Data.List (stripPrefix)
11import qualified Data.Map as Map
12import Data.Maybe (catMaybes, listToMaybe,
14import Data.Monoid (Monoid (..))
15import Data.Set (member)
16import qualified Data.Set as Set
17import Distribution.Compiler (CompilerFlavor (GHC))
18import Distribution.Package (Dependency (Dependency))
19import Distribution.PackageDescription (Condition (..),
25 buildInfo, buildTools,
33 flagDefault, flagName,
35 homepage, libBuildInfo,
39import Distribution.PackageDescription.Parse (ParseResult (ParseOk),
40 parsePackageDescription)
41import Distribution.System (buildArch, buildOS)
42import Distribution.Text (simpleParse)
43import Distribution.Version (Version (Version),
46import Stackage.Config (convertGithubUser)
49import System.Directory (doesFileExist, getDirectoryContents)
50import System.FilePath ((<.>), (</>))
52-- | Load the raw package database.
54-- We want to put in some restrictions:
56-- * Drop all core packages. We never want to install a new version of
57-- those, nor include them in the package list.
59-- * For packages with a specific version bound, find the maximum matching
62-- * For other packages, select the maximum version number.
63loadPackageDB :: SelectSettings
64 -> Map PackageName Version -- ^ core packages from HP file
65 -> Set PackageName -- ^ all core packages, including extras
66 -> Map PackageName (VersionRange, Maintainer) -- ^ additional deps
67 -> Set PackageName -- ^ underlay packages to exclude
69loadPackageDB settings coreMap core deps underlay = do
70 tarName <- getTarballName
71 lbs <- L.readFile tarName
72 pdb <- addEntries mempty $ Tar.read lbs
73 contents <- handle (\(_ :: IOException) -> return [])
74 $ getDirectoryContents $ selectTarballDir settings
75 pdb' <- foldM addTarball pdb $ mapMaybe stripTarGz contents
76 return $ excludeUnderlay pdb'
78 addEntries _ (Tar.Fail e) = error $ show e
79 addEntries db Tar.Done = return db
80 addEntries db (Tar.Next e es) = addEntry db e >>= flip addEntries es
82 stripTarGz = fmap reverse . stripPrefix (reverse ".tar.gz") . reverse
85 let GhcMajorVersion x y = selectGhcVersion settings
86 in Version [x, y, 2] []
88 addEntry :: PackageDB -> Tar.Entry -> IO PackageDB
90 case getPackageVersion e of
93 | p `member` core -> return pdb
95 case Map.lookup p deps of
96 Just (vrange, _maintainer)
97 | not $ withinRange v vrange -> return pdb
99 let pkgname = packageVersionString (p, v)
100 tarball = selectTarballDir settings </> pkgname <.> "tar.gz"
101 case Tar.entryContent e of
102 Tar.NormalFile bs _ -> addPackage p v bs pdb
105 addTarball :: PackageDB -> FilePath -> IO PackageDB
106 addTarball pdb tarball' = do
107 lbs <- L.readFile tarball
108 let (v', p') = break (== '-') $ reverse tarball'
109 p = PackageName $ reverse $ drop 1 p'
110 v <- maybe (error $ "Invalid tarball name: " ++ tarball) return
111 $ simpleParse $ reverse v'
112 case Map.lookup p deps of
114 | not $ withinRange v vrange -> return pdb
115 _ -> findCabalAndAddPackage tarball p v pdb $ Tar.read $ GZip.decompress lbs
117 tarball = selectTarballDir settings </> tarball' <.> "tar.gz"
119 excludeUnderlay :: PackageDB -> PackageDB
120 excludeUnderlay (PackageDB pdb) =
121 PackageDB $ Map.filterWithKey (\k _ -> Set.notMember k underlay) pdb
123 skipTests p = p `Set.member` skippedTests settings
125 -- Find the relevant cabal file in the given entries and add its contents
126 -- to the package database
127 findCabalAndAddPackage tarball p v pdb =
133 expectedPath = let PackageName p' = p in concat
134 [ packageVersionString (p, v)
139 loop Tar.Done = error $ concat
140 [ "Missing cabal file "
145 loop (Tar.Fail e) = error $ concat
146 [ "Unable to read tarball "
151 loop (Tar.Next entry rest)
152 | map fixPath (Tar.entryPath entry) == expectedPath =
153 case Tar.entryContent entry of
154 Tar.NormalFile bs _ -> addPackage p v bs pdb
160 , " was not a normal file"
162 | otherwise = loop rest
164 addPackage p v lbs pdb = do
165 let (deps', hasTests, buildToolsExe', buildToolsOther', mgpd, execs, mgithub) = parseDeps p lbs
166 return $ mappend pdb $ PackageDB $ Map.singleton p PackageInfo
169 , piHasTests = hasTests
170 , piBuildToolsExe = buildToolsExe'
171 , piBuildToolsAll = buildToolsExe' `Set.union` buildToolsOther'
174 , piGithubUser = fromMaybe [] mgithub
178 case parsePackageDescription $ L8.unpack lbs of
179 ParseOk _ gpd -> (mconcat
180 [ maybe mempty (go gpd) $ condLibrary gpd
181 , mconcat $ map (go gpd . snd) $ condExecutables gpd
184 else mconcat $ map (go gpd . snd) $ condTestSuites gpd
185 -- FIXME , mconcat $ map (go gpd . snd) $ condBenchmarks gpd
186 ], not $ null $ condTestSuites gpd
187 , Set.fromList $ map depName $ libExeBuildInfo gpd
188 , Set.fromList $ map depName $ testBenchBuildInfo gpd
190 , Set.fromList $ map (Executable . fst) $ condExecutables gpd
191 , fmap convertGithubUser $ listToMaybe $ catMaybes
192 $ parseGithubUserHP (homepage $ packageDescription gpd)
193 : map parseGithubUserSR (sourceRepos $ packageDescription gpd)
195 _ -> (mempty, defaultHasTestSuites, Set.empty, Set.empty, Nothing, Set.empty, Nothing)
197 libExeBuildInfo gpd = concat
198 [ maybe mempty (goBI libBuildInfo) $ condLibrary gpd
199 , concat $ map (goBI buildInfo . snd) $ condExecutables gpd
201 testBenchBuildInfo gpd = concat
204 else concat $ map (goBI testBuildInfo . snd) $ condTestSuites gpd
205 , concat $ map (goBI benchmarkBuildInfo . snd) $ condBenchmarks gpd
207 goBI f x = buildTools $ f $ condTreeData x
209 depName (Dependency (PackageName pn) _) = Executable pn
211 = Map.filterWithKey (\k _ -> not $ ignoredDep k)
212 $ Map.unionsWith unionVersionRanges
213 $ Map.fromList (map (\(Dependency pn vr) -> (pn, vr)) $ condTreeConstraints tree)
214 : map (go gpd) (mapMaybe (checkCond gpd) $ condTreeComponents tree)
216 -- Some specific overrides for cases where getting Stackage to be smart
217 -- enough to handle things would be too difficult.
218 ignoredDep :: PackageName -> Bool
220 -- The flag logic used by text-stream-decode confuses Stackage.
221 | dep == PackageName "text" && p == PackageName "text-stream-decode" = True
224 checkCond gpd (cond, tree, melse)
225 | checkCond' cond = Just tree
228 checkCond' (Var (OS os)) = os == buildOS
229 checkCond' (Var (Arch arch)) = arch == buildArch
231 -- Sigh... the small_base flag on mersenne-random-pure64 is backwards
232 checkCond' (Var (Flag (FlagName "small_base")))
233 | p == PackageName "mersenne-random-pure64" = False
235 checkCond' (Var (Flag flag@(FlagName flag'))) =
236 flag' `Set.notMember` disabledFlags settings &&
238 checkCond' (Var (Impl compiler range)) =
239 compiler == GHC && withinRange ghcVersion' range
240 checkCond' (Lit b) = b
241 checkCond' (CNot c) = not $ checkCond' c
242 checkCond' (COr c1 c2) = checkCond' c1 || checkCond' c2
243 checkCond' (CAnd c1 c2) = checkCond' c1 && checkCond' c2
245 flags' = map flagName (filter flagDefault $ genPackageFlags gpd) ++
246 (map FlagName $ Set.toList $ Stackage.Types.flags settings coreMap)
248-- | Attempt to grab the Github username from a homepage.
249parseGithubUserHP :: String -> Maybe String
250parseGithubUserHP url1 = do
251 url2 <- listToMaybe $ mapMaybe (flip stripPrefix url1)
252 [ "http://github.com/"
253 , "https://github.com/"
255 let x = takeWhile (/= '/') url2
259-- | Attempt to grab the Github username from a source repo.
260parseGithubUserSR :: SourceRepo -> Maybe String
261parseGithubUserSR sr =
262 case (repoType sr, repoLocation sr) of
263 (Just Git, Just s) -> parseGithubUserHP s
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list