Commit 5d8469d8 authored by Andrew Gibiansky's avatar Andrew Gibiansky

fixing up interface, and using notebook directory

parent 23c9286a
...@@ -12,7 +12,7 @@ module IHaskell.IPython ( ...@@ -12,7 +12,7 @@ module IHaskell.IPython (
) where ) where
import ClassyPrelude import ClassyPrelude
import Prelude (read, reads) import Prelude (read, reads, init)
import Shelly hiding (find, trace, path) import Shelly hiding (find, trace, path)
import System.Argv0 import System.Argv0
import System.Directory import System.Directory
...@@ -22,7 +22,6 @@ import Data.String.Utils (rstrip) ...@@ -22,7 +22,6 @@ import Data.String.Utils (rstrip)
import Text.Printf import Text.Printf
import qualified System.IO.Strict as StrictIO import qualified System.IO.Strict as StrictIO
import qualified Paths_ihaskell as Paths import qualified Paths_ihaskell as Paths
import qualified Codec.Archive.Tar as Tar import qualified Codec.Archive.Tar as Tar
...@@ -35,7 +34,7 @@ ipython :: Bool -- ^ Whether to suppress output. ...@@ -35,7 +34,7 @@ ipython :: Bool -- ^ Whether to suppress output.
-> [Text] -- ^ IPython command line arguments. -> [Text] -- ^ IPython command line arguments.
-> Sh String -- ^ IPython output. -> Sh String -- ^ IPython output.
ipython suppress args = do ipython suppress args = do
(_, ipythonDir) <- ihaskellDirs (_, ipythonDir, _) <- ihaskellDirs
let ipythonPath = fromText $ ipythonDir ++ "/bin/ipython" let ipythonPath = fromText $ ipythonDir ++ "/bin/ipython"
sub $ do sub $ do
setenv "PYTHONPATH" $ ipythonDir ++ "/lib/python2.7/site-packages" setenv "PYTHONPATH" $ ipythonDir ++ "/lib/python2.7/site-packages"
...@@ -56,21 +55,23 @@ quietRun path args = runHandles path args handles nothing ...@@ -56,21 +55,23 @@ quietRun path args = runHandles path args handles nothing
nothing _ _ _ = return () nothing _ _ _ = return ()
-- | Return the data directory for IHaskell and the IPython subdirectory. -- | Return the data directory for IHaskell and the IPython subdirectory.
ihaskellDirs :: Sh (Text, Text) ihaskellDirs :: Sh (Text, Text, Text)
ihaskellDirs = do ihaskellDirs = do
home <- maybe (error "$HOME not defined.") id <$> get_env "HOME" :: Sh Text home <- maybe (error "$HOME not defined.") id <$> get_env "HOME" :: Sh Text
let ihaskellDir = home ++ "/.ihaskell" let ihaskellDir = home ++ "/.ihaskell"
ipythonDir = ihaskellDir ++ "/ipython" ipythonDir = ihaskellDir ++ "/ipython"
notebookDir = ihaskellDir ++ "/notebooks"
-- Make sure the directories exist. -- Make sure the directories exist.
mkdir_p $ fromText ipythonDir mkdir_p $ fromText ipythonDir
mkdir_p $ fromText notebookDir
return (ihaskellDir, ipythonDir) return (ihaskellDir, ipythonDir, notebookDir)
-- | Install IPython from source. -- | Install IPython from source.
installIPython :: IO () installIPython :: IO ()
installIPython = void . shellyNoDir $ do installIPython = void . shellyNoDir $ do
(ihaskellDir, ipythonDir) <- ihaskellDirs (ihaskellDir, ipythonDir, _) <- ihaskellDirs
-- Install all Python dependencies. -- Install all Python dependencies.
pipPath <- path "pip" pipPath <- path "pip"
...@@ -98,7 +99,7 @@ installIPython = void . shellyNoDir $ do ...@@ -98,7 +99,7 @@ installIPython = void . shellyNoDir $ do
-- | Check whether IPython is properly installed. -- | Check whether IPython is properly installed.
ipythonInstalled :: IO Bool ipythonInstalled :: IO Bool
ipythonInstalled = shellyNoDir $ do ipythonInstalled = shellyNoDir $ do
(_, ipythonDir) <- ihaskellDirs (_, ipythonDir, _) <- ihaskellDirs
let ipythonPath = ipythonDir ++ "/bin/ipython" let ipythonPath = ipythonDir ++ "/bin/ipython"
test_f $ fromText ipythonPath test_f $ fromText ipythonPath
...@@ -133,6 +134,10 @@ runIHaskell :: String -- ^ IHaskell profile name. ...@@ -133,6 +134,10 @@ runIHaskell :: String -- ^ IHaskell profile name.
-> [String] -- ^ Arguments to IPython. -> [String] -- ^ Arguments to IPython.
-> IO () -> IO ()
runIHaskell profile app args = void . shellyNoDir $ do runIHaskell profile app args = void . shellyNoDir $ do
-- Switch to our directory.
(_, _, notebookDir) <- ihaskellDirs
cd $ fromText notebookDir
-- Try to locate the profile. Do not die if it doesn't exist. -- Try to locate the profile. Do not die if it doesn't exist.
errExit False $ ipython True ["locate", "profile", pack profile] errExit False $ ipython True ["locate", "profile", pack profile]
...@@ -164,6 +169,17 @@ setupIPythonProfile profile = shellyNoDir $ do ...@@ -164,6 +169,17 @@ setupIPythonProfile profile = shellyNoDir $ do
copyProfile :: Text -> IO () copyProfile :: Text -> IO ()
copyProfile profileDir = do copyProfile profileDir = do
profileTar <- Paths.getDataFileName "profile/profile.tar" profileTar <- Paths.getDataFileName "profile/profile.tar"
{-
-- Load profile from Resources directory of Mac *.app.
ihaskellPath <- shellyNoDir getIHaskellPath
profileTar <- if "IHaskell.app/Contents/MacOS" `isInfixOf` ihaskellPath
then
let pieces = split "/" ihaskellPath
pathPieces = init pieces ++ ["..", "Resources", "profile.tar"] in
return $ intercalate "/" pathPieces
else Paths.getDataFileName "profile/profile.tar"
-}
putStrLn $ pack $ "Loading profile from " ++ profileTar
Tar.extract (unpack profileDir) profileTar Tar.extract (unpack profileDir) profileTar
-- | Insert the IHaskell path into the IPython configuration. -- | Insert the IHaskell path into the IPython configuration.
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
module Main where module Main where
import ClassyPrelude hiding (liftIO) import ClassyPrelude hiding (liftIO)
import Control.Concurrent.Chan import Control.Concurrent.Chan
import Control.Concurrent (threadDelay)
import Data.Aeson import Data.Aeson
import Text.Printf import Text.Printf
import System.Exit (exitSuccess) import System.Exit (exitSuccess)
...@@ -31,10 +32,13 @@ data KernelState = KernelState ...@@ -31,10 +32,13 @@ data KernelState = KernelState
main :: IO () main :: IO ()
main = do main = do
args <- map unpack <$> getArgs
ihaskell args
ihaskell args = do
installed <- ipythonInstalled installed <- ipythonInstalled
unless installed installIPython unless installed installIPython
args <- map unpack <$> getArgs
case args of case args of
-- Create the "haskell" profile. -- Create the "haskell" profile.
["setup"] -> setupIPythonProfile "haskell" ["setup"] -> setupIPythonProfile "haskell"
...@@ -47,8 +51,16 @@ main = do ...@@ -47,8 +51,16 @@ main = do
["kernel", profileSrc] -> kernel profileSrc ["kernel", profileSrc] -> kernel profileSrc
-- Bad arguments. -- Bad arguments.
[] -> putStrLn $ "Provide command to run ('setup', 'kernel <profile-file.json>', " ++ [] -> do
"'notebook [args]', 'console [args]')." mapM_ putStrLn [
"Available Commands:",
" `IHaskell console` - run command-line console.",
" `IHaskell setup` - repeat setup.",
" `IHaskell notebook` - run browser-based notebook.",
" `IHaskell kernel <file>` - just run the kernel.",
"Defaulting to `IHaskell notebook.`"]
threadDelay $ 2 * 1000 * 1000
ihaskell ["notebook"]
cmd:_ -> putStrLn $ "Unknown command: " ++ pack cmd cmd:_ -> putStrLn $ "Unknown command: " ++ pack cmd
......
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