Skip to content

Commit 68b9225

Browse files
authored
Clean up --bug-report option (#1876)
* hlint: Forbid importing System.FilePath.Posix * Extract common functions to Kore.BugReport * withBugReport: Re-throw exceptions * test/include.mk: Print KORE_EXEC_OPTS * kore-exec: Only write saved proofs file if given as input * gitignore: kore-exec.tar.gz * withBugReport: Re-throw exceptions * withBugReport: ExeName argument
1 parent c5f3aef commit 68b9225

File tree

8 files changed

+132
-74
lines changed

8 files changed

+132
-74
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ cabal.project.local
4747
/shell.local.nix
4848
/TAGS
4949
/.TAGS*
50-
report-*/
50+
kore-exec.tar.gz

.hlint.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
- modules:
3333
- {name: [Data.Map], within: []}
34+
- {name: [System.FilePath.Posix], within: []}
3435
- name:
3536
- Data.Text.Prettyprint.Doc
3637
- Data.Text.Prettyprint.Doc.Render.Text

kore/app/exec/Main.hs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import Control.Monad.Catch
66
( MonadCatch
77
, SomeException
88
, displayException
9-
, finally
109
, handle
1110
, throwM
1211
)
13-
import Control.Monad.Trans
14-
( lift
15-
)
12+
import Control.Monad.Extra as Monad
1613
import qualified Data.Char as Char
1714
import Data.Default
1815
( def
@@ -73,6 +70,9 @@ import System.Exit
7370
( ExitCode (..)
7471
, exitWith
7572
)
73+
import System.FilePath
74+
( (</>)
75+
)
7676
import System.IO
7777
( IOMode (WriteMode)
7878
, withFile
@@ -81,9 +81,6 @@ import System.IO
8181
import qualified Data.Limit as Limit
8282
import Kore.Attribute.Symbol as Attribute
8383
import Kore.BugReport
84-
( BugReport (..)
85-
, parseBugReport
86-
)
8784
import Kore.Exec
8885
import Kore.IndexedModule.IndexedModule
8986
( VerifiedModule
@@ -117,7 +114,6 @@ import Kore.Log
117114
, LogMessage
118115
, SomeEntry (..)
119116
, WithLog
120-
, archiveDirectoryReport
121117
, logEntry
122118
, parseKoreLogOptions
123119
, runKoreLog
@@ -178,12 +174,8 @@ import SMT
178174
)
179175
import qualified SMT
180176
import Stats
181-
import qualified System.IO.Temp as Temp
182177

183178
import GlobalMain
184-
import System.FilePath.Posix
185-
( (</>)
186-
)
187179

188180
{-
189181
Main module to run kore-exec
@@ -504,9 +496,14 @@ writeKoreMergeFiles reportFile KoreMergeOptions { rulesFileName } =
504496
copyFile rulesFileName $ reportFile <> "/mergeRules.kore"
505497

506498
writeKoreProveFiles :: FilePath -> KoreProveOptions -> IO ()
507-
writeKoreProveFiles reportFile KoreProveOptions { specFileName, saveProofs } = do
508-
copyFile specFileName $ reportFile <> "/spec.kore"
509-
Foldable.forM_ saveProofs $ flip copyFile (reportFile <> "/saveProofs.kore")
499+
writeKoreProveFiles reportFile koreProveOptions = do
500+
let KoreProveOptions { specFileName } = koreProveOptions
501+
copyFile specFileName (reportFile </> "spec.kore")
502+
let KoreProveOptions { saveProofs } = koreProveOptions
503+
Foldable.forM_ saveProofs $ \filePath ->
504+
Monad.whenM
505+
(doesFileExist filePath)
506+
(copyFile filePath (reportFile </> "save-proofs.kore"))
510507

511508
writeOptionsAndKoreFiles :: FilePath -> KoreExecOptions -> IO ()
512509
writeOptionsAndKoreFiles
@@ -548,34 +545,31 @@ writeOptionsAndKoreFiles
548545
koreProveOptions
549546
(writeKoreProveFiles reportDirectory)
550547

548+
exeName :: ExeName
549+
exeName = ExeName "kore-exec"
550+
551551
-- TODO(virgil): Maybe add a regression test for main.
552552
-- | Loads a kore definition file and uses it to execute kore programs
553553
main :: IO ()
554554
main = do
555-
options <-
556-
mainGlobal (ExeName "kore-exec") parseKoreExecOptions parserInfoModifiers
555+
options <- mainGlobal Main.exeName parseKoreExecOptions parserInfoModifiers
557556
Foldable.forM_ (localOptions options) mainWithOptions
558557

559558
mainWithOptions :: KoreExecOptions -> IO ()
560559
mainWithOptions execOptions = do
561560
let KoreExecOptions { koreLogOptions, bugReport } = execOptions
562-
Temp.withSystemTempDirectory
563-
(fromMaybe "report" $ toReport bugReport)
564-
$ \tempDirectory -> do
565-
traceM tempDirectory
566-
exitCode <-
567-
runKoreLog tempDirectory koreLogOptions
568-
$ handle (handleSomeException tempDirectory)
569-
$ handle handleSomeEntry
570-
$ handle handleWithConfiguration go
571-
let KoreExecOptions { rtsStatistics } = execOptions
572-
Foldable.forM_ rtsStatistics $ \filePath ->
573-
writeStats filePath =<< getStats
574-
let reportPath = maybe tempDirectory ("./" <>) (toReport bugReport)
575-
finally
576-
(writeInReportDirectory tempDirectory)
577-
(archiveDirectoryReport tempDirectory reportPath)
578-
exitWith exitCode
561+
exitCode <-
562+
withBugReport Main.exeName bugReport $ \tmpDir -> do
563+
writeOptionsAndKoreFiles tmpDir execOptions
564+
go
565+
& handle handleWithConfiguration
566+
& handle handleSomeEntry
567+
& handle (handleSomeException tmpDir)
568+
& runKoreLog tmpDir koreLogOptions
569+
let KoreExecOptions { rtsStatistics } = execOptions
570+
Foldable.forM_ rtsStatistics $ \filePath ->
571+
writeStats filePath =<< getStats
572+
exitWith exitCode
579573
where
580574
KoreExecOptions { koreProveOptions } = execOptions
581575
KoreExecOptions { koreSearchOptions } = execOptions
@@ -621,13 +615,6 @@ mainWithOptions execOptions = do
621615
| otherwise =
622616
koreRun execOptions
623617

624-
writeInReportDirectory :: FilePath -> IO ()
625-
writeInReportDirectory tempDirectory = do
626-
when . isJust . toReport . bugReport
627-
<*> writeOptionsAndKoreFiles tempDirectory $ execOptions
628-
Foldable.forM_ (outputFileName execOptions)
629-
$ flip copyFile (tempDirectory <> "/outputFile.kore")
630-
631618
koreSearch :: KoreExecOptions -> KoreSearchOptions -> Main ExitCode
632619
koreSearch execOptions searchOptions = do
633620
let KoreExecOptions { definitionFileName } = execOptions

kore/app/repl/Main.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ import Options.Applicative
3434
)
3535
import System.Exit
3636
( exitFailure
37+
, exitWith
3738
)
3839

3940
import Data.Limit
4041
( Limit (..)
4142
)
43+
import Kore.BugReport
4244
import Kore.Exec
4345
( proveWithRepl
4446
)
@@ -47,7 +49,6 @@ import qualified Kore.IndexedModule.MetadataToolsBuilder as MetadataTools
4749
)
4850
import Kore.Log
4951
( KoreLogOptions (..)
50-
, archiveDirectoryReport
5152
, runLoggerT
5253
, swappableLogger
5354
, withLogger
@@ -61,7 +62,6 @@ import Kore.Syntax.Module
6162
( ModuleName (..)
6263
)
6364
import qualified SMT
64-
import qualified System.IO.Temp as Temp
6565

6666
import GlobalMain
6767

@@ -185,10 +185,12 @@ parserInfoModifiers =
185185
<> progDesc "REPL debugger for proofs"
186186
<> header "kore-repl - a repl for Kore proofs"
187187

188+
exeName :: ExeName
189+
exeName = ExeName "kore-repl"
190+
188191
main :: IO ()
189192
main = do
190-
options <-
191-
mainGlobal (ExeName "kore-repl") parseKoreReplOptions parserInfoModifiers
193+
options <- mainGlobal Main.exeName parseKoreReplOptions parserInfoModifiers
192194
case localOptions options of
193195
Nothing -> pure ()
194196
Just koreReplOptions -> mainWithOptions koreReplOptions
@@ -205,10 +207,9 @@ mainWithOptions
205207
, outputFile
206208
, koreLogOptions
207209
}
208-
=
209-
Temp.withSystemTempDirectory
210-
"report"
211-
$ \tempDirectory ->
210+
= do
211+
exitCode <-
212+
withBugReport Main.exeName (BugReport Nothing) $ \tempDirectory ->
212213
withLogger tempDirectory koreLogOptions $ \actualLogAction -> do
213214
mvarLogAction <- newMVar actualLogAction
214215
let swapLogAction = swappableLogger mvarLogAction
@@ -257,8 +258,9 @@ mainWithOptions
257258
scriptModeOutput
258259
outputFile
259260
mainModuleName
260-
archiveDirectoryReport tempDirectory tempDirectory
261261

262+
pure ExitSuccess
263+
exitWith exitCode
262264
where
263265
mainModuleName :: ModuleName
264266
mainModuleName = moduleName definitionModule

kore/src/Kore/BugReport.hs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,46 @@ License : NCSA
77
module Kore.BugReport
88
( BugReport (..)
99
, parseBugReport
10+
, withBugReport
11+
-- * Re-exports
12+
, ExitCode (..)
1013
) where
1114

1215
import Prelude.Kore
1316

17+
import qualified Codec.Archive.Tar as Tar
18+
import qualified Codec.Compression.GZip as GZip
19+
import Control.Monad.Catch
20+
( ExitCase (..)
21+
, displayException
22+
, generalBracket
23+
)
24+
import qualified Data.ByteString.Lazy as ByteString.Lazy
25+
import qualified Data.Foldable as Foldable
1426
import Options.Applicative
27+
import System.Directory
28+
( listDirectory
29+
, removePathForcibly
30+
)
31+
import System.Exit
32+
( ExitCode (..)
33+
)
34+
import System.FilePath
35+
( (<.>)
36+
, (</>)
37+
)
38+
import System.IO
39+
( hPutStrLn
40+
, stderr
41+
)
42+
import System.IO.Temp
43+
( createTempDirectory
44+
, getCanonicalTemporaryDirectory
45+
)
46+
47+
import Kore.Log.KoreLogOptions
48+
( ExeName (..)
49+
)
1550

1651
newtype BugReport = BugReport { toReport :: Maybe FilePath }
1752
deriving Show
@@ -26,3 +61,55 @@ parseBugReport =
2661
<> help "Whether to report a bug"
2762
)
2863
)
64+
65+
{- | Create a @.tar.gz@ archive containing the bug report.
66+
-}
67+
writeBugReportArchive
68+
:: FilePath -- ^ Directory to archive
69+
-> FilePath -- ^ Name of the archive file, without extension.
70+
-> IO ()
71+
writeBugReportArchive base tar = do
72+
contents <- listDirectory base
73+
let filename = tar <.> "tar" <.> "gz"
74+
ByteString.Lazy.writeFile filename . GZip.compress . Tar.write
75+
=<< Tar.pack base contents
76+
(hPutStrLn stderr . unwords) ["Created bug report:", filename]
77+
78+
{- | Run the inner action with a temporary directory holding the bug report.
79+
80+
The bug report will be saved as an archive if that was requested by the user, or
81+
if there is an error in the inner action.
82+
83+
-}
84+
withBugReport
85+
:: ExeName
86+
-> BugReport
87+
-> (FilePath -> IO ExitCode)
88+
-> IO ExitCode
89+
withBugReport exeName bugReport act = do
90+
(exitCode, _) <-
91+
generalBracket
92+
acquireTempDirectory
93+
releaseTempDirectory
94+
act
95+
pure exitCode
96+
where
97+
acquireTempDirectory = do
98+
tmp <- getCanonicalTemporaryDirectory
99+
createTempDirectory tmp (getExeName exeName)
100+
releaseTempDirectory tmpDir exitCase = do
101+
case exitCase of
102+
ExitCaseSuccess _ -> optionalWriteBugReport tmpDir
103+
ExitCaseException someException -> do
104+
let message = displayException someException
105+
writeFile (tmpDir </> "error" <.> "log") message
106+
alwaysWriteBugReport tmpDir
107+
ExitCaseAbort -> alwaysWriteBugReport tmpDir
108+
removePathForcibly tmpDir
109+
alwaysWriteBugReport tmpDir =
110+
writeBugReportArchive tmpDir
111+
(fromMaybe (getExeName exeName) (toReport bugReport))
112+
optionalWriteBugReport tmpDir =
113+
Foldable.traverse_
114+
(writeBugReportArchive tmpDir)
115+
(toReport bugReport)

