Debian Codegraph @

etc/DepsToDot.hs

raw symbols SCIP
1 #! /usr/bin/env runhaskell
2 {-# LANGUAGE UnicodeSyntax, ViewPatterns #-}
3 {-# OPTIONS_GHC -Wall #-}
4
5 {- Copied and slightly modified from https://github.com/EvgenyMakarov/math-classes/blob/master/tools/DepsToDot.hs -}
6
7 import Data.Graph.Inductive
8 (reachable, delEdge, mkGraph, nmap, Edge, Gr, DynGraph, UEdge, LEdge, efilter, LNode, labNodes, Graph, delNodes)
9 import Data.GraphViz
10 (Attributes, toGraphID, color, toLabel, printDotGraph, nonClusteredParams, graphToDot, fmtNode, setID, X11Color(..))
11 import Data.GraphViz.Attributes.Complete
12 (Attribute(URL))
13 import Data.Text.Lazy (Text, pack, unpack)
14 import Data.List (nub, elemIndex, isSuffixOf, isPrefixOf)
15 import Control.Monad (liftM2)
16 import Data.Maybe
17 import System.Environment
18 import System.Exit
19 import System.IO
20 import System.Console.GetOpt
21 import Prelude hiding ((.))
22
23 (.) :: Functor f ⇒ (a → b) → (f a → f b)
24 (.) = fmap
25
26 dropBack :: Int → [a] → [a]
27 dropBack n = reverse . drop n . reverse
28
29 uedge :: LEdge a → Edge
30 uedge (x, y, _) = (x, y)
31
32 nfilter :: Graph gr ⇒ (LNode a → Bool) → gr a b → gr a b
33 nfilter p g = delNodes (map fst $ filter (not . p) $ labNodes g) g
34
35 untransitive :: DynGraph gr ⇒ gr a b → gr a b
36 untransitive g = efilter (not . redundant . uedge) g
37 where redundant e@(from, to) = to `elem` reachable from (delEdge e g)
38
39 read_deps :: String → Gr FilePath ()
40 read_deps input = mkGraph (zip [0..] nodes) edges
41 where
42 content :: [(FilePath, FilePath)]
43 content = do
44 (left, _ : right) ← break (==':') . lines input
45 liftM2 (,) (words left) (words right)
46 nodes :: [FilePath]
47 nodes = nub $ map fst content ++ map snd content
48 edges :: [UEdge]
49 edges = map (\(from, to) →
50 (fromJust $ elemIndex from nodes, fromJust $ elemIndex to nodes, ())) content
51
52 cut_dotvo :: String → String
53 cut_dotvo = dropBack 3
54
55 -- strip to basename
56 basename :: String → String
57 basename name = if "src/" `isPrefixOf` name then drop (length "src/") name
58 else name
59
60 cryptoRenameCoqDoc :: String → String
61 cryptoRenameCoqDoc p = if "src/" `isPrefixOf` p then "Crypto." ++ drop (length "src/") p
62 else p
63
64 coqDocURL :: String → FilePath → String
65 coqDocURL base p = base
66 ++ map (\c -> if c == '/' then '.' else c) (cryptoRenameCoqDoc (cut_dotvo p))
67 ++ ".html"
68
69 label :: Options → FilePath → Attributes
70 label opts p' =
71 [ toLabel (basename $ cut_dotvo p)
72 , color myColor
73 -- , LabelFontColor (X11Color color)
74 ] ++ maybe [] (\base -> [URL (pack $ coqDocURL base p)]) (optCoqDocBase opts)
75 where
76 p = if take 2 p' == "./" then drop 2 p' else p'
77
78 myColor :: X11Color
79 myColor
80 | "src/Rewriter" `isPrefixOf` p = Red
81 | "src/Util" `isPrefixOf` p = Blue
82 | "src/Arithmetic" `isPrefixOf` p = Orange
83 | "src/Algebra" `isPrefixOf` p = Magenta
84 | "src/PushButtonSynthesis" `isPrefixOf` p = Green
85 | otherwise = Black
86
87 makeGraph :: Options -> String -> Text
88 makeGraph opts = printDotGraph .
89 setID (toGraphID $ optTitle opts) .
90 graphToDot (nonClusteredParams {fmtNode = snd}) .
91 nmap (label opts).
92 untransitive .
93 nfilter (isSuffixOf ".vo" . snd) .
94 read_deps
95
96 data Options = Options {
97 optCoqDocBase :: Maybe String,
98 optTitle :: String,
99 optInput :: IO String,
100 optOutput :: String -> IO ()
101 }
102
103 defaultOptions :: Options
104 defaultOptions = Options {
105 optCoqDocBase = Nothing,
106 optTitle = "",
107 optInput = getContents,
108 optOutput = putStr
109 }
110
111 options :: [OptDescr (Options -> IO Options)]
112 options = [
113 Option [] ["coqdocbase"] (ReqArg (\arg opt ->
114 return opt { optCoqDocBase = Just arg }) "URL") "coqdoc base path (include trailing slash)",
115 Option ['i'] ["input"] (ReqArg (\arg opt ->
116 return opt { optInput = readFile arg }) "FILE") "input file, stdin if omitted",
117 Option ['o'] ["output"] (ReqArg (\arg opt ->
118 return opt { optOutput = writeFile arg }) "FILE") "output file, stdout if omitted",
119 Option ['t'] ["title"] (ReqArg (\arg opt ->
120 return opt { optTitle = arg }) "TITLE") "title of the graph page",
121 Option ['h'] ["help"] (NoArg (\_ ->
122 usage >> exitSuccess)) "display this help page"]
123
124 usage :: IO ()
125 usage = do
126 prg <- getProgName
127 hPutStrLn stderr $ usageInfo ("Usage: " ++ prg ++" [OPTION...]") options
128 hPutStrLn stderr "Use dot -Tsvg deps.dot -o deps.svg to render the graph"
129 hPutStrLn stderr $ replicate 30 ' ' ++ "This DepsToDot has Super Coq Powers."
130
131 main :: IO ()
132 main = do
133 argv <- getArgs
134 case getOpt Permute options argv of
135 (actions,_,[]) -> do
136 opts <- foldl (>>=) (return defaultOptions) actions
137 input <- optInput opts
138 optOutput opts $ unpack $ makeGraph opts $ input
139 (_,_,errors) -> do
140 hPutStrLn stderr $ concat errors
141 usage
142 exitFailure