<commit_before>-- | This scripts parses cabal files of different raaz packages and
-- installs all the packages in linear fashion by resolving
-- dependencies programmatically.

import           Control.Applicative
import           Data.List
import qualified Data.Map                              as Map
import           Data.Maybe
import qualified Data.Set                              as Set
import           Distribution.Package
import           Distribution.PackageDescription
import           Distribution.PackageDescription.Parse
import           Distribution.Text
import           Distribution.Verbosity
import           Distribution.Version
import           System.Environment
import           System.Exit
import           System.Directory
import           System.Process

-- | All the packages to be installed.
allPackages :: [PackageName]
allPackages = [ PackageName "raaz"
              , PackageName "raaz-benchmarks"
              , PackageName "raaz-cipher"
              , PackageName "raaz-core"
              , PackageName "raaz-hash"
              , PackageName "raaz-modular"
              , PackageName "raaz-random"
              , PackageName "raaz-ssh"
              ]

-- | Travis Environment given by HASKELL_PLATFORM and PARALLEL_BUILDS
-- environment variables set alongwith their corresponding
-- constraints.
data TravisEnv = TravisEnv { haskellPlatform    :: Maybe String
                           , parallelBuilds     :: Bool
                           , installConstraints :: [Dependency]
                           , verboseConstraints :: [String]
                           } deriving (Eq, Show)

-- | The main routine which gets all the parsed results from cabal
-- files of packages and then resolve dependencies to get a linear
-- list of packages which is then installed.
main :: IO ()
main = do allgpds   <- getAllGPD allPackages
          travisEnv <- getTravisEnv
          args      <- getArgs
          case args of
            [cmd]     -> sequence_ $ map (makePackage travisEnv cmd)
                                         (resolve allgpds)
            otherwise -> error "Invalid argument provided."
  where resolve = resolveDependency . getPackageDepency allPackages

-- | Build a single package with a command line argument.
makePackage :: TravisEnv -> String -> PackageName -> IO ()
makePackage travisEnv cmd package =
  do setCurrentDirectory $ "./" ++ getPackageName package
     exitCode <- case cmd of
       "install" -> sequence_
                    [ cabalCmd
                        ([ "install"
                         , "--only-dependencies"
                         ] ++ verboseConstraints travisEnv)
                        "Installing Dependencies of"
                    , cabalCmd
                        [ "install"
                        , "--enable-documentation"
                        ]
                        "Installing"
                    ]
       "config"  -> cabalCmd ["configure", "--enable-tests"] "Configuring"
       "build"   -> cabalCmd ["build"] "Building"
       "test"    -> cabalCmd ["test", "--show-details=failures"] "Testing"
       "tarball" -> cabalCmd ["sdist"] "Creating Source tarball of"
       otherwise -> error "Invalid argument provided."
     setCurrentDirectory "../"
     return exitCode
  where cabalCmd = makeCommand package "cabal"

-- | To execute commands with arguments on packages.
makeCommand :: PackageName -> String -> [String] -> String -> IO ()
makeCommand package command args msg =
  do putStrLn startMsg
     exitCode <- rawSystem command args
     case exitCode of ExitSuccess -> putStrLn doneMsg
                      otherwise   -> error failMsg
  where startMsg = msg ++ " **" ++ getPackageName package ++ "** started."
        doneMsg  = msg ++ " **" ++ getPackageName package ++ " ** done."
        failMsg  = msg ++ " failed for **" ++ getPackageName package ++ "** ."

-- | Results of parsing cabal file of all packages.
getAllGPD :: [PackageName] -> IO [GenericPackageDescription]
getAllGPD = sequence . map mapFn
  where mapFn package = parseCabal $ "./" ++ getPackageName package ++ "/"
                                          ++ getPackageName package ++ ".cabal"

---------------------- Helper functions ------------------------------

-- | Get Travis Environment.
getTravisEnv :: IO TravisEnv
getTravisEnv =
  do env <- getEnvironment
     hp  <- return . lookup "HASKELL_PLATFORM" $ env
     pb  <- return . lookup "PARALLEL_BUILDS" $ env
     ct  <- maybe (return []) getConstraint hp
     return TravisEnv { haskellPlatform    = hp
                      , parallelBuilds     = paraBuilds pb
                      , installConstraints = ct
                      , verboseConstraints = getVerbose ct (paraBuilds pb)
                      }
  where paraBuilds       = maybe False (\opt -> if opt == "yes" then True
                                                                else False)
        getConstraint pf = (parseCabal $ "./platform/cabal/" ++ pf ++ ".cabal")
                       >>= (return . getDependencies)
        getVerbose ct pb = map mapFn ct ++ (if pb then ["-j"] else [])
          where mapFn (Dependency pn vr) = "--constraint="<commit_msg>refactored cabal path code<commit_after>-- | This scripts parses cabal files of different raaz packages and
-- installs all the packages in linear fashion by resolving
-- dependencies programmatically.

