<commit_before>module Main ( main ) where

import Data.Char
import Data.List
import Data.Version
import Distribution.Text
import System.Process
import Text.Regex.Posix
import Control.Exception ( assert )

type Pkg    = (String,Version,String) -- (Name, Version, Attribute)
type Pkgset = [Pkg]

comparePkgByVersion :: Pkg -> Pkg -> Ordering  -- prefers the latest version
comparePkgByVersion (n1,v1,a1) (n2,v2,a2)
  | a1 == a2    = assert (n1 == n2) (compare v2 v1)
  | otherwise   = compare a2 a1

comparePkgByName :: Pkg -> Pkg-> Ordering
comparePkgByName (n1,_,_) (n2,_,_) = compare (map toLower n1) (map toLower n2)

parseHaskellPackageName :: String -> Maybe Pkg
parseHaskellPackageName name =
  case name `regsubmatch` "^(haskellPackages[^ \t]+)[ \t]+haskell-(.*)-ghc[0-9.]+-(.*)$" of
    [attr,name',version] -> case simpleParse version of
                              Just version' -> Just (name',version',attr)
                              _             -> Nothing
    _               -> Nothing

getHaskellPackageList :: IO Pkgset
getHaskellPackageList = do
  allPkgs <- fmap lines (readProcess "bash" ["-c", "exec nix-env -qaP \\* 2>/dev/tty"] "")
  return [ p | Just p <- map parseHaskellPackageName allPkgs ]

stripProfilingVersions :: Pkgset -> Pkgset
stripProfilingVersions pkgs = [ p | p@(_,_,attr) <- pkgs , not (attr =~ "ghc[0-9.]+_profiling") ]

stripGhc721Versions :: Pkgset -> Pkgset
stripGhc721Versions pkgs = [ p | p@(_,_,attr) <- pkgs , not (attr =~ "ghc721") ]

selectLatestVersions :: Pkgset -> Pkgset
selectLatestVersions = nubBy (\x y -> comparePkgByName x y == EQ) . sortBy comparePkgByVersion

formatPackageLine :: Pkg -> String
formatPackageLine (name,version,attr) = show (name, showVersion version, Just url)
  where
    url = "http://hydra.nixos.org/job/nixpkgs/trunk/" ++ attr

regsubmatch :: String -> String -> [String]
regsubmatch buf patt = let (_,_,_,x) = f in x
  where f :: (String,String,String,[String])
        f = match (makeRegexOpts compExtended execBlank patt) buf

main :: IO ()
main = do
  pkgset <- fmap (selectLatestVersions . stripGhc721Versions . stripProfilingVersions) getHaskellPackageList
  mapM_ (putStrLn . formatPackageLine) (sortBy comparePkgByName pkgset)
<commit_msg>PackageList.hs: extended parseHaskellPackageName to recognize all "haskellPackages"<commit_after>module Main ( main ) where

import Control.Exception ( assert )
import Data.Char
import Data.List
import Data.Version
import Distribution.Package
import Distribution.Text
import System.Process
import Text.Regex.Posix

type Pkg    = (String,Version,String) -- (Name, Version, Attribute)
type Pkgset = [Pkg]

comparePkgByVersion :: Pkg -> Pkg -> Ordering  -- prefers the latest version
comparePkgByVersion (n1,v1,a1) (n2,v2,a2)
  | a1 == a2    = assert (n1 == n2) (compare v2 v1)
  | otherwise   = compare a2 a1

comparePkgByName :: Pkg -> Pkg-> Ordering
comparePkgByName (n1,_,_) (n2,_,_) = compare (map toLower n1) (map toLower n2)

parseHaskellPackageName :: String -> Maybe Pkg
parseHaskellPackageName name =
  case name `regsubmatch` "^(haskellPackages[^ \t]+)[ \t]+(.+)$" of
    [attr,name'] -> case name' `regsubmatch` "^haskell-(.+)-ghc[0-9.]+-(.+)$" of
                      [name'',version] -> case simpleParse version of
                                            Just version' -> Just (name'',version',attr)
                                            _             -> error ("cannot parse " ++ show name)
                      _                -> case simpleParse name' of
                                            Just (PackageIdentifier (PackageName n) v) -> Just (n,v,attr)
                                            _                                          -> error ("cannot parse " ++ show name)
    _            -> Nothing

getHaskellPackageList :: IO Pkgset
getHaskellPackageList = do
  allPkgs <- fmap lines (readProcess "bash" ["-c", "exec nix-env -qaP \\* 2>/dev/tty"] "")
  return [ p | Just p <- map parseHaskellPackageName allPkgs ]

stripProfilingVersions :: Pkgset -> Pkgset
stripProfilingVersions pkgs = [ p | p@(_,_,attr) <- pkgs , not (attr =~ "ghc[0-9.]+_profiling") ]

stripGhc721Versions :: Pkgset -> Pkgset
stripGhc721Versions pkgs = [ p | p@(_,_,attr) <- pkgs , not (attr =~ "ghc721") ]

selectLatestVersions :: Pkgset -> Pkgset
selectLatestVersions = nubBy (\x y -> comparePkgByName x y == EQ) . sortBy comparePkgByVersion

formatPackageLine :: Pkg -> String
formatPackageLine (name,version,attr) = show (name, showVersion version, Just url)
  where
    url = "http://hydra.nixos.org/job/nixpkgs/trunk/" ++ attr

regsubmatch :: String -> String -> [String]
regsubmatch buf patt = let (_,_,_,x) = f in x
  where f :: (String,String,String,[String])
        f = match (makeRegexOpts compExtended execBlank patt) buf

main :: IO ()
main = do
  pkgset <- fmap (selectLatestVersions . stripGhc721Versions . stripProfilingVersions) getHaskellPackageList
  mapM_ (putStrLn . formatPackageLine) (sortBy comparePkgByName pkgset)

{'fraction_non_alphanumeric': 0.12943297936285314, 'fraction_numerical': 0.014025245441795231, 'mean_word_length': 3.01609010458568, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6393513', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1614, 'n_words': 434}