1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
-- |
-- Module : $header$
-- Copyright : (c) Laurent P René de Cotret, 2019 - present
-- License : GNU GPL, version 2 or above
-- Maintainer : laurent.decotret@outlook.com
-- Stability : internal
-- Portability : portable
--
-- This module defines the @PlotM@ monad and related capabilities.
module Text.Pandoc.Filter.Plot.Monad
( Configuration (..),
PlotM,
RuntimeEnv (..),
PlotState (..),
runPlotM,
-- * Concurrent execution
mapConcurrentlyN,
-- * Running external commands
runCommand,
withPrependedPath,
-- * Halting pandoc-plot
throwStrictError,
-- * Getting file hashes
fileHash,
-- * Getting executables
executable,
-- * Logging
Verbosity (..),
LogSink (..),
debug,
err,
warning,
info,
-- * Lifting and other monadic operations
liftIO,
ask,
asks,
asksConfig,
-- * Base types
module Text.Pandoc.Filter.Plot.Monad.Types,
)
where
import Control.Concurrent.Async.Lifted (mapConcurrently)
import Control.Concurrent.MVar (MVar, newMVar, putMVar, takeMVar)
import Control.Concurrent.QSemN
( QSemN,
newQSemN,
signalQSemN,
waitQSemN,
)
import Control.Exception.Lifted (bracket, bracket_)
import Control.Monad.Reader
( MonadIO (liftIO),
MonadReader (ask),
ReaderT (runReaderT),
asks,
)
import Control.Monad.State.Strict
( MonadState (get, put),
StateT,
evalStateT,
)
import Data.ByteString.Lazy (toStrict)
import Data.Functor ((<&>))
import Data.Hashable (hash)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Text (Text, pack, unpack)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8With)
import Data.Text.Encoding.Error (lenientDecode)
import System.Directory
( doesFileExist,
getCurrentDirectory,
getModificationTime,
)
import System.Environment (getEnv, setEnv)
import System.Exit (ExitCode (..), exitFailure)
import System.Process.Typed
( byteStringInput,
byteStringOutput,
nullStream,
readProcessStderr,
setStderr,
setStdin,
setStdout,
setWorkingDir,
shell,
)
import Text.Pandoc.Definition (Format (..))
import Text.Pandoc.Filter.Plot.Monad.Logging
( LogSink (..),
Logger,
MonadLogger (..),
Verbosity (..),
debug,
err,
info,
strict,
terminateLogging,
warning,
withLogger,
)
import Text.Pandoc.Filter.Plot.Monad.Types
import Prelude hiding (fst, log, snd)
-- | pandoc-plot monad
type PlotM = StateT PlotState (ReaderT RuntimeEnv IO)
instance MonadLogger PlotM where
askLogger = asks envLogger
data RuntimeEnv = RuntimeEnv
{ envFormat :: Maybe Format, -- pandoc output format
envConfig :: Configuration,
envLogger :: Logger,
envCWD :: FilePath,
-- The following lock prevents from writing to the same file multiple times.
-- This is a rather crude fix for issue #53.
envIOLock :: MVar ()
}
-- | Get access to configuration within the @PlotM@ monad.
asksConfig :: (Configuration -> a) -> PlotM a
asksConfig f = asks (f . envConfig)
-- | Evaluate a @PlotM@ action.
runPlotM :: Maybe Format -> Configuration -> PlotM a -> IO a
runPlotM fmt conf v = do
cwd <- getCurrentDirectory
sem <- newMVar ()
st <-
PlotState <$> newMVar mempty
let verbosity = logVerbosity conf
sink = logSink conf
withLogger verbosity sink $
\logger -> runReaderT (evalStateT v st) (RuntimeEnv fmt conf logger cwd sem)
-- | maps a function, performing at most @N@ actions concurrently.
mapConcurrentlyN :: (Traversable t) => Int -> (a -> PlotM b) -> t a -> PlotM (t b)
mapConcurrentlyN n f xs = do
-- Emulating a pool of processes with locked access
sem <- liftIO $ newQSemN n
mapConcurrently (with sem . f) xs
where
with :: QSemN -> PlotM a -> PlotM a
with s = bracket_ (liftIO $ waitQSemN s 1) (liftIO $ signalQSemN s 1)
-- | Run a command within the @PlotM@ monad. Stderr stream
-- is read and decoded, while Stdout is ignored.
-- Logging happens at the debug level if the command succeeds, or at
-- the error level if it does not succeed.
runCommand ::
FilePath -> -- Directory from which to run the command
Text -> -- Command to run, including executable
PlotM (ExitCode, Text)
runCommand wordir command = do
(ec, processOutput') <-
liftIO $
readProcessStderr $
-- For Julia specifically, if the line below is not there (`setStdin (byteStringInput "")`),
-- the following error is thrown on Windows:
-- ERROR: error initializing stdin in uv_dup:
-- Unknown system error 50 (Unknown system error 50 50)
setStdin (byteStringInput "") $
setStdout nullStream $
setStderr byteStringOutput $
setWorkingDir wordir $
shell (unpack command)
let processOutput = decodeUtf8With lenientDecode $ toStrict processOutput'
logFunc =
if ec == ExitSuccess
then debug
else err
message =
T.unlines
[ "Running command",
" " <> command,
"ended with exit code " <> (pack . show $ ec)
]
errorMessage =
if processOutput == mempty
then mempty
else
T.unlines
[ "*******",
processOutput,
"*******"
]
logFunc $ message <> errorMessage
return (ec, processOutput)
-- | Prepend a directory to the PATH environment variable for the duration
-- of a computation.
--
-- This function is exception-safe; even if an exception happens during the
-- computation, the PATH environment variable will be reverted back to
-- its initial value.
withPrependedPath :: FilePath -> PlotM a -> PlotM a
withPrependedPath dir f = do
pathVar <- liftIO $ getEnv "PATH"
let pathVarPrepended = mconcat [dir, ";", pathVar]
bracket
(liftIO $ setEnv "PATH" pathVarPrepended)
(\_ -> liftIO $ setEnv "PATH" pathVar)
(const f)
-- | Throw an error that halts the execution of pandoc-plot due to a strict-mode.
throwStrictError :: Text -> PlotM ()
throwStrictError msg = do
strict msg
logger <- askLogger
liftIO $ terminateLogging logger >> exitFailure
-- Plot state is used for caching a map of filepaths to hashes
-- This allows multiple plots to depend on the same file/directory, and the file hashes
-- will only be calculated once. This is OK because pandoc-plot will not run for long.
-- We note that because figures are rendered possibly in parallel, access to
-- the state must be synchronized; otherwise, each thread might compute its own
-- hashes.
type FileHash = Word
newtype PlotState
= PlotState
(MVar (Map FilePath FileHash))
-- | Get a filehash. If the file hash has been computed before,
-- it is reused. Otherwise, the filehash is calculated and stored.
fileHash :: FilePath -> PlotM FileHash
fileHash path = do
PlotState varHashes <- get
hashes <- liftIO $ takeMVar varHashes
(fh, hashes') <- case M.lookup path hashes of
Nothing -> do
debug $ mconcat ["Calculating hash of dependency ", pack path]
fh <- fileHash' path
let hashes' = M.insert path fh hashes
return (fh, hashes')
Just h -> do
debug $ mconcat ["Hash of dependency ", pack path, " already calculated."]
return (h, hashes)
liftIO $ putMVar varHashes hashes'
put $ PlotState varHashes
return fh
where
-- As a proxy for the state of a file dependency, we use the modification time
-- This is much faster than actual file hashing
fileHash' :: FilePath -> PlotM FileHash
fileHash' fp = do
fileExists <- liftIO $ doesFileExist fp
if fileExists
then liftIO . fmap (fromIntegral . hash . show) . getModificationTime $ fp
else err (mconcat [
|