module Debian.Apt.Package where
import qualified Data.Map as Map
import Debian.Version
import Debian.Relation
type PackageNameMap a = Map.Map BinPkgName [a]
packageNameMap :: (a -> BinPkgName) -> [a] -> PackageNameMap a
packageNameMap getName packages = foldl (\m p -> Map.insertWith (++) (getName p) [p] m) Map.empty packages
addProvides :: (p -> [BinPkgName]) -> [p] -> PackageNameMap p -> PackageNameMap p
addProvides providesf ps pnm =
let provides = findProvides providesf ps in
foldl (\m (packageName, package) -> Map.insertWith (flip (++)) packageName [package] m) pnm provides
findProvides :: forall p. (p -> [BinPkgName]) -> [p] -> [(BinPkgName, p)]
findProvides providesf packages = foldl addProvides' [] packages
where addProvides' :: [(BinPkgName, p)] -> p -> [(BinPkgName, p)]
addProvides' providesList package =
foldl (\pl pkgName -> (pkgName, package): pl) providesList (providesf package)
lookupPackageByRel :: PackageNameMap a -> (a -> (BinPkgName, DebianVersion)) -> Relation -> [a]
lookupPackageByRel pm packageVersionF (Rel pkgName mVerReq _mArch) =
case Map.lookup pkgName pm of
Nothing -> []
Just packages -> filter filterVer packages
where filterVer p =
case mVerReq of
Nothing -> True
Just _verReq ->
let (pName, pVersion) = packageVersionF p
in if pName /= pkgName
then False
else checkVersionReq mVerReq (Just pVersion)