module System.Process.Sequential where
import Data.List (isPrefixOf)
import Data.Maybe (mapMaybe, fromJust)
import Data.Monoid (mappend)
import System.Process (readProcessWithExitCode)
import System.Directory (listDirectory,doesFileExist,removeFile)
import System.Exit (ExitCode(..))
import EndOfExe (showE)
import Sublists (intoRegularSublists)
seqFlsReadProcessWithExitCode
:: FilePath
-> Int
-> (String -> String)
-> String
-> [Int]
-> ([Int] -> [Int])
-> ([String] -> Int -> [String])
-> [String]
-> String
-> IO ([FilePath],[Int],[String],Int)
seqFlsReadProcessWithExitCode :: FilePath
-> Int
-> (FilePath -> FilePath)
-> FilePath
-> [Int]
-> ([Int] -> [Int])
-> ([FilePath] -> Int -> [FilePath])
-> [FilePath]
-> FilePath
-> IO ([FilePath], [Int], [FilePath], Int)
seqFlsReadProcessWithExitCode FilePath
executable Int
limK FilePath -> FilePath
f FilePath
searchNeedle1 [Int]
ns [Int] -> [Int]
g_N [FilePath] -> Int -> [FilePath]
h_S [FilePath]
xss FilePath
ys = do
[FilePath]
ends1 <- FilePath
-> (FilePath -> FilePath)
-> FilePath
-> [Int]
-> [FilePath]
-> FilePath
-> IO [FilePath]
seqFlsReadProcessWithExitCode1 FilePath
executable FilePath -> FilePath
f FilePath
searchNeedle1 [Int]
ns [FilePath]
xss FilePath
ys
FilePath
-> Int
-> (FilePath -> FilePath)
-> [FilePath]
-> [Int]
-> ([Int] -> [Int])
-> ([FilePath] -> Int -> [FilePath])
-> [FilePath]
-> FilePath
-> IO ([FilePath], [Int], [FilePath], Int)
recursiveApplyFGH FilePath
executable (Int
limK Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) FilePath -> FilePath
f [FilePath]
ends1 ([Int] -> [Int]
g_N [Int]
ns) [Int] -> [Int]
g_N [FilePath] -> Int -> [FilePath]
h_S ([FilePath] -> Int -> [FilePath]
h_S [FilePath]
xss (Int
limK Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) FilePath
ys
seqFlsReadProcessWithExitCode1
:: FilePath
-> (String -> String)
-> String
-> [Int]
-> [String]
-> String
-> IO [FilePath]
seqFlsReadProcessWithExitCode1 :: FilePath
-> (FilePath -> FilePath)
-> FilePath
-> [Int]
-> [FilePath]
-> FilePath
-> IO [FilePath]
seqFlsReadProcessWithExitCode1 FilePath
executable FilePath -> FilePath
f FilePath
searchNeedle [Int]
ns [FilePath]
xss FilePath
ys = do
[FilePath]
dir <- FilePath -> IO [FilePath]
listDirectory FilePath
"."
let zss :: [FilePath]
zss = (FilePath -> Bool) -> [FilePath] -> [FilePath]
forall a. (a -> Bool) -> [a] -> [a]
filter (FilePath -> FilePath -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf FilePath
searchNeedle) [FilePath]
dir
yss :: [[FilePath]]
yss = ([FilePath] -> Bool) -> [[FilePath]] -> [[FilePath]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([FilePath] -> Bool) -> [FilePath] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ([[FilePath]] -> [[FilePath]])
-> ([FilePath] -> [[FilePath]]) -> [FilePath] -> [[FilePath]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [FilePath] -> [[FilePath]]
forall a. [Int] -> [a] -> [[a]]
intoRegularSublists [Int]
ns ([FilePath] -> [[FilePath]]) -> [FilePath] -> [[FilePath]]
forall a b. (a -> b) -> a -> b
$ [FilePath]
zss
ends1 :: [FilePath]
ends1 = ([FilePath] -> FilePath) -> [[FilePath]] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath -> FilePath
f (FilePath -> FilePath)
-> ([FilePath] -> FilePath) -> [FilePath] -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> FilePath
forall a. [a] -> a
last) [[FilePath]]
yss
y1ss :: [[FilePath]]
y1ss = ([FilePath] -> FilePath -> [FilePath])
-> [[FilePath]] -> [FilePath] -> [[FilePath]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\[FilePath]
xss FilePath
rs -> [FilePath]
xss [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [FilePath
rs]) [[FilePath]]
yss [FilePath]
ends1
([FilePath] -> IO ExitCode) -> [[FilePath]] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\[FilePath]
uss -> FilePath -> [FilePath] -> [FilePath] -> FilePath -> IO ExitCode
seqFlsHelp1 FilePath
executable [FilePath]
uss [FilePath]
xss FilePath
ys) [[FilePath]]
y1ss
[FilePath] -> IO [FilePath]
forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath]
ends1
seqFlsReadProcessWithExitCode0
:: FilePath
-> (String -> String)
-> [Int]
-> [FilePath]
-> [String]
-> String
-> IO ([FilePath],[Int],[String])
seqFlsReadProcessWithExitCode0 :: FilePath
-> (FilePath -> FilePath)
-> [Int]
-> [FilePath]
-> [FilePath]
-> FilePath
-> IO ([FilePath], [Int], [FilePath])
seqFlsReadProcessWithExitCode0 FilePath
executable FilePath -> FilePath
f [Int]
ns [FilePath]
zss [FilePath]
xss FilePath
ys = do
let yss :: [[FilePath]]
yss = ([FilePath] -> Bool) -> [[FilePath]] -> [[FilePath]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([FilePath] -> Bool) -> [FilePath] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ([[FilePath]] -> [[FilePath]])
-> ([FilePath] -> [[FilePath]]) -> [FilePath] -> [[FilePath]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [FilePath] -> [[FilePath]]
forall a. [Int] -> [a] -> [[a]]
intoRegularSublists [Int]
ns ([FilePath] -> [[FilePath]]) -> [FilePath] -> [[FilePath]]
forall a b. (a -> b) -> a -> b
$ [FilePath]
zss
ends1 :: [FilePath]
ends1 = ([FilePath] -> FilePath) -> [[FilePath]] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath -> FilePath
f (FilePath -> FilePath)
-> ([FilePath] -> FilePath) -> [FilePath] -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FilePath] -> FilePath
forall a. [a] -> a
last) [[FilePath]]
yss
y1ss :: [[FilePath]]
y1ss = ([FilePath] -> FilePath -> [FilePath])
-> [[FilePath]] -> [FilePath] -> [[FilePath]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\[FilePath]
xss FilePath
rs -> [FilePath]
xss [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [FilePath
rs]) [[FilePath]]
yss [FilePath]
ends1
([FilePath] -> IO ExitCode) -> [[FilePath]] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\[FilePath]
uss -> FilePath -> [FilePath] -> [FilePath] -> FilePath -> IO ExitCode
seqFlsHelp1 FilePath
executable [FilePath]
uss [FilePath]
xss FilePath
ys) [[FilePath]]
y1ss
([FilePath], [Int], [FilePath])
-> IO ([FilePath], [Int], [FilePath])
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath]
ends1, [Int]
ns, [FilePath]
xss)
recursiveApplyFGH
:: FilePath
-> Int
-> (String -> String)
-> [FilePath]
-> [Int]
-> ([Int] -> [Int])
-> ([String] -> Int -> [String])
-> [String]
-> String
-> IO ([FilePath],[Int],[String],Int)
recursiveApplyFGH :: FilePath
-> Int
-> (FilePath -> FilePath)
-> [FilePath]
-> [Int]
-> ([Int] -> [Int])
-> ([FilePath] -> Int -> [FilePath])
-> [FilePath]
-> FilePath
-> IO ([FilePath], [Int], [FilePath], Int)
recursiveApplyFGH FilePath
executable Int
limK FilePath -> FilePath
f [FilePath]
zss [Int]
ns [Int] -> [Int]
g_N [FilePath] -> Int -> [FilePath]
h_S [FilePath]
xss FilePath
ys
| Int
limK Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = do
([FilePath]
ends1, [Int]
ns, [FilePath]
xss) <- FilePath
-> (FilePath -> FilePath)
-> [Int]
-> [FilePath]
-> [FilePath]
-> FilePath
-> IO ([FilePath], [Int], [FilePath])
seqFlsReadProcessWithExitCode0 FilePath
executable FilePath -> FilePath
f [Int]
ns [FilePath]
zss [FilePath]
xss FilePath
ys
let newNs :: [Int]
newNs = [Int] -> [Int]
g_N [Int]
ns
newXss :: [FilePath]
newXss = [FilePath] -> Int -> [FilePath]
h_S [FilePath]
xss (Int
limK Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
FilePath
-> Int
-> (FilePath -> FilePath)
-> [FilePath]
-> [Int]
-> ([Int] -> [Int])
-> ([FilePath] -> Int -> [FilePath])
-> [FilePath]
-> FilePath
-> IO ([FilePath], [Int], [FilePath], Int)
recursiveApplyFGH FilePath
executable (Int
limK Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) FilePath -> FilePath
f [FilePath]
ends1 [Int]
newNs [Int] -> [Int]
g_N [FilePath] -> Int -> [FilePath]
h_S [FilePath]
newXss FilePath
ys
| Bool
otherwise = ([FilePath], [Int], [FilePath], Int)
-> IO ([FilePath], [Int], [FilePath], Int)
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath]
zss, [Int]
ns, [FilePath]
xss, Int
limK)
seqFlsHelp1
:: FilePath
-> [FilePath]
-> [String]
-> String
-> IO ExitCode
seqFlsHelp1 :: FilePath -> [FilePath] -> [FilePath] -> FilePath -> IO ExitCode
seqFlsHelp1 FilePath
executable [FilePath]
files [FilePath]
args FilePath
poststr = do
(ExitCode
code,FilePath
hout,FilePath
herr) <- FilePath
-> [FilePath] -> FilePath -> IO (ExitCode, FilePath, FilePath)
readProcessWithExitCode (Maybe FilePath -> FilePath
forall a. HasCallStack => Maybe a -> a
fromJust (FilePath -> Maybe FilePath
showE FilePath
executable)) ([FilePath]
files [FilePath] -> [FilePath] -> [FilePath]
forall a. Monoid a => a -> a -> a
`mappend` [FilePath]
args) FilePath
poststr
case ExitCode
code of
ExitCode
ExitSuccess -> FilePath -> IO ()
putStr FilePath
hout
ExitCode
_ -> do
Bool
exi <- FilePath -> IO Bool
doesFileExist ([FilePath] -> FilePath
forall a. [a] -> a
last [FilePath]
files)
if Bool
exi then FilePath -> IO ()
removeFile ([FilePath] -> FilePath
forall a. [a] -> a
last [FilePath]
files) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> FilePath -> IO ()
putStrLn FilePath
"System.Process.Sequential.seqFlsHelp1: not successful operation. " IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
FilePath -> IO ()
putStrLn FilePath
herr
else FilePath -> IO ()
putStrLn FilePath
"System.Process.Sequential.seqFlsHelp1: not successful operation. " IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> FilePath -> IO ()
putStrLn FilePath
herr
ExitCode -> IO ExitCode
forall (m :: * -> *) a. Monad m => a -> m a
return ExitCode
code