import           Control.Applicative
import           Data.List
import qualified Data.Map                              as Map
import           Data.Maybe
import qualified Data.Set                              as Set
import           Distribution.Package
import           Distribution.PackageDescription
import           Distribution.PackageDescription.Parse
import           Distribution.Text
import           Distribution.Verbosity
import           Distribution.Version
import           System.Environment
import           System.Exit
import           System.FilePath
import           System.Directory
import           System.Process

-------------------- Configuration ------------------------

-- | All the packages to be installed.
allPackages :: [PackageName]
allPackages = [ PackageName "raaz"
              , PackageName "raaz-benchmarks"
              , PackageName "raaz-cipher"
              , PackageName "raaz-core"
              , PackageName "raaz-hash"
              , PackageName "raaz-modular"
              , PackageName "raaz-random"
              , PackageName "raaz-ssh"
              ]


-- | Relative path to the platform cabal file
platformCabal :: String -> FilePath
platformCabal p = "platform" </> "cabal" </> p <.> "cabal"

-- | Relative path to the package cabal file
packageCabal  :: String -> FilePath
packageCabal pkg = pkg </> pkg <.> "cabal"


---------------------------------------------------------------------

-- | Travis Environment given by HASKELL_PLATFORM and PARALLEL_BUILDS
-- environment variables set alongwith their corresponding
-- constraints.
data TravisEnv = TravisEnv { haskellPlatform    :: Maybe String
                           , parallelBuilds     :: Bool
                           , installConstraints :: [Dependency]
                           , verboseConstraints :: [String]
                           } deriving (Eq, Show)

-- | The main routine which gets all the parsed results from cabal
-- files of packages and then resolve dependencies to get a linear
-- list of packages which is then installed.
main :: IO ()
main = do allgpds   <- getAllGPD allPackages
          travisEnv <- getTravisEnv
          args      <- getArgs
          case args of
            [cmd]     -> sequence_ $ map (makePackage travisEnv cmd)
                                         (resolve allgpds)
            otherwise -> error "Invalid argument provided."
  where resolve = resolveDependency . getPackageDepency allPackages

-- | Build a single package with a command line argument.
makePackage :: TravisEnv -> String -> PackageName -> IO ()
makePackage travisEnv cmd package =
  do setCurrentDirectory $ "./" ++ getPackageName package
     exitCode <- case cmd of
       "install" -> sequence_
                    [ cabalCmd
                        ([ "install"
                         , "--only-dependencies"
                         ] ++ verboseConstraints travisEnv)
                        "Installing Dependencies of"
                    , cabalCmd
                        [ "install"
                        , "--enable-documentation"
                        ]
                        "Installing"
                    ]
       "config"  -> cabalCmd ["configure", "--enable-tests"] "Configuring"
       "build"   -> cabalCmd ["build"] "Building"
       "test"    -> cabalCmd ["test", "--show-details=failures"] "Testing"
       "tarball" -> cabalCmd ["sdist"] "Creating Source tarball of"
       otherwise -> error "Invalid argument provided."
     setCurrentDirectory "../"
     return exitCode
  where cabalCmd = makeCommand package "cabal"

-- | To execute commands with arguments on packages.
makeCommand :: PackageName -> String -> [String] -> String -> IO ()
makeCommand package command args msg =
  do putStrLn startMsg
     exitCode <- rawSystem command args
     case exitCode of ExitSuccess -> putStrLn doneMsg
                      otherwise   -> error failMsg
  where startMsg = msg ++ " **" ++ getPackageName package ++ "** started."
        doneMsg  = msg ++ " **" ++ getPackageName package ++ " ** done."
        failMsg  = msg ++ " failed for **" ++ getPackageName package ++ "** ."

-- | Results of parsing cabal file of all packages.
getAllGPD :: [PackageName] -> IO [GenericPackageDescription]
getAllGPD = mapM mapFn
  where mapFn pkg = parseCabal $ packageCabal $ getPackageName pkg

---------------------- Helper functions ------------------------------

-- | Get Travis Environment.
getTravisEnv :: IO TravisEnv
getTravisEnv =
  do env <- getEnvironment
     hp  <- return . lookup "HASKELL_PLATFORM" $ env
     pb  <- return . lookup "PARALLEL_BUILDS" $ env
     ct  <- maybe (return []) getConstraint hp
     return TravisEnv { haskellPlatform    = hp
                      , parallelBuilds     = paraBuilds pb
                      , installConstraints = ct
                      , verboseConstraints = getVerbose ct (paraBuilds pb)
                      }
  where paraBuilds       = maybe False (\opt -> if opt == "yes" then True
                                                                else False)
        getConstraint pf = (parseCabal $ "./platform/cabal/" ++ pf ++ ".cabal")
                       >>= (return . getDependencies)
        getVerbose ct pb = map mapFn ct ++ (if pb then ["-j"] else [])
          where mapFn (Dependency pn vr) = "--constraint="
{'fraction_non_alphanumeric': 0.10681416751882496, 'fraction_numerical': 0.0, 'mean_word_length': 1.682793017456359, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 52, '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': '6239001', 'n_tokens_mistral': 2638, 'n_tokens_neox': 2538, 'n_words': 835}