Logging.hs 9.63 KB
Newer Older
1
2
-----------------------------------------------------------------------------
-- |
3
-- Module      :  Distribution.Client.Logging
4
5
6
7
8
9
10
11
12
13
-- Copyright   :  (c) David Waern 2008
-- License     :  BSD-like
--
-- Maintainer  :  david.waern@gmail.com
-- Stability   :  experimental
-- Portability :  portable
--
-- Build log data structure
--
-----------------------------------------------------------------------------
14
module Distribution.Client.Logging (
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    BuildLogEntry(..),
    InstallOutcome(..),
    Outcome(..),

    -- * Constructing and writing reports
    buildLogEntry,
    writeBuildLog,

    -- * parsing and pretty printing
    parseBuildLogEntry,
    parseBuildLog,
    showBuildLogEntry,

    -- * 'InstallPlan' variants
    planPackageBuildLogEntry,
    installPlanBuildLog,
    writeInstallPlanBuildLog
  ) where

34
import Distribution.Client.Reporting
35
         ( InstallOutcome(..), Outcome(..) )
36
import Distribution.Client.Types
37
         ( ConfiguredPackage(..), BuildResult )
38
import Distribution.Client.Config
39
         ( defaultCabalDir )
40
import qualified Distribution.Client.Types as BR
41
         ( BuildResult(..) )
42
43
import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Client.InstallPlan
44
         ( InstallPlan, PlanPackage )
45
import Distribution.Client.ParseUtils
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
         ( showFields, parseBasicStanza )
import qualified Paths_cabal_install (version)

import Distribution.Package
         ( PackageIdentifier(PackageIdentifier), Package(packageId) )
import Distribution.PackageDescription
         ( FlagName(..), FlagAssignment )
--import Distribution.Version
--         ( Version )
import Distribution.System
         ( OS, Arch )
import Distribution.Compiler
         ( CompilerId )
import Distribution.Text
         ( Text(disp, parse) )
import Distribution.ParseUtils
         ( FieldDescr(..), ParseResult(..), simpleField, listField )
import qualified Distribution.Compat.ReadP as Parse
         ( ReadP, {-pfail,-} munch1, char, option )
import Text.PrettyPrint.HughesPJ as Disp
         ( Doc, char, text, (<>) )

import Data.List
         ( unfoldr )
import Data.Maybe
         ( catMaybes )
import Data.Char as Char
         ( isAlphaNum )
import System.FilePath
         ( (</>) )
--import Network.URI
--         ( URI, uriToString, parseAbsoluteURI )

type BuildLog = [BuildLogEntry]

data BuildLogEntry
   = BuildLogEntry {
    -- | The package this build report is about
    package         :: PackageIdentifier,

Duncan Coutts's avatar
Duncan Coutts committed
86
    --   Which hackage server this package is from or local
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
--    server          :: Maybe URI,

    -- | The OS and Arch the package was built on
    os              :: OS,
    arch            :: Arch,

    -- | The Haskell compiler (and hopefully version) used
    compiler        :: CompilerId,

    -- | The uploading client, ie cabal-install-x.y.z
    client          :: PackageIdentifier,

    -- | Which configurations flags we used
    flagAssignment  :: FlagAssignment,

    -- | Which dependent packages we were using exactly
    dependencies    :: [PackageIdentifier],

    -- | Did installing work ok?
    installOutcome  :: InstallOutcome,

Duncan Coutts's avatar
Duncan Coutts committed
108
    --   Which version of the Cabal library was used to compile the Setup.hs
109
110
--    cabalVersion    :: Version,

Duncan Coutts's avatar
Duncan Coutts committed
111
    --   Which build tools we were using (with versions)
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
--    tools      :: [PackageIdentifier],

    -- | Configure outcome, did configure work ok?
    docsOutcome     :: Outcome,

    -- | Configure outcome, did configure work ok?
    testsOutcome    :: Outcome
  }

writeBuildLog :: BuildLog -> IO ()
writeBuildLog reports = do
  cabalDir <- defaultCabalDir
  let file = cabalDir </> "build.log"
  appendFile file (concatMap format reports)
  --TODO: make this concurrency safe, either lock the report file or make sure
  -- the writes for each report are atomic (under 4k and flush at boundaries)

  where
    format r = '\n' : showBuildLogEntry r ++ "\n"

buildLogEntry :: OS -> Arch -> CompilerId -- -> Version
              -> ConfiguredPackage -> BR.BuildResult
              -> BuildLogEntry
buildLogEntry os' arch' comp (ConfiguredPackage pkg flags deps) result =
  BuildLogEntry {
    package               = packageId pkg,
--    server                = Nothing,
    os                    = os',
    arch                  = arch',
    compiler              = comp,
    client                = cabalInstallID,
    flagAssignment        = flags,
    dependencies          = deps,
    installOutcome        = case result of
    BR.DependentFailed p -> DependencyFailed p
    BR.UnpackFailed _    -> UnpackFailed
    BR.ConfigureFailed _ -> ConfigureFailed
    BR.BuildFailed _     -> BuildFailed
    BR.InstallFailed _   -> InstallFailed
    BR.BuildOk           -> InstallOk,
--    cabalVersion          = undefined
    docsOutcome           = NotTried,
    testsOutcome          = NotTried
  }
  where
    cabalInstallID =
      PackageIdentifier "cabal-install" Paths_cabal_install.version