kore/src/Kore/Log.hs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ module Kore.Log
1515
, Colog.logTextStderr
1616
, Colog.logTextHandle
1717
, runKoreLog
18-
, archiveDirectoryReport
1918
, module Log
2019
, module KoreLogOptions
2120
) where
2221

2322
import Prelude.Kore
2423

25-
import qualified Codec.Archive.Tar as Tar
26-
import qualified Codec.Compression.GZip as GZip
2724
import Colog
2825
( LogAction (..)
2926
)
@@ -53,7 +50,6 @@ import Control.Monad.Cont
5350
( ContT (..)
5451
, runContT
5552
)
56-
import qualified Data.ByteString.Lazy as BS
5753
import Data.Foldable
5854
( toList
5955
)
@@ -93,17 +89,10 @@ import Kore.Log.Registry
9389
)
9490
import Kore.Log.SQLite
9591
import Log
96-
import System.Directory
97-
( listDirectory
98-
)
99-
import System.FilePath.Posix
92+
import System.FilePath
10093
( (<.>)
10194
, (</>)
10295
)
103-
import System.IO
104-
( hPutStrLn
105-
, stderr
106-
)
10796

10897
-- | Internal type used to add timestamps to a 'LogMessage'.
10998
data WithTimestamp = WithTimestamp ActualEntry LocalTime
@@ -336,13 +325,3 @@ swappableLogger mvar =
336325
acquire = liftIO $ takeMVar mvar
337326
release = liftIO . putMVar mvar
338327
worker a logAction = Colog.unLogAction logAction a
339-
340-
archiveDirectoryReport
341-
:: FilePath -- ^ Path of the \".tar.gz\" file to write.
342-
-> FilePath -- ^ Directory to archive
343-
-> IO ()
344-
archiveDirectoryReport tar base = do
345-
contents <- listDirectory base
346-
BS.writeFile (tar <> ".tar.gz") . GZip.compress . Tar.write
347-
=<< Tar.pack base contents
348-
hPutStrLn stderr $ "\nCreated " <> tar <> ".tar.gz"

kore/src/Kore/Repl/Interpreter.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ import System.Directory
122122
, findExecutable
123123
)
124124
import System.Exit
125-
import System.FilePath.Posix
125+
import System.FilePath
126126
( splitFileName
127127
, (<.>)
128128
)

0 commit comments

Comments
 (0)