From 1183f7a774d73fecd90e1481cdd97f2e41a33370 Mon Sep 17 00:00:00 2001 From: PHO Date: Thu, 1 Oct 2009 19:26:37 +0900 Subject: [PATCH] extract PackageIdentifier from Makefile's --- Main.hs | 62 ++++++++++++++++++++++++++++++++++- pkg-find-outdated-cabal.cabal | 3 +- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/Main.hs b/Main.hs index bb96adf..abbbaeb 100644 --- a/Main.hs +++ b/Main.hs @@ -1,9 +1,69 @@ module Main (main) where +import Control.Monad +import qualified Data.ByteString.Lazy as Lazy +import Data.List.Utils +import Data.Version +import Distribution.Package +import Distribution.PackageDescription +import Distribution.Query +import HSH import System.Directory +import System.FilePath +import System.FilePath.Glob +import Text.ParserCombinators.ReadP + +indexURL :: String +indexURL = "http://hackage.haskell.org/packages/archive/00-index.tar.gz" + +pkgsrcPath :: String +pkgsrcPath = "/usr/pkgsrc" -- FIXME! main :: IO () main = do appDir <- getAppUserDataDirectory "pkg-find-outdated-cabal" createDirectoryIfMissing False appDir - putStrLn appDir + let indexFile = appDir "00-index.tar.gz" + + putStrLn "Downloading the Hackage index..." + cwd <- pwd + cd appDir + runIO ("wget", ["-N", indexURL]) + cd cwd + putStrLn "Done." + + indexBin <- Lazy.readFile indexFile + let runQuery q = queryIndex q indexBin + + makefiles <- namesMatching (pkgsrcPath "*" "*" "Makefile") + scanPkgs runQuery makefiles + +scanPkgs :: (Query -> [PackageDescription]) -> [FilePath] -> IO () +scanPkgs runQuery = mapM_ scanPkg + where + scanPkg :: FilePath -> IO () + scanPkg makPath + = do isCabalPkg <- run ("fgrep", ["-q", "mk/haskell.mk", makPath]) + when isCabalPkg + $ checkPkg makPath + + checkPkg :: FilePath -> IO () + checkPkg makPath + = do mak <- readFile makPath + case grep "DISTNAME=" (lines mak) of + [l] -> do let line = (trd ' ' . trd '\t') l + distname = cut 1 '=' line + pkgName = extractPkgName distname + pkgVer = extractPkgVersion distname + pkgId = PackageIdentifier pkgName pkgVer + putStrLn (distname ++ ": " ++ show pkgId) + _ -> return () + +extractPkgName :: String -> PackageName +extractPkgName = PackageName . head . split "-" + +extractPkgVersion :: String -> Version +extractPkgVersion = take' . readP_to_S parseVersion . last . split "-" + where + take' ((v, ""):_ ) = v + take' (_ :xs) = take' xs diff --git a/pkg-find-outdated-cabal.cabal b/pkg-find-outdated-cabal.cabal index 9bb92f1..6effb39 100644 --- a/pkg-find-outdated-cabal.cabal +++ b/pkg-find-outdated-cabal.cabal @@ -18,7 +18,8 @@ Source-Repository head Executable pkg-find-outdated-cabal Build-Depends: - Cabal, HTTP, base >= 4 && < 5, directory + Cabal, FileManip, HSH, MissingH, base >= 4 && < 5, bytestring, + cabal-query, directory, filepath Main-Is: Main.hs ghc-options: -- 2.40.0