Commit 3a339bc6 authored by Andrew Gibiansky's avatar Andrew Gibiansky

Adding proper command-line flags!

parent 5d8469d8
...@@ -7,3 +7,4 @@ env ...@@ -7,3 +7,4 @@ env
.ihaskell_capture .ihaskell_capture
.ipynb_checkpoints .ipynb_checkpoints
Hspec Hspec
todo
...@@ -47,6 +47,7 @@ data-files: ...@@ -47,6 +47,7 @@ data-files:
library library
build-depends: base ==4.6.*, build-depends: base ==4.6.*,
cmdargs >= 0.10,
tar, tar,
ghc-parser, ghc-parser,
unix >= 2.6, unix >= 2.6,
...@@ -103,6 +104,7 @@ executable IHaskell ...@@ -103,6 +104,7 @@ executable IHaskell
-- Other library packages from which modules are imported. -- Other library packages from which modules are imported.
build-depends: base ==4.6.*, build-depends: base ==4.6.*,
cmdargs >= 0.10,
tar, tar,
ghc-parser, ghc-parser,
unix >= 2.6, unix >= 2.6,
...@@ -135,6 +137,7 @@ Test-Suite hspec ...@@ -135,6 +137,7 @@ Test-Suite hspec
Ghc-Options: -threaded Ghc-Options: -threaded
Main-Is: Hspec.hs Main-Is: Hspec.hs
build-depends: base ==4.6.*, build-depends: base ==4.6.*,
cmdargs >= 0.10,
tar, tar,
ghc-parser, ghc-parser,
unix >= 2.6, unix >= 2.6,
......
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
-- | Description : Shell scripting wrapper using @Shelly@ for the @notebook@, @setup@, and -- | Description : Shell scripting wrapper using @Shelly@ for the @notebook@, @setup@, and
-- @console@ commands. -- @console@ commands.
module IHaskell.IPython ( module IHaskell.IPython (
runIHaskell,
setupIPythonProfile,
ipythonVersion,
parseVersion,
ipythonInstalled, ipythonInstalled,
installIPython installIPython,
removeIPython,
runConsole,
runNotebook,
readInitInfo,
defaultConfFile,
) where ) where
import ClassyPrelude import ClassyPrelude
...@@ -25,10 +26,16 @@ import qualified System.IO.Strict as StrictIO ...@@ -25,10 +26,16 @@ 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
import IHaskell.Types
-- | Which commit of IPython we are on. -- | Which commit of IPython we are on.
ipythonCommit :: Text ipythonCommit :: Text
ipythonCommit = "1faf2f6e77fa31f4533e3edbe101c38ddf8943d8" ipythonCommit = "1faf2f6e77fa31f4533e3edbe101c38ddf8943d8"
-- | The IPython profile name.
ipythonProfile :: String
ipythonProfile = "haskell"
-- | Run IPython with any arguments. -- | Run IPython with any arguments.
ipython :: Bool -- ^ Whether to suppress output. ipython :: Bool -- ^ Whether to suppress output.
-> [Text] -- ^ IPython command line arguments. -> [Text] -- ^ IPython command line arguments.
...@@ -68,6 +75,22 @@ ihaskellDirs = do ...@@ -68,6 +75,22 @@ ihaskellDirs = do
return (ihaskellDir, ipythonDir, notebookDir) return (ihaskellDir, ipythonDir, notebookDir)
defaultConfFile :: IO (Maybe String)
defaultConfFile = shellyNoDir $ do
(ihaskellDir, _, _) <- ihaskellDirs
let filename = ihaskellDir ++ "/rc.hs"
exists <- test_f $ fromText filename
return $ if exists
then Just $ unpack filename
else Nothing
-- | Remove IPython so it can be reinstalled.
removeIPython :: IO ()
removeIPython = void . shellyNoDir $ do
(ihaskellDir, _, _) <- ihaskellDirs
cd $ fromText ihaskellDir
rm_rf "ipython-src"
-- | Install IPython from source. -- | Install IPython from source.
installIPython :: IO () installIPython :: IO ()
installIPython = void . shellyNoDir $ do installIPython = void . shellyNoDir $ do
...@@ -132,12 +155,8 @@ parseVersion versionStr = map read' $ split "." versionStr ...@@ -132,12 +155,8 @@ parseVersion versionStr = map read' $ split "." versionStr
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 () -> Sh ()
runIHaskell profile app args = void . shellyNoDir $ do runIHaskell profile app args = void $ 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]
...@@ -150,6 +169,33 @@ runIHaskell profile app args = void . shellyNoDir $ do ...@@ -150,6 +169,33 @@ runIHaskell profile app args = void . shellyNoDir $ do
-- Run the IHaskell command. -- Run the IHaskell command.
ipython False $ map pack $ [app, "--profile", profile] ++ args ipython False $ map pack $ [app, "--profile", profile] ++ args
runConsole :: InitInfo -> IO ()
runConsole initInfo = void . shellyNoDir $ do
writeInitInfo initInfo
runIHaskell ipythonProfile "console" []
runNotebook :: InitInfo -> Maybe String -> IO ()
runNotebook initInfo maybeServeDir = void . shellyNoDir $ do
(_, _, notebookDir) <- ihaskellDirs
let args = case maybeServeDir of
Nothing -> ["--notebook-dir", unpack notebookDir]
Just dir -> ["--notebook-dir", dir]
writeInitInfo initInfo
runIHaskell ipythonProfile "notebook" args
writeInitInfo :: InitInfo -> Sh ()
writeInitInfo info = do
(ihaskellDir, _, _) <- ihaskellDirs
let filename = fromText $ ihaskellDir ++ "/last-arguments"
liftIO $ writeFile filename $ show info
readInitInfo :: IO InitInfo
readInitInfo = shellyNoDir $ do
(ihaskellDir, _, _) <- ihaskellDirs
let filename = fromText $ ihaskellDir ++ "/last-arguments"
read <$> liftIO (readFile filename)
-- | Create the IPython profile. -- | Create the IPython profile.
setupIPythonProfile :: String -- ^ IHaskell profile name. setupIPythonProfile :: String -- ^ IHaskell profile name.
-> IO () -> IO ()
......
...@@ -15,6 +15,7 @@ module IHaskell.Types ( ...@@ -15,6 +15,7 @@ module IHaskell.Types (
MimeType(..), MimeType(..),
DisplayData(..), DisplayData(..),
ExecuteReplyStatus(..), ExecuteReplyStatus(..),
InitInfo(..),
) where ) where
import ClassyPrelude import ClassyPrelude
...@@ -65,6 +66,13 @@ instance ToJSON Profile where ...@@ -65,6 +66,13 @@ instance ToJSON Profile where
"key" .= key profile "key" .= key profile
] ]
-- | Initialization information for the kernel.
data InitInfo = InitInfo {
extensions :: [String], -- ^ Extensions to enable at start.
initCells :: [String] -- ^ Code blocks to run before start.
}
deriving (Show, Read)
-- | A message header with some metadata. -- | A message header with some metadata.
data MessageHeader = MessageHeader { data MessageHeader = MessageHeader {
identifiers :: [ByteString], -- ^ The identifiers sent with the message. identifiers :: [ByteString], -- ^ The identifiers sent with the message.
......
...@@ -5,12 +5,14 @@ ...@@ -5,12 +5,14 @@
-- Chans to communicate with the ZeroMQ sockets. -- Chans to communicate with the ZeroMQ sockets.
module Main where module Main where
import ClassyPrelude hiding (liftIO) import ClassyPrelude hiding (liftIO)
import Prelude (last)
import Control.Concurrent.Chan import Control.Concurrent.Chan
import Control.Concurrent (threadDelay) import Control.Concurrent (threadDelay)
import Data.Aeson import Data.Aeson
import Text.Printf import Text.Printf
import System.Exit (exitSuccess) import System.Exit (exitSuccess)
import System.Directory import System.Directory
import System.Console.CmdArgs.Explicit hiding (complete)
import qualified Data.Map as Map import qualified Data.Map as Map
...@@ -23,51 +25,160 @@ import IHaskell.Eval.Info ...@@ -23,51 +25,160 @@ import IHaskell.Eval.Info
import qualified Data.ByteString.Char8 as Chars import qualified Data.ByteString.Char8 as Chars
import IHaskell.IPython import IHaskell.IPython
import GHC import GHC hiding (extensions)
import Outputable (showSDoc, ppr) import Outputable (showSDoc, ppr)
-- All state stored in the kernel between executions.
data KernelState = KernelState data KernelState = KernelState
{ getExecutionCounter :: Int { getExecutionCounter :: Int
} }
-- Command line arguments to IHaskell. A set of aruments is annotated with
-- the mode being invoked.
data Args = Args IHaskellMode [Argument]
data Argument
= ServeFrom String -- ^ Which directory to serve notebooks from.
| Extension String -- ^ An extension to load at startup.
| ConfFile String -- ^ A file with commands to load at startup.
| Help -- ^ Display help text.
deriving (Eq, Show)
-- Which mode IHaskell is being invoked in.
-- `None` means no mode was specified.
data IHaskellMode
= None
| Notebook
| Console
| UpdateIPython
| Kernel (Maybe String)
deriving (Eq, Show)
main :: IO () main :: IO ()
main = do main = do
args <- map unpack <$> getArgs stringArgs <- map unpack <$> getArgs
ihaskell args writeFile "/users/silver/bloop" $ show stringArgs
case process ihaskellArgs stringArgs of
ihaskell args = do Left errmsg -> putStrLn $ pack errmsg
Right args ->
ihaskell args
universalFlags :: [Flag Args]
universalFlags = [
flagReq ["extension","e", "X"] (store Extension) "<ghc-extension>" "Extension to enable at start.",
flagReq ["conf","c"] (store ConfFile) "<file.hs>" "File with commands to execute at start.",
flagHelpSimple (add Help)
]
where
add flag (Args mode flags) = Args mode $ flag : flags
store :: (String -> Argument) -> String -> Args -> Either String Args
store constructor str (Args mode prev) = Right $ Args mode $ constructor str : prev
notebook :: Mode Args
notebook = mode "notebook" (Args Notebook []) "Browser-based notebook interface." noArgs $
flagReq ["serve","s"] (store ServeFrom) "<dir>" "Directory to serve notebooks from.":
universalFlags
console :: Mode Args
console = mode "console" (Args Console []) "Console-based interactive repl." noArgs universalFlags
kernel = mode "kernel" (Args (Kernel Nothing) []) "Invoke the IHaskell kernel." kernelArg []
where
kernelArg = flagArg update "<json-kernel-file>"
update filename (Args _ flags) = Right $ Args (Kernel $ Just filename) flags
update :: Mode Args
update = mode "update" (Args UpdateIPython []) "Update IPython frontends." noArgs []
ihaskellArgs :: Mode Args
ihaskellArgs = (modeEmpty $ Args None []) { modeGroupModes = toGroup [console, notebook, update, kernel] }
noArgs = flagArg unexpected ""
where
unexpected a = error $ "Unexpected argument: " ++ a
ihaskell :: Args -> IO ()
-- If no mode is specified, print help text.
ihaskell (Args None _) =
print $ helpText [] HelpFormatAll ihaskellArgs
-- Update IPython: remove then reinstall.
-- This is in case cabal updates IHaskell but the corresponding IPython
-- isn't updated. This is hard to detect since versions of IPython might
-- not change!
ihaskell (Args UpdateIPython _) = do
removeIPython
installIPython
putStrLn "IPython updated."
ihaskell (Args Console flags) = showingHelp Console flags $ do
installed <- ipythonInstalled installed <- ipythonInstalled
unless installed installIPython unless installed installIPython
case args of flags <- addDefaultConfFile flags
-- Create the "haskell" profile. info <- initInfo flags
["setup"] -> setupIPythonProfile "haskell" runConsole info
-- Run the ipython <cmd> --profile haskell <args> command.
"notebook":ipythonArgs -> runIHaskell "haskell" "notebook" ipythonArgs
"console":ipythonArgs -> runIHaskell "haskell" "console" ipythonArgs
-- Read the profile JSON file from the argument list. ihaskell (Args Notebook flags) = showingHelp Notebook flags $ do
["kernel", profileSrc] -> kernel profileSrc installed <- ipythonInstalled
unless installed installIPython
-- Bad arguments.
[] -> do
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
let server = case mapMaybe serveDir flags of
[] -> Nothing
xs -> Just $ last xs
flags <- addDefaultConfFile flags
info <- initInfo flags
runNotebook info server
where
serveDir (ServeFrom dir) = Just dir
serveDir _ = Nothing
ihaskell (Args (Kernel (Just filename)) _) = do
initInfo <- readInitInfo
runKernel filename initInfo
-- | Add a conf file to the arguments if none exists.
addDefaultConfFile :: [Argument] -> IO [Argument]
addDefaultConfFile flags = do
def <- defaultConfFile
case (find isConfFile flags, def) of
(Nothing, Just file) -> return $ ConfFile file : flags
_ -> return flags
where
isConfFile (ConfFile _) = True
isConfFile _ = False
showingHelp :: IHaskellMode -> [Argument] -> IO () -> IO ()
showingHelp mode flags act =
case find (==Help) flags of
Just _ ->
print $ helpText [] HelpFormatAll $ chooseMode mode
Nothing ->
act
where
chooseMode Console = console
chooseMode Notebook = notebook
chooseMode (Kernel _) = kernel
chooseMode UpdateIPython = update
-- | Parse initialization information from the flags.
initInfo :: [Argument] -> IO InitInfo
initInfo [] = return InitInfo { extensions = [], initCells = []}
initInfo (flag:flags) = do
info <- initInfo flags
case flag of
Extension ext -> return info { extensions = ext:extensions info }
ConfFile filename -> do
cell <- readFile (fpFromText $ pack filename)
return info { initCells = cell:initCells info }
-- | Run the IHaskell language kernel. -- | Run the IHaskell language kernel.
kernel :: String -- ^ Filename of profile JSON file. runKernel :: String -- ^ Filename of profile JSON file.
-> IO () -> InitInfo -- ^ Initialization information from the invocation.
kernel profileSrc = do -> IO ()
runKernel profileSrc initInfo = do
-- Switch to a temporary directory so that any files we create aren't -- Switch to a temporary directory so that any files we create aren't
-- visible. On Unix, this is usually /tmp. If there is no temporary -- visible. On Unix, this is usually /tmp. If there is no temporary
-- directory available, just stay in the current one and ignore the -- directory available, just stay in the current one and ignore the
...@@ -83,20 +194,31 @@ kernel profileSrc = do ...@@ -83,20 +194,31 @@ kernel profileSrc = do
state <- initialKernelState state <- initialKernelState
-- Receive and reply to all messages on the shell socket. -- Receive and reply to all messages on the shell socket.
interpret $ forever $ do interpret $ do
-- Read the request from the request channel. -- Initialize the context by evaluating everything we got from the
request <- liftIO $ readChan $ shellRequestChannel interface -- command line flags. This includes enabling some extensions and also
-- running some code.
-- Create a header for the reply. let extLines = map (":extension " ++) $ extensions initInfo
replyHeader <- createReplyHeader (header request) noPublish _ _ = return ()
zero = 0 -- To please hlint
-- Create the reply, possibly modifying kernel state. evaluator line = evaluate zero line noPublish
oldState <- liftIO $ takeMVar state mapM_ evaluator extLines
(newState, reply) <- replyTo interface request replyHeader oldState mapM_ evaluator $ initCells initInfo
liftIO $ putMVar state newState
forever $ do
-- Write the reply to the reply channel. -- Read the request from the request channel.
liftIO $ writeChan (shellReplyChannel interface) reply request <- liftIO $ readChan $ shellRequestChannel interface
-- Create a header for the reply.
replyHeader <- createReplyHeader (header request)
-- Create the reply, possibly modifying kernel state.
oldState <- liftIO $ takeMVar state
(newState, reply) <- replyTo interface request replyHeader oldState
liftIO $ putMVar state newState
-- Write the reply to the reply channel.
liftIO $ writeChan (shellReplyChannel interface) reply
-- Initial kernel state. -- Initial kernel state.
initialKernelState :: IO (MVar KernelState) initialKernelState :: IO (MVar KernelState)
......
# Empty. # Empty.
c = get_config()
c.TerminalIPythonApp.display_banner = False
No preview for this file type
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