-- ------------------------------------------------------------
-- * External format
-- ------------------------------------------------------------

initialBuildLogEntry :: BuildLogEntry
initialBuildLogEntry = BuildLogEntry {
    package         = requiredField "package",
--    server          = Nothing,
    os              = requiredField "os",
    arch            = requiredField "arch",
    compiler        = requiredField "compiler",
    client          = requiredField "client",
    flagAssignment  = [],
    dependencies    = [],
    installOutcome  = requiredField "install-outcome",
--    cabalVersion  = Nothing,
--    tools         = [],
    docsOutcome     = NotTried,
    testsOutcome    = NotTried
  }
  where
    requiredField fname = error ("required field: " ++ fname)

-- -----------------------------------------------------------------------------
-- Parsing

parseBuildLogEntry :: String -> ParseResult BuildLogEntry
parseBuildLogEntry = parseBasicStanza fieldDescrs initialBuildLogEntry

parseBuildLog :: String -> [BuildLogEntry]
parseBuildLog str =
  [ report | ParseOk [] report <- map parseBuildLogEntry (split str) ]

  where
    split :: String -> [String]
    split = filter (not . null) . unfoldr chunk . lines
    chunk [] = Nothing
    chunk ls = case break null ls of
                 (r, rs) -> Just (unlines r, dropWhile null rs)

-- -----------------------------------------------------------------------------
-- Pretty-printing

showBuildLogEntry :: BuildLogEntry -> String
showBuildLogEntry = showFields fieldDescrs

-- -----------------------------------------------------------------------------
-- Description of the fields, for parsing/printing

fieldDescrs :: [FieldDescr BuildLogEntry]
fieldDescrs =
 [ simpleField "package"         disp           parse
                                 package        (\v r -> r { package = v })
-- , simpleField "server"          disp           parse
--                                 server         (\v r -> r { server = v })
 , simpleField "os"              disp           parse
                                 os             (\v r -> r { os = v })
 , simpleField "arch"            disp           parse
                                 arch           (\v r -> r { arch = v })
 , simpleField "compiler"        disp           parse
                                 compiler       (\v r -> r { compiler = v })
 , simpleField "client"          disp           parse
                                 client         (\v r -> r { client = v })
 , listField   "flags"           dispFlag       parseFlag
                                 flagAssignment (\v r -> r { flagAssignment = v })
 , listField   "dependencies"    disp           parse
                                 dependencies   (\v r -> r { dependencies = v })
 , simpleField "install-outcome" disp           parse
                                 installOutcome (\v r -> r { installOutcome = v })
 , simpleField "docs-outcome"    disp           parse
                                 docsOutcome    (\v r -> r { docsOutcome = v })
 , simpleField "tests-outcome"   disp           parse
                                 testsOutcome   (\v r -> r { testsOutcome = v })
 ]

dispFlag :: (FlagName, Bool) -> Disp.Doc
dispFlag (FlagName name, True)  =                  Disp.text name
dispFlag (FlagName name, False) = Disp.char '-' <> Disp.text name

parseFlag :: Parse.ReadP r (FlagName, Bool)
parseFlag = do
  value <- Parse.option True (Parse.char '-' >> return False)
  name  <- Parse.munch1 (\c -> Char.isAlphaNum c || c == '_' || c == '-')
  return (FlagName name, value)

{-
instance Text URI where
  disp uri = Disp.text (uriToString id uri [])
  parse = do
    str <- Parse.munch1 (\c -> isAlphaNum c || c `elem` "+-=._/*()@'$:;&!?")
    maybe Parse.pfail return (parseAbsoluteURI str)
-}
-- ------------------------------------------------------------
-- * InstallPlan support
-- ------------------------------------------------------------

writeInstallPlanBuildLog :: InstallPlan BuildResult -> IO ()
writeInstallPlanBuildLog = writeBuildLog . installPlanBuildLog

installPlanBuildLog :: InstallPlan BuildResult -> BuildLog
installPlanBuildLog plan = catMaybes
                             . map (planPackageBuildLogEntry os' arch' comp)
                             . InstallPlan.toList
                             $ plan
  where os'   = InstallPlan.planOS plan
        arch' = InstallPlan.planArch plan
        comp  = InstallPlan.planCompiler plan

planPackageBuildLogEntry :: OS -> Arch -> CompilerId
                         -> InstallPlan.PlanPackage BuildResult
                         -> Maybe BuildLogEntry
planPackageBuildLogEntry os' arch' comp planPackage = case planPackage of

  InstallPlan.Installed pkg
    -> Just $ buildLogEntry os' arch' comp pkg BR.BuildOk

  InstallPlan.Failed pkg result
    -> Just $ buildLogEntry os' arch' comp pkg result

  _ -> Nothing