File: darcs-monitor.hs

package info (click to toggle)
darcs-monitor 0.4.2-14
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 196 kB
  • sloc: haskell: 907; makefile: 3
file content (283 lines) | stat: -rw-r--r-- 12,580 bytes parent folder | download | duplicates (4)
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
{-# LANGUAGE ScopedTypeVariables #-}
{-  darcs-monitor - Darcs repository monitor
    Copyright © 2007 Antti-Juhani Kaijanaho
    Copyright © 2007 Benja Fallenstein
    Copyright © 2007 Benjamin Franksen
    Copyright © 2010 Marco Túlio Gontijo e Silva <marcot@marcot.eti.br>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-}
module Main where

import Control.Arrow
import Control.Monad
import Control.Monad.Trans
import Data.Char
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Version
import System.Console.GetOpt
import System.Directory
import System.Environment
import System.Exit
import System.IO
import Text.XML.HaXml hiding (when,version,x,tag,cdata)
import Text.XML.HaXml.Posn
import Control.Exception (catch, IOException)

import Config
import Darcs
import EmailPatch
import PatchData
import Paths_darcs_monitor (version)

data Opt = HelpOpt
         | VersionOpt
         | DarcsPathOpt String
         | DarcsArgsOpt [String]
         | SendmailOpt String
         | MaxDiffOpt Int
         | EmailTemplateOpt String
         | CharsetOpt String
         | DryRunOpt

options :: [OptDescr Opt]
options = 
    [Option "n" ["dry-run"] (NoArg DryRunOpt)
                "Do not actually do anything, but do mark it done"
    ,Option "" ["darcs-path"] (ReqArg DarcsPathOpt "FILE")
                "Provide location of darcs"
    ,Option "" ["darcs-args"] (ReqArg (DarcsArgsOpt . splitArgs) "ARGS")
                "Provide additional arguments to darcs\n\
                \(separate arguments with commas)"
    ,Option "" ["use-sendmail"] (OptArg (\m -> SendmailOpt $
                                               case m of
                                                Nothing -> defaultSendmail
                                                Just s  -> s)
                                 "FILE")
     ("Use sendmail (either " ++ defaultSendmail ++ " or FILE")
    ,Option "d" ["max-diff"] (ReqArg (MaxDiffOpt . read) "SIZE")
                "Only include the first SIZE bytes of the diff in the e-mail"
    ,Option "" ["email-template"] (ReqArg EmailTemplateOpt "FILE")
                "Provide a template for emails"
    ,Option "" ["charset"] (ReqArg CharsetOpt "CHARSET")
              "Specify character set"
    ,Option "h" ["help"] (NoArg HelpOpt) "Show usage"
    ,Option "" ["version"] (NoArg VersionOpt) "Show version"
    ]

findOpt :: [a] -> b -> (a -> Maybe b) -> b
findOpt [] b _ = b
findOpt (x:xs) b f = case f x of
                       Nothing -> findOpt xs b f
                       Just b' -> b'

splitArgs :: String -> [String]
splitArgs str = let (a,b) = span (/=',') str
                in case b of ',':r -> a : splitArgs r
                             [] -> [a]
                             _ -> error
                                  $ "darcs-monitor: splitArgs called with \
                                    \wrong arguments: b = " ++ b

main :: IO ()
main = do args <- getArgs
          let (oa, args', errs) = getOpt Permute options args
          when (errs /= []) $ do mapM_ putStr errs
                                 exitFailure
          pn <- getProgName
          forM_ oa $ \ opt ->
              case opt of
                HelpOpt -> do putStr (usageInfo (usage pn) options)
                              exitWith ExitSuccess
                    where usage pn_ = "Usage: " ++ pn_ ++
                                     " [OPTIONS] email RECIPIENT \
                                     \[email RECIPIENT ...] [REPO ...]"
                VersionOpt -> do putStrLn $ "darcs-monitor " ++
                                            showVersion version
                                 exitWith ExitSuccess
                _ -> return ()
          let dryRun = findOpt roa False $ \opt ->
                       case opt of DryRunOpt -> Just True
                                   _         -> Nothing
              roa = reverse oa
              conf = Config { confDarcsPath = findOpt roa "darcs" $ \opt ->
                                              case opt of
                                                DarcsPathOpt s -> Just s
                                                _              -> Nothing
                            , confDarcsArgs = findOpt roa [] $ \opt ->
                                              case opt of
                                                DarcsArgsOpt s -> Just s
                                                _              -> Nothing
                            , confProgName = pn
                            , confSendmailPath = findOpt roa defaultSendmail $
                                                 \opt ->
                                                 case opt of
                                                   SendmailOpt s -> Just s
                                                   _             -> Nothing
                            , confMaxDiff = findOpt roa Nothing $ \opt ->
                                            case opt of
                                              MaxDiffOpt s -> Just $ Just s
                                              _            -> Nothing
                            , confEmailTemplate = findOpt roa Nothing $ \opt ->
                                                  case opt of
                                                    EmailTemplateOpt s ->
                                                         Just (Just s)
                                                    _ -> Nothing
                            , confCharset = findOpt roa Nothing $ \opt ->
                                            case opt of 
                                              CharsetOpt s -> Just (Just s)
                                              _            -> Nothing
                            }
          flip runReaderT conf $ do
            (cmd,args'') <- case args' of
                              ["email"] -> err "email requires an argument"
                              ("email":addr:rest) ->
                                  let (addrs, rest_) = getMoreEmails rest in
                                  return (("emailPatch " ++ addr,
                                           emailPatch (addr : addrs)),
                                          rest_)
                              ("print":rest) ->
                                  return (("print",
                                           \p -> liftIO $ print p >> 
                                                 return True),
                                          rest)
                              s:_ -> err ("unknown command " ++ s)
                              _ -> err "missing command"
            repos <- case args'' of [] -> do wd <- liftIO getCurrentDirectory
                                             return [wd]
                                    _  -> return args''
            mapM_ (processRepo dryRun cmd) repos

getMoreEmails :: [String] -> ([String], [String])
getMoreEmails ("email" : addr : rest) = first (addr :) $ getMoreEmails rest
getMoreEmails rest = ([], rest)

err :: (MonadConfig m, MonadIO m) => String -> m a
err msg = do pn <- asks confProgName
             liftIO $ do hPutStrLn stderr (pn ++ ": " ++ msg)
                         exitFailure

type Command m = (String, PatchData -> m Bool)

dir :: String
dir = "_darcs/third-party/darcs-monitor/"

seenPatchesFileName :: String
seenPatchesFileName = dir ++ "seen-patches"

type SeenPatches = Map String (Set String)

readSeenPatches :: MonadIO m => String -> m SeenPatches
readSeenPatches repo =
  liftIO $ Control.Exception.catch (do fc <- readFile (repo ++ seenPatchesFileName)
                                       return (read fc)
                 ) $ \(_::IOException) -> return Map.empty


processRepo :: (MonadConfig m, MonadIO m) => Bool -> Command m -> FilePath -> m ()
processRepo dryRun (tag,cmd) repo' = do
  let repo = case last repo' of '/' -> repo'
                                _   -> repo' ++ "/"
  liftIO $ createDirectoryIfMissing True (repo++dir)
  seenPatches <- readSeenPatches repo
  xml <- invokeDarcs ["changes", "--reverse", "--repo="++repo,"--xml-output","-a"]
  let Document _ _ (Elem (N "changelog") _ content) _ = xmlParse repo xml 
  let patches
        = filter (\c -> case c of CElem _e _ -> True ; _ -> False) content
  spl <- forM patches $ \ (CElem (Elem (N "patch") attrs content_) _) -> 
      do let author = getAttr (N "author") attrs
             localDate = getAttr (N "local_date") attrs
             hash = getAttr (N "hash") attrs
             name = getElem (N "name") content_
             comment = getElem (N "comment") content_
             (authorName, authorEmail) = parseAuthor author
             dt = PatchData { patchRepo = repo
                            , patchAuthor = authorName
                            , patchAuthorEmail = authorEmail
                            , patchDate = localDate
                            , patchHash = hash
                            , patchTitle = name
                            , patchComment = comment
                            , patchRepoDir = (repo++dir)
                            }
         res <- let f set | dryRun = return (Set.insert tag set)
                    f set = do ok <- cmd dt
                               return $ if ok then Set.insert tag set else set
                in case Map.lookup hash seenPatches of
                     Just set
                       -> if Set.member tag set then return set else f set
                     Nothing -> f Set.empty
         return (hash,res)
  let seenPatches' :: SeenPatches
      seenPatches' = Map.fromList spl
  liftIO $ writeFile (repo ++ seenPatchesFileName) (show seenPatches')

parseAuthor :: String -> (String, String)
parseAuthor str
    | '<' `elem` str = let (name, '<':rest) = span (/= '<') str
                           (addr, _) = span (/= '>') rest
                       in (trim name, trim addr)
    | '(' `elem` str = let (addr, '(':rest) = span (/= '(') str
                           (name, _) = span (/= ')') rest
                       in (trim name, trim addr)
    | otherwise      = ("", trim str)

ltrim :: String -> String
ltrim = dropWhile isSpace

trim :: String -> String
trim = reverse . ltrim . reverse . ltrim

getAttr :: (Eq a) => a -> [(a, AttValue)] -> String
getAttr name attrs = case lookup name attrs of
                       Nothing -> ""
                       Just (AttValue x) -> massage x ""
    where massage (Left s : ss) = showString s .
                                  massage ss
          massage (Right ref : ss) = getRef ref .
                                     massage ss
          massage [] = id

getElem :: QName -> [Content Posn] -> String
getElem name (CElem (Elem name' _ content) _ : _rest)
    | name == name'     = getContent content ""
getElem name (_ : rest) = getElem name rest
getElem name []
  = error
    $ "darcs-monitor: getElem called with wrong parameter: name = "
      ++ show name
      ++ ", contents = []"

getContent :: [Content Posn] -> String -> String
getContent (CElem (Elem _ _ content) _ : rest) = getContent content .
                                               getContent rest
getContent (CString _ cdata _ : rest) = showString cdata .
                                      getContent rest
getContent (CRef ref _ : rest) = getRef ref .
                               getContent rest
getContent (CMisc _ _ : rest) = getContent rest
getContent [] = id

getRef :: Reference -> String -> String
getRef (RefEntity "lt") = showChar '<' 
getRef (RefEntity "gt") = showChar '>' 
getRef (RefEntity "amp") = showChar '&' 
getRef (RefEntity "apos") = showChar '\''
getRef (RefEntity "quot") = showChar '"'
getRef (RefEntity s) = error ("unsupported entity reference &" ++ s ++ ";")
getRef (RefChar i) = showChar (toEnum i)