<commit_before>{-# LANGUAGE NoImplicitPrelude, OverloadedStrings #-}
{-# LANGUAGE DoAndIfThenElse #-}
-- | Description : Shell scripting wrapper using @Shelly@ for the @notebook@, and
--                 @console@ commands.
module IHaskell.IPython (
  setupIPython,
  runConsole,
  runNotebook,
  readInitInfo,
  defaultConfFile,
  getIHaskellDir,
  getSandboxPackageConf,
  nbconvert,
  subHome,
  ViewFormat(..),
  WhichIPython(..),
) where

import            ClassyPrelude
import            Control.Concurrent (threadDelay)
import            Prelude (read, reads, init)
import            Shelly hiding (find, trace, path, (</>))
import            System.Argv0
import            System.Directory
import qualified  Filesystem.Path.CurrentOS as FS
import            Data.List.Utils (split)
import            Data.String.Utils (rstrip, endswith, strip, replace)
import            Text.Printf

import qualified System.IO.Strict as StrictIO
import qualified Paths_ihaskell as Paths
import qualified Codec.Archive.Tar as Tar

import IHaskell.Types

-- | Which IPython to use.
data WhichIPython
     = DefaultIPython           -- ^ Use the one that IHaskell tries to install.
     | ExplicitIPython String   -- ^ Use the command-line flag provided one.
     deriving Eq

-- | Which commit of IPython we are on.
ipythonCommit :: Text
ipythonCommit = "4a08574f74201ef7ca39a4e80d19a2d158ab6950"

-- | The IPython profile name.
ipythonProfile :: String
ipythonProfile = "haskell"

-- | Run IPython with any arguments.
ipython :: WhichIPython -- ^ Which IPython to use (user-provided or IHaskell-installed).
        -> Bool         -- ^ Whether to suppress output.
        -> [Text]       -- ^ IPython command line arguments.
        -> Sh String    -- ^ IPython output.
ipython which suppress args
  | which == DefaultIPython = do
      runCmd <- liftIO $ Paths.getDataFileName "installation/run.sh"
      venv <- fpToText <$> ipythonDir
      let cmdArgs = [pack runCmd, venv] ++ args
      -- We have this because `silently` in shelly < 1.4 does not silence
      -- stderr. In shelly 1.4, however, using `run` does not let us use stdin,
      -- and the current code breaks for unknown reasons. When the bug
      --      https://github.com/yesodweb/Shelly.hs/issues/54
      -- is closed, we should edit ihaskell.cabal to allow shelly 1.4.
      runHandles "bash" cmdArgs handles doNothing
  | otherwise = do
      let ExplicitIPython exe = which
      runHandles (fpFromString exe) args handles doNothing

  where handles = [InHandle Inherit, outHandle suppress, errorHandle suppress]
        outHandle True = OutHandle CreatePipe
        outHandle False = OutHandle Inherit
        errorHandle True =  ErrorHandle CreatePipe
        errorHandle False = ErrorHandle Inherit<commit_msg>Fixing #211<commit_after>{-# LANGUAGE NoImplicitPrelude, OverloadedStrings #-}
{-# LANGUAGE DoAndIfThenElse #-}
-- | Description : Shell scripting wrapper using @Shelly@ for the @notebook@, and
--                 @console@ commands.
module IHaskell.IPython (
  setupIPython,
  runConsole,
  runNotebook,
  readInitInfo,
  defaultConfFile,
  getIHaskellDir,
  getSandboxPackageConf,
  nbconvert,
  subHome,
  ViewFormat(..),
  WhichIPython(..),
) where

import            ClassyPrelude
import            Control.Concurrent (threadDelay)
import            Prelude (read, reads, init)
import            Shelly hiding (find, trace, path, (</>))
import            System.Argv0
import            System.Directory
import qualified  Filesystem.Path.CurrentOS as FS
import            Data.List.Utils (split)
import            Data.String.Utils (rstrip, endswith, strip, replace)
import            Text.Printf

import qualified System.IO.Strict as StrictIO
import qualified Paths_ihaskell as Paths
import qualified Codec.Archive.Tar as Tar

import IHaskell.Types

-- | Which IPython to use.
data WhichIPython
     = DefaultIPython           -- ^ Use the one that IHaskell tries to install.
     | ExplicitIPython String   -- ^ Use the command-line flag provided one.
     deriving Eq

-- | Which commit of IPython we are on.
ipythonCommit :: Text
ipythonCommit = "4a08574f74201ef7ca39a4e80d19a2d158ab6950"

-- | The IPython profile name.
ipythonProfile :: String
ipythonProfile = "haskell"

-- | Run IPython with any arguments.
ipython :: WhichIPython -- ^ Which IPython to use (user-provided or IHaskell-installed).
        -> Bool         -- ^ Whether to suppress output.
        -> [Text]       -- ^ IPython command line arguments.
        -> Sh String    -- ^ IPython output.
ipython which suppress args
  | which == DefaultIPython = do
      runCmd <- liftIO $ Paths.getDataFileName "installation/run.sh"
      venv <- fpToText <$> ipythonDir
      let cmdArgs = [pack runCmd, venv] ++ args
      -- If we have PYTHONDONTWRITEBYTECODE enabled, everything breaks.
      setenv "PYTHONDONTWRITEBYTECODE" ""

      -- We have this because `silently` in shelly < 1.4 does not silence
      -- stderr. In shelly 1.4, however, using `run` does not let us use stdin,
      -- and the current code breaks for unknown reasons. When the bug
      --      https://github.com/yesodweb/Shelly.hs/issues/54
      -- is closed, we should edit ihaskell.cabal to allow shelly 1.4.
      runHandles "bash" cmdArgs handles doNothing
  | otherwise = do
      let ExplicitIPython exe = which
      runHandles (fpFromString exe) args handles doNothing

  where handles = [InHandle Inherit, outHandle suppress, errorHandle suppress]
        outHandle True = OutHandle CreatePipe
        outHandle False = OutHandle Inherit
        errorHandle True =  ErrorHandle CreatePipe
        errorHandle False = ErrorHandle Inherit
{'fraction_non_alphanumeric': 0.08302485457429931, 'fraction_numerical': 0.013220518244315178, 'mean_word_length': 2.730440499671269, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7356185', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1716, 'n_words': 537}