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 exists <- doesFileExist tarball
104 lbs <- L.readFile tarball
105 findCabalAndAddPackage tarball p v pdb $ Tar.read $ GZip.decompress lbs
107 case Tar.entryContent e of
108 Tar.NormalFile bs _ -> addPackage p v bs pdb
111 addTarball :: PackageDB -> FilePath -> IO PackageDB
112 addTarball pdb tarball' = do
113 lbs <- L.readFile tarball
114 let (v', p') = break (== '-') $ reverse tarball'
115 p = PackageName $ reverse $ drop 1 p'
116 v <- maybe (error $ "Invalid tarball name: " ++ tarball) return
117 $ simpleParse $ reverse v'
118 findCabalAndAddPackage tarball p v pdb $ Tar.read $ GZip.decompress lbs
120 tarball = selectTarballDir settings </> tarball' <.> "tar.gz"
122 excludeUnderlay :: PackageDB -> PackageDB
123 excludeUnderlay (PackageDB pdb) =
124 PackageDB $ Map.filterWithKey (\k _ -> Set.notMember k underlay) pdb
126 skipTests p = p `Set.member` skippedTests settings
128 -- Find the relevant cabal file in the given entries and add its contents
129 -- to the package database
130 findCabalAndAddPackage tarball p v pdb =
136 expectedPath = let PackageName p' = p in concat
137 [ packageVersionString (p, v)
142 loop Tar.Done = error $ concat
143 [ "Missing cabal file "
148 loop (Tar.Fail e) = error $ concat
149 [ "Unable to read tarball "
154 loop (Tar.Next entry rest)
155 | map fixPath (Tar.entryPath entry) == expectedPath =
156 case Tar.entryContent entry of
157 Tar.NormalFile bs _ -> addPackage p v bs pdb
163 , " was not a normal file"
165 | otherwise = loop rest
167 addPackage p v lbs pdb = do
168 let (deps', hasTests, buildToolsExe', buildToolsOther', mgpd, execs, mgithub) = parseDeps p lbs
169 return $ mappend pdb $ PackageDB $ Map.singleton p PackageInfo
172 , piHasTests = hasTests
173 , piBuildToolsExe = buildToolsExe'
174 , piBuildToolsAll = buildToolsExe' `Set.union` buildToolsOther'
177 , piGithubUser = fromMaybe [] mgithub
181 case parsePackageDescription $ L8.unpack lbs of
182 ParseOk _ gpd -> (mconcat
183 [ maybe mempty (go gpd) $ condLibrary gpd
184 , mconcat $ map (go gpd . snd) $ condExecutables gpd
187 else mconcat $ map (go gpd . snd) $ condTestSuites gpd
188 -- FIXME , mconcat $ map (go gpd . snd) $ condBenchmarks gpd
189 ], not $ null $ condTestSuites gpd
190 , Set.fromList $ map depName $ libExeBuildInfo gpd
191 , Set.fromList $ map depName $ testBenchBuildInfo gpd
193 , Set.fromList $ map (Executable . fst) $ condExecutables gpd
194 , fmap convertGithubUser $ listToMaybe $ catMaybes
195 $ parseGithubUserHP (homepage $ packageDescription gpd)
196 : map parseGithubUserSR (sourceRepos $ packageDescription gpd)
198 _ -> (mempty, defaultHasTestSuites, Set.empty, Set.empty, Nothing, Set.empty, Nothing)
200 libExeBuildInfo gpd = concat
201 [ maybe mempty (goBI libBuildInfo) $ condLibrary gpd
202 , concat $ map (goBI buildInfo . snd) $ condExecutables gpd
204 testBenchBuildInfo gpd = concat
207 else concat $ map (goBI testBuildInfo . snd) $ condTestSuites gpd
208 , concat $ map (goBI benchmarkBuildInfo . snd) $ condBenchmarks gpd
210 goBI f x = buildTools $ f $ condTreeData x
212 depName (Dependency (PackageName pn) _) = Executable pn
214 = Map.filterWithKey (\k _ -> not $ ignoredDep k)
215 $ Map.unionsWith unionVersionRanges
216 $ Map.fromList (map (\(Dependency pn vr) -> (pn, vr)) $ condTreeConstraints tree)
217 : map (go gpd) (mapMaybe (checkCond gpd) $ condTreeComponents tree)
219 -- Some specific overrides for cases where getting Stackage to be smart
220 -- enough to handle things would be too difficult.
221 ignoredDep :: PackageName -> Bool
223 -- The flag logic used by text-stream-decode confuses Stackage.
224 | dep == PackageName "text" && p == PackageName "text-stream-decode" = True
227 checkCond gpd (cond, tree, melse)
228 | checkCond' cond = Just tree
231 checkCond' (Var (OS os)) = os == buildOS
232 checkCond' (Var (Arch arch)) = arch == buildArch
234 -- Sigh... the small_base flag on mersenne-random-pure64 is backwards
235 checkCond' (Var (Flag (FlagName "small_base")))
236 | p == PackageName "mersenne-random-pure64" = False
238 checkCond' (Var (Flag flag@(FlagName flag'))) =
239 flag' `Set.notMember` disabledFlags settings &&
241 checkCond' (Var (Impl compiler range)) =
242 compiler == GHC && withinRange ghcVersion' range
243 checkCond' (Lit b) = b
244 checkCond' (CNot c) = not $ checkCond' c
245 checkCond' (COr c1 c2) = checkCond' c1 || checkCond' c2
246 checkCond' (CAnd c1 c2) = checkCond' c1 && checkCond' c2
248 flags' = map flagName (filter flagDefault $ genPackageFlags gpd) ++
249 (map FlagName $ Set.toList $ Stackage.Types.flags settings coreMap)
251-- | Attempt to grab the Github username from a homepage.
252parseGithubUserHP :: String -> Maybe String
253parseGithubUserHP url1 = do
254 url2 <- listToMaybe $ mapMaybe (flip stripPrefix url1)
255 [ "http://github.com/"
256 , "https://github.com/"
258 let x = takeWhile (/= '/') url2
262-- | Attempt to grab the Github username from a source repo.
263parseGithubUserSR :: SourceRepo -> Maybe String
264parseGithubUserSR sr =
265 case (repoType sr, repoLocation sr) of
266 (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