Commit 10002807 authored by Andrew Gibiansky's avatar Andrew Gibiansky

Using `ipython locate` to find ipython profile directory.

parent 722c46f9
...@@ -6,11 +6,11 @@ module IHaskell.IPython ( ...@@ -6,11 +6,11 @@ module IHaskell.IPython (
import ClassyPrelude import ClassyPrelude
import Shelly hiding (find, trace) import Shelly hiding (find, trace)
import Text.Printf
import System.Argv0 import System.Argv0
import System.Directory import System.Directory
import qualified Filesystem.Path.CurrentOS as FS import qualified Filesystem.Path.CurrentOS as FS
import Data.List.Utils (split) import Data.List.Utils (split)
import Data.String.Utils (rstrip)
import Prelude (read) import Prelude (read)
import qualified System.IO.Strict as StrictIO import qualified System.IO.Strict as StrictIO
...@@ -18,61 +18,47 @@ import qualified System.IO.Strict as StrictIO ...@@ -18,61 +18,47 @@ import qualified System.IO.Strict as StrictIO
import qualified IHaskell.Config as Config import qualified IHaskell.Config as Config
-- | Run IPython with any arguments. -- | Run IPython with any arguments.
ipython :: [Text] -> Sh () ipython :: Bool -- ^ Whether to suppress output.
ipython args = do -> [Text] -- ^ IPython command line arguments.
-> Sh String -- ^ IPython output.
ipython suppress args = do
path <- which "ipython" path <- which "ipython"
case path of case path of
Nothing -> putStrLn "Could not find `ipython` executable." Nothing -> do
Just ipythonPath -> runHandles ipythonPath args inheritHandles doNothing putStrLn "Could not find `ipython` executable."
where inheritHandles = [InHandle Inherit, OutHandle Inherit, ErrorHandle Inherit] fail "`ipython` not on $PATH."
doNothing _ _ _ = return () Just ipythonPath -> runHandles ipythonPath args handles doNothing
where handles = [InHandle Inherit, outHandle suppress, ErrorHandle Inherit]
outHandle True = OutHandle CreatePipe
outHandle False = OutHandle Inherit
doNothing _ stdout _ = if suppress
then liftIO $ StrictIO.hGetContents stdout
else return ""
-- | Use the `ipython --version` command to figure out the version. -- | Use the `ipython --version` command to figure out the version.
-- Return a tuple with (major, minor, patch). -- Return a tuple with (major, minor, patch).
ipythonVersion :: IO (Int, Int, Int) ipythonVersion :: IO (Int, Int, Int)
ipythonVersion = shelly $ do ipythonVersion = shelly $ do
path <- which "ipython" [major, minor, patch] <- map read <$> split "." <$> ipython True ["--version"]
case path of return (major, minor, patch)
Nothing -> error "Could not find `ipython` executable."
Just path -> do
[major, minor, patch] <- map read <$> split "." <$> runHandle path ["--version"] (liftIO . StrictIO.hGetContents) :: Sh [Int]
return (major, minor, patch)
-- | Run an IHaskell application using the given profile. -- | Run an IHaskell application using the given profile.
runIHaskell :: String -- ^ IHaskell profile name. runIHaskell :: String -- ^ IHaskell profile name.
-> String -- ^ IPython app name. -> String -- ^ IPython app name.
-> [String] -- ^ Arguments to IPython. -> [String] -- ^ Arguments to IPython.
-> IO () -> IO ()
runIHaskell profile app args = shelly . ipython $ [pack app, "--profile", pack profile] ++ map pack args runIHaskell profile app args = void . shelly . ipython False $ [pack app, "--profile", pack profile] ++ map pack args
-- | Create the IPython profile. -- | Create the IPython profile.
setupIPythonProfile :: String -- ^ IHaskell profile name. setupIPythonProfile :: String -- ^ IHaskell profile name.
-> IO () -> IO ()
setupIPythonProfile profile = shelly $ do setupIPythonProfile profile = shelly $ do
-- Create the IPython profile. -- Create the IPython profile.
ipython ["profile", "create", pack profile] void $ ipython False ["profile", "create", pack profile]
-- Find the IPython profile directory.
ipythonDirs <- catMaybes <$> sequence [get_env "IPYTHON_DIR", get_env "IPYTHONDIR"]
ipythonDir <- case ipythonDirs of
dir:_ -> return dir
[] -> do
home <- get_env "HOME"
case home of
Nothing -> error "Could not locate $HOME."
Just home -> do
dotIpython <- test_d . fromText $ home ++ "/.ipython"
dotConfigIpython <- test_d . fromText $ home ++ "/.config/ipython"
when (not dotIpython && not dotConfigIpython) $ do
putStrLn "Could not find ~/.ipython or ~/.config/ipython."
error "Could not find IPython directory."
return $ home ++ (if dotIpython
then "/.ipython"
else "/.config/ipython")
-- Find the IPython profile directory. Make sure to get rid of trailing
-- newlines from the output of the `ipython locate` call.
ipythonDir <- pack <$> rstrip <$> ipython True ["locate"]
let profileDir = ipythonDir ++ "/profile_" ++ pack profile ++ "/" let profileDir = ipythonDir ++ "/profile_" ++ pack profile ++ "/"
path <- getIHaskellPath path <- getIHaskellPath
...@@ -87,6 +73,9 @@ writeConfigFilesTo profileDir ihaskellPath = do ...@@ -87,6 +73,9 @@ writeConfigFilesTo profileDir ihaskellPath = do
writeFile (conf "ipython_notebook_config.py") Config.notebook writeFile (conf "ipython_notebook_config.py") Config.notebook
writeFile (conf "ipython_console_config.py") Config.console writeFile (conf "ipython_console_config.py") Config.console
writeFile (conf "ipython_qtconsole_config.py") Config.qtconsole writeFile (conf "ipython_qtconsole_config.py") Config.qtconsole
-- The custom directory many not exist, in which case we'll create it.
mkdir_p (conf "static/custom/")
writeFile (conf "static/custom/custom.js") Config.customjs writeFile (conf "static/custom/custom.js") Config.customjs
where where
conf filename = fromText $ profileDir ++ filename conf filename = fromText $ profileDir ++ filename
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment