root / src / Ganeti / Utils.hs @ b6772b46
History | View | Annotate | Download (16 kB)
1 | 525bfb36 | Iustin Pop | {-| Utility functions. -} |
---|---|---|---|
2 | e4f08c46 | Iustin Pop | |
3 | e2fa2baf | Iustin Pop | {- |
4 | e2fa2baf | Iustin Pop | |
5 | a7f0953a | Iustin Pop | Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
6 | e2fa2baf | Iustin Pop | |
7 | e2fa2baf | Iustin Pop | This program is free software; you can redistribute it and/or modify |
8 | e2fa2baf | Iustin Pop | it under the terms of the GNU General Public License as published by |
9 | e2fa2baf | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
10 | e2fa2baf | Iustin Pop | (at your option) any later version. |
11 | e2fa2baf | Iustin Pop | |
12 | e2fa2baf | Iustin Pop | This program is distributed in the hope that it will be useful, but |
13 | e2fa2baf | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | e2fa2baf | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | e2fa2baf | Iustin Pop | General Public License for more details. |
16 | e2fa2baf | Iustin Pop | |
17 | e2fa2baf | Iustin Pop | You should have received a copy of the GNU General Public License |
18 | e2fa2baf | Iustin Pop | along with this program; if not, write to the Free Software |
19 | e2fa2baf | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
20 | e2fa2baf | Iustin Pop | 02110-1301, USA. |
21 | e2fa2baf | Iustin Pop | |
22 | e2fa2baf | Iustin Pop | -} |
23 | e2fa2baf | Iustin Pop | |
24 | 26d62e4c | Iustin Pop | module Ganeti.Utils |
25 | ebf38064 | Iustin Pop | ( debug |
26 | ebf38064 | Iustin Pop | , debugFn |
27 | ebf38064 | Iustin Pop | , debugXy |
28 | ebf38064 | Iustin Pop | , sepSplit |
29 | ebf38064 | Iustin Pop | , stdDev |
30 | ebf38064 | Iustin Pop | , if' |
31 | ebf38064 | Iustin Pop | , select |
32 | ebf38064 | Iustin Pop | , applyIf |
33 | ebf38064 | Iustin Pop | , commaJoin |
34 | 79eef90b | Agata Murawska | , ensureQuoted |
35 | ebf38064 | Iustin Pop | , tryRead |
36 | ebf38064 | Iustin Pop | , formatTable |
37 | c3024b7e | René Nussbaumer | , printTable |
38 | ebf38064 | Iustin Pop | , parseUnit |
39 | 19e310cc | René Nussbaumer | , plural |
40 | 04edfc99 | Iustin Pop | , niceSort |
41 | 04edfc99 | Iustin Pop | , niceSortKey |
42 | 88a10df5 | Iustin Pop | , exitIfBad |
43 | 88a10df5 | Iustin Pop | , exitErr |
44 | 88a10df5 | Iustin Pop | , exitWhen |
45 | 88a10df5 | Iustin Pop | , exitUnless |
46 | 838393d1 | Michele Tartara | , logWarningIfBad |
47 | 256e28c4 | Iustin Pop | , rStripSpace |
48 | 80a0546b | Michele Tartara | , newUUID |
49 | ace37e24 | Michele Tartara | , getCurrentTime |
50 | a6e054a8 | Iustin Pop | , getCurrentTimeUSec |
51 | b6aeda4a | Dato Simó | , clockTimeToString |
52 | b009f682 | Dato Simó | , chompPrefix |
53 | 7dbe4c72 | Klaus Aehlig | , warn |
54 | 9fb621af | Yiannis Tsiouris | , wrap |
55 | 9fb621af | Yiannis Tsiouris | , trim |
56 | 72747d91 | Iustin Pop | , defaultHead |
57 | 72747d91 | Iustin Pop | , exitIfEmpty |
58 | da9e2aff | Iustin Pop | , splitEithers |
59 | da9e2aff | Iustin Pop | , recombineEithers |
60 | a39cd547 | Michele Tartara | , setOwnerAndGroupFromNames |
61 | ebf38064 | Iustin Pop | ) where |
62 | e4f08c46 | Iustin Pop | |
63 | 256e28c4 | Iustin Pop | import Data.Char (toUpper, isAlphaNum, isDigit, isSpace) |
64 | 04edfc99 | Iustin Pop | import Data.Function (on) |
65 | 29ac5975 | Iustin Pop | import Data.List |
66 | a39cd547 | Michele Tartara | import qualified Data.Map as M |
67 | da9e2aff | Iustin Pop | import Control.Monad (foldM) |
68 | e4f08c46 | Iustin Pop | |
69 | e4f08c46 | Iustin Pop | import Debug.Trace |
70 | e4f08c46 | Iustin Pop | |
71 | 88a10df5 | Iustin Pop | import Ganeti.BasicTypes |
72 | 80a0546b | Michele Tartara | import qualified Ganeti.Constants as C |
73 | 838393d1 | Michele Tartara | import Ganeti.Logging |
74 | a39cd547 | Michele Tartara | import Ganeti.Runtime |
75 | 88a10df5 | Iustin Pop | import System.IO |
76 | 88a10df5 | Iustin Pop | import System.Exit |
77 | a39cd547 | Michele Tartara | import System.Posix.Files |
78 | b6aeda4a | Dato Simó | import System.Time |
79 | 88a10df5 | Iustin Pop | |
80 | 9188aeef | Iustin Pop | -- * Debug functions |
81 | 9188aeef | Iustin Pop | |
82 | e4f08c46 | Iustin Pop | -- | To be used only for debugging, breaks referential integrity. |
83 | e4f08c46 | Iustin Pop | debug :: Show a => a -> a |
84 | e4f08c46 | Iustin Pop | debug x = trace (show x) x |
85 | e4f08c46 | Iustin Pop | |
86 | 525bfb36 | Iustin Pop | -- | Displays a modified form of the second parameter before returning |
87 | 525bfb36 | Iustin Pop | -- it. |
88 | adc5c176 | Iustin Pop | debugFn :: Show b => (a -> b) -> a -> a |
89 | adc5c176 | Iustin Pop | debugFn fn x = debug (fn x) `seq` x |
90 | adc5c176 | Iustin Pop | |
91 | 525bfb36 | Iustin Pop | -- | Show the first parameter before returning the second one. |
92 | adc5c176 | Iustin Pop | debugXy :: Show a => a -> b -> b |
93 | 05ff7a00 | Agata Murawska | debugXy = seq . debug |
94 | adc5c176 | Iustin Pop | |
95 | 525bfb36 | Iustin Pop | -- * Miscellaneous |
96 | 1b7cf8ca | Iustin Pop | |
97 | 61bbbed7 | Agata Murawska | -- | Apply the function if condition holds, otherwise use default value. |
98 | 61bbbed7 | Agata Murawska | applyIf :: Bool -> (a -> a) -> a -> a |
99 | 61bbbed7 | Agata Murawska | applyIf b f x = if b then f x else x |
100 | 61bbbed7 | Agata Murawska | |
101 | e4f08c46 | Iustin Pop | -- | Comma-join a string list. |
102 | e4f08c46 | Iustin Pop | commaJoin :: [String] -> String |
103 | e4f08c46 | Iustin Pop | commaJoin = intercalate "," |
104 | e4f08c46 | Iustin Pop | |
105 | 748d5d50 | Iustin Pop | -- | Split a list on a separator and return an array. |
106 | 748d5d50 | Iustin Pop | sepSplit :: Eq a => a -> [a] -> [[a]] |
107 | e4f08c46 | Iustin Pop | sepSplit sep s |
108 | ebf38064 | Iustin Pop | | null s = [] |
109 | ebf38064 | Iustin Pop | | null xs = [x] |
110 | ebf38064 | Iustin Pop | | null ys = [x,[]] |
111 | ebf38064 | Iustin Pop | | otherwise = x:sepSplit sep ys |
112 | ebf38064 | Iustin Pop | where (x, xs) = break (== sep) s |
113 | ebf38064 | Iustin Pop | ys = drop 1 xs |
114 | e4f08c46 | Iustin Pop | |
115 | 19e310cc | René Nussbaumer | -- | Simple pluralize helper |
116 | 19e310cc | René Nussbaumer | plural :: Int -> String -> String -> String |
117 | 19e310cc | René Nussbaumer | plural 1 s _ = s |
118 | 19e310cc | René Nussbaumer | plural _ _ p = p |
119 | 19e310cc | René Nussbaumer | |
120 | 79eef90b | Agata Murawska | -- | Ensure a value is quoted if needed. |
121 | 79eef90b | Agata Murawska | ensureQuoted :: String -> String |
122 | 79eef90b | Agata Murawska | ensureQuoted v = if not (all (\c -> isAlphaNum c || c == '.') v) |
123 | 79eef90b | Agata Murawska | then '\'':v ++ "'" |
124 | 79eef90b | Agata Murawska | else v |
125 | 79eef90b | Agata Murawska | |
126 | 9188aeef | Iustin Pop | -- * Mathematical functions |
127 | 9188aeef | Iustin Pop | |
128 | 185297fa | Iustin Pop | -- Simple and slow statistical functions, please replace with better |
129 | 185297fa | Iustin Pop | -- versions |
130 | e4f08c46 | Iustin Pop | |
131 | 525bfb36 | Iustin Pop | -- | Standard deviation function. |
132 | 4715711d | Iustin Pop | stdDev :: [Double] -> Double |
133 | 4715711d | Iustin Pop | stdDev lst = |
134 | 7570569e | Iustin Pop | -- first, calculate the list length and sum lst in a single step, |
135 | 7570569e | Iustin Pop | -- for performance reasons |
136 | 7570569e | Iustin Pop | let (ll', sx) = foldl' (\(rl, rs) e -> |
137 | 7570569e | Iustin Pop | let rl' = rl + 1 |
138 | 7570569e | Iustin Pop | rs' = rs + e |
139 | 7570569e | Iustin Pop | in rl' `seq` rs' `seq` (rl', rs')) (0::Int, 0) lst |
140 | 7570569e | Iustin Pop | ll = fromIntegral ll'::Double |
141 | 7570569e | Iustin Pop | mv = sx / ll |
142 | 7570569e | Iustin Pop | av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst |
143 | 4715711d | Iustin Pop | in sqrt (av / ll) -- stddev |
144 | dd4c56ed | Iustin Pop | |
145 | bfe6c954 | Guido Trotter | -- * Logical functions |
146 | bfe6c954 | Guido Trotter | |
147 | bfe6c954 | Guido Trotter | -- Avoid syntactic sugar and enhance readability. These functions are proposed |
148 | bfe6c954 | Guido Trotter | -- by some for inclusion in the Prelude, and at the moment they are present |
149 | bfe6c954 | Guido Trotter | -- (with various definitions) in the utility-ht package. Some rationale and |
150 | bfe6c954 | Guido Trotter | -- discussion is available at <http://www.haskell.org/haskellwiki/If-then-else> |
151 | bfe6c954 | Guido Trotter | |
152 | bfe6c954 | Guido Trotter | -- | \"if\" as a function, rather than as syntactic sugar. |
153 | bfe6c954 | Guido Trotter | if' :: Bool -- ^ condition |
154 | bfe6c954 | Guido Trotter | -> a -- ^ \"then\" result |
155 | bfe6c954 | Guido Trotter | -> a -- ^ \"else\" result |
156 | bfe6c954 | Guido Trotter | -> a -- ^ \"then\" or "else" result depending on the condition |
157 | bfe6c954 | Guido Trotter | if' True x _ = x |
158 | bfe6c954 | Guido Trotter | if' _ _ y = y |
159 | bfe6c954 | Guido Trotter | |
160 | 5b763470 | Iustin Pop | -- * Parsing utility functions |
161 | 5b763470 | Iustin Pop | |
162 | 525bfb36 | Iustin Pop | -- | Parse results from readsPrec. |
163 | 5b763470 | Iustin Pop | parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a |
164 | 5b763470 | Iustin Pop | parseChoices _ _ ((v, ""):[]) = return v |
165 | 5b763470 | Iustin Pop | parseChoices name s ((_, e):[]) = |
166 | 5b763470 | Iustin Pop | fail $ name ++ ": leftover characters when parsing '" |
167 | 5b763470 | Iustin Pop | ++ s ++ "': '" ++ e ++ "'" |
168 | 5b763470 | Iustin Pop | parseChoices name s _ = fail $ name ++ ": cannot parse string '" ++ s ++ "'" |
169 | 5b763470 | Iustin Pop | |
170 | 5b763470 | Iustin Pop | -- | Safe 'read' function returning data encapsulated in a Result. |
171 | 5b763470 | Iustin Pop | tryRead :: (Monad m, Read a) => String -> String -> m a |
172 | 5b763470 | Iustin Pop | tryRead name s = parseChoices name s $ reads s |
173 | c5f7412e | Iustin Pop | |
174 | 525bfb36 | Iustin Pop | -- | Format a table of strings to maintain consistent length. |
175 | c5f7412e | Iustin Pop | formatTable :: [[String]] -> [Bool] -> [[String]] |
176 | c5f7412e | Iustin Pop | formatTable vals numpos = |
177 | c5f7412e | Iustin Pop | let vtrans = transpose vals -- transpose, so that we work on rows |
178 | c5f7412e | Iustin Pop | -- rather than columns |
179 | c5f7412e | Iustin Pop | mlens = map (maximum . map length) vtrans |
180 | c5f7412e | Iustin Pop | expnd = map (\(flds, isnum, ml) -> |
181 | c5f7412e | Iustin Pop | map (\val -> |
182 | c5f7412e | Iustin Pop | let delta = ml - length val |
183 | c5f7412e | Iustin Pop | filler = replicate delta ' ' |
184 | c5f7412e | Iustin Pop | in if delta > 0 |
185 | c5f7412e | Iustin Pop | then if isnum |
186 | c5f7412e | Iustin Pop | then filler ++ val |
187 | c5f7412e | Iustin Pop | else val ++ filler |
188 | c5f7412e | Iustin Pop | else val |
189 | c5f7412e | Iustin Pop | ) flds |
190 | c5f7412e | Iustin Pop | ) (zip3 vtrans numpos mlens) |
191 | c5f7412e | Iustin Pop | in transpose expnd |
192 | 9b9da389 | Iustin Pop | |
193 | c3024b7e | René Nussbaumer | -- | Constructs a printable table from given header and rows |
194 | c3024b7e | René Nussbaumer | printTable :: String -> [String] -> [[String]] -> [Bool] -> String |
195 | c3024b7e | René Nussbaumer | printTable lp header rows isnum = |
196 | 2cdaf225 | Iustin Pop | unlines . map ((++) lp . (:) ' ' . unwords) $ |
197 | c3024b7e | René Nussbaumer | formatTable (header:rows) isnum |
198 | c3024b7e | René Nussbaumer | |
199 | 1cdcf8f3 | Iustin Pop | -- | Converts a unit (e.g. m or GB) into a scaling factor. |
200 | 1cdcf8f3 | Iustin Pop | parseUnitValue :: (Monad m) => String -> m Rational |
201 | 1cdcf8f3 | Iustin Pop | parseUnitValue unit |
202 | 1cdcf8f3 | Iustin Pop | -- binary conversions first |
203 | 1cdcf8f3 | Iustin Pop | | null unit = return 1 |
204 | 1cdcf8f3 | Iustin Pop | | unit == "m" || upper == "MIB" = return 1 |
205 | 1cdcf8f3 | Iustin Pop | | unit == "g" || upper == "GIB" = return kbBinary |
206 | 1cdcf8f3 | Iustin Pop | | unit == "t" || upper == "TIB" = return $ kbBinary * kbBinary |
207 | 1cdcf8f3 | Iustin Pop | -- SI conversions |
208 | 1cdcf8f3 | Iustin Pop | | unit == "M" || upper == "MB" = return mbFactor |
209 | 1cdcf8f3 | Iustin Pop | | unit == "G" || upper == "GB" = return $ mbFactor * kbDecimal |
210 | 1cdcf8f3 | Iustin Pop | | unit == "T" || upper == "TB" = return $ mbFactor * kbDecimal * kbDecimal |
211 | 1cdcf8f3 | Iustin Pop | | otherwise = fail $ "Unknown unit '" ++ unit ++ "'" |
212 | 1cdcf8f3 | Iustin Pop | where upper = map toUpper unit |
213 | 5850e990 | Iustin Pop | kbBinary = 1024 :: Rational |
214 | 5850e990 | Iustin Pop | kbDecimal = 1000 :: Rational |
215 | 1cdcf8f3 | Iustin Pop | decToBin = kbDecimal / kbBinary -- factor for 1K conversion |
216 | 1cdcf8f3 | Iustin Pop | mbFactor = decToBin * decToBin -- twice the factor for just 1K |
217 | 1cdcf8f3 | Iustin Pop | |
218 | 1cb92fac | Iustin Pop | -- | Tries to extract number and scale from the given string. |
219 | 1cb92fac | Iustin Pop | -- |
220 | 1cb92fac | Iustin Pop | -- Input must be in the format NUMBER+ SPACE* [UNIT]. If no unit is |
221 | 1cb92fac | Iustin Pop | -- specified, it defaults to MiB. Return value is always an integral |
222 | 1cb92fac | Iustin Pop | -- value in MiB. |
223 | 1cb92fac | Iustin Pop | parseUnit :: (Monad m, Integral a, Read a) => String -> m a |
224 | 1cb92fac | Iustin Pop | parseUnit str = |
225 | ebf38064 | Iustin Pop | -- TODO: enhance this by splitting the unit parsing code out and |
226 | ebf38064 | Iustin Pop | -- accepting floating-point numbers |
227 | 1cdcf8f3 | Iustin Pop | case (reads str::[(Int, String)]) of |
228 | ebf38064 | Iustin Pop | [(v, suffix)] -> |
229 | ebf38064 | Iustin Pop | let unit = dropWhile (== ' ') suffix |
230 | 1cdcf8f3 | Iustin Pop | in do |
231 | 1cdcf8f3 | Iustin Pop | scaling <- parseUnitValue unit |
232 | 1cdcf8f3 | Iustin Pop | return $ truncate (fromIntegral v * scaling) |
233 | ebf38064 | Iustin Pop | _ -> fail $ "Can't parse string '" ++ str ++ "'" |
234 | 88a10df5 | Iustin Pop | |
235 | 88a10df5 | Iustin Pop | -- | Unwraps a 'Result', exiting the program if it is a 'Bad' value, |
236 | 88a10df5 | Iustin Pop | -- otherwise returning the actual contained value. |
237 | 88a10df5 | Iustin Pop | exitIfBad :: String -> Result a -> IO a |
238 | 707cd3d7 | Helga Velroyen | exitIfBad msg (Bad s) = exitErr (msg ++ ": " ++ s) |
239 | 88a10df5 | Iustin Pop | exitIfBad _ (Ok v) = return v |
240 | 88a10df5 | Iustin Pop | |
241 | 88a10df5 | Iustin Pop | -- | Exits immediately with an error message. |
242 | 88a10df5 | Iustin Pop | exitErr :: String -> IO a |
243 | 88a10df5 | Iustin Pop | exitErr errmsg = do |
244 | 707cd3d7 | Helga Velroyen | hPutStrLn stderr $ "Error: " ++ errmsg |
245 | 88a10df5 | Iustin Pop | exitWith (ExitFailure 1) |
246 | 88a10df5 | Iustin Pop | |
247 | 88a10df5 | Iustin Pop | -- | Exits with an error message if the given boolean condition if true. |
248 | 88a10df5 | Iustin Pop | exitWhen :: Bool -> String -> IO () |
249 | 88a10df5 | Iustin Pop | exitWhen True msg = exitErr msg |
250 | 88a10df5 | Iustin Pop | exitWhen False _ = return () |
251 | 88a10df5 | Iustin Pop | |
252 | 88a10df5 | Iustin Pop | -- | Exits with an error message /unless/ the given boolean condition |
253 | 88a10df5 | Iustin Pop | -- if true, the opposite of 'exitWhen'. |
254 | 88a10df5 | Iustin Pop | exitUnless :: Bool -> String -> IO () |
255 | 88a10df5 | Iustin Pop | exitUnless cond = exitWhen (not cond) |
256 | 04edfc99 | Iustin Pop | |
257 | 838393d1 | Michele Tartara | -- | Unwraps a 'Result', logging a warning message and then returning a default |
258 | 838393d1 | Michele Tartara | -- value if it is a 'Bad' value, otherwise returning the actual contained value. |
259 | 838393d1 | Michele Tartara | logWarningIfBad :: String -> a -> Result a -> IO a |
260 | 838393d1 | Michele Tartara | logWarningIfBad msg defVal (Bad s) = do |
261 | 838393d1 | Michele Tartara | logWarning $ msg ++ ": " ++ s |
262 | 838393d1 | Michele Tartara | return defVal |
263 | 838393d1 | Michele Tartara | logWarningIfBad _ _ (Ok v) = return v |
264 | 838393d1 | Michele Tartara | |
265 | 7dbe4c72 | Klaus Aehlig | -- | Print a warning, but do not exit. |
266 | 7dbe4c72 | Klaus Aehlig | warn :: String -> IO () |
267 | 7dbe4c72 | Klaus Aehlig | warn = hPutStrLn stderr . (++) "Warning: " |
268 | 7dbe4c72 | Klaus Aehlig | |
269 | 04edfc99 | Iustin Pop | -- | Helper for 'niceSort'. Computes the key element for a given string. |
270 | 04edfc99 | Iustin Pop | extractKey :: [Either Integer String] -- ^ Current (partial) key, reversed |
271 | 04edfc99 | Iustin Pop | -> String -- ^ Remaining string |
272 | 04edfc99 | Iustin Pop | -> ([Either Integer String], String) |
273 | 04edfc99 | Iustin Pop | extractKey ek [] = (reverse ek, []) |
274 | 04edfc99 | Iustin Pop | extractKey ek xs@(x:_) = |
275 | 04edfc99 | Iustin Pop | let (span_fn, conv_fn) = if isDigit x |
276 | 04edfc99 | Iustin Pop | then (isDigit, Left . read) |
277 | 04edfc99 | Iustin Pop | else (not . isDigit, Right) |
278 | 04edfc99 | Iustin Pop | (k, rest) = span span_fn xs |
279 | 04edfc99 | Iustin Pop | in extractKey (conv_fn k:ek) rest |
280 | 04edfc99 | Iustin Pop | |
281 | 04edfc99 | Iustin Pop | {-| Sort a list of strings based on digit and non-digit groupings. |
282 | 04edfc99 | Iustin Pop | |
283 | 04edfc99 | Iustin Pop | Given a list of names @['a1', 'a10', 'a11', 'a2']@ this function |
284 | 04edfc99 | Iustin Pop | will sort the list in the logical order @['a1', 'a2', 'a10', 'a11']@. |
285 | 04edfc99 | Iustin Pop | |
286 | 04edfc99 | Iustin Pop | The sort algorithm breaks each name in groups of either only-digits or |
287 | 04edfc99 | Iustin Pop | no-digits, and sorts based on each group. |
288 | 04edfc99 | Iustin Pop | |
289 | 04edfc99 | Iustin Pop | Internally, this is not implemented via regexes (like the Python |
290 | 04edfc99 | Iustin Pop | version), but via actual splitting of the string in sequences of |
291 | 04edfc99 | Iustin Pop | either digits or everything else, and converting the digit sequences |
292 | 04edfc99 | Iustin Pop | in /Left Integer/ and the non-digit ones in /Right String/, at which |
293 | 04edfc99 | Iustin Pop | point sorting becomes trivial due to the built-in 'Either' ordering; |
294 | 04edfc99 | Iustin Pop | we only need one extra step of dropping the key at the end. |
295 | 04edfc99 | Iustin Pop | |
296 | 04edfc99 | Iustin Pop | -} |
297 | 04edfc99 | Iustin Pop | niceSort :: [String] -> [String] |
298 | a7f0953a | Iustin Pop | niceSort = niceSortKey id |
299 | 04edfc99 | Iustin Pop | |
300 | 04edfc99 | Iustin Pop | -- | Key-version of 'niceSort'. We use 'sortBy' and @compare `on` fst@ |
301 | 04edfc99 | Iustin Pop | -- since we don't want to add an ordering constraint on the /a/ type, |
302 | 04edfc99 | Iustin Pop | -- hence the need to only compare the first element of the /(key, a)/ |
303 | 04edfc99 | Iustin Pop | -- tuple. |
304 | 04edfc99 | Iustin Pop | niceSortKey :: (a -> String) -> [a] -> [a] |
305 | 04edfc99 | Iustin Pop | niceSortKey keyfn = |
306 | 04edfc99 | Iustin Pop | map snd . sortBy (compare `on` fst) . |
307 | 04edfc99 | Iustin Pop | map (\s -> (fst . extractKey [] $ keyfn s, s)) |
308 | 256e28c4 | Iustin Pop | |
309 | 256e28c4 | Iustin Pop | -- | Strip space characthers (including newline). As this is |
310 | 256e28c4 | Iustin Pop | -- expensive, should only be run on small strings. |
311 | 256e28c4 | Iustin Pop | rStripSpace :: String -> String |
312 | 256e28c4 | Iustin Pop | rStripSpace = reverse . dropWhile isSpace . reverse |
313 | 80a0546b | Michele Tartara | |
314 | 80a0546b | Michele Tartara | -- | Returns a random UUID. |
315 | 80a0546b | Michele Tartara | -- This is a Linux-specific method as it uses the /proc filesystem. |
316 | 80a0546b | Michele Tartara | newUUID :: IO String |
317 | 80a0546b | Michele Tartara | newUUID = do |
318 | 80a0546b | Michele Tartara | contents <- readFile C.randomUuidFile |
319 | 37dfcacb | Iustin Pop | return $! rStripSpace $ take 128 contents |
320 | b6aeda4a | Dato Simó | |
321 | a6e054a8 | Iustin Pop | -- | Returns the current time as an 'Integer' representing the number |
322 | a6e054a8 | Iustin Pop | -- of seconds from the Unix epoch. |
323 | ace37e24 | Michele Tartara | getCurrentTime :: IO Integer |
324 | ace37e24 | Michele Tartara | getCurrentTime = do |
325 | ace37e24 | Michele Tartara | TOD ctime _ <- getClockTime |
326 | ace37e24 | Michele Tartara | return ctime |
327 | ace37e24 | Michele Tartara | |
328 | a6e054a8 | Iustin Pop | -- | Returns the current time as an 'Integer' representing the number |
329 | a6e054a8 | Iustin Pop | -- of microseconds from the Unix epoch (hence the need for 'Integer'). |
330 | a6e054a8 | Iustin Pop | getCurrentTimeUSec :: IO Integer |
331 | a6e054a8 | Iustin Pop | getCurrentTimeUSec = do |
332 | a6e054a8 | Iustin Pop | TOD ctime pico <- getClockTime |
333 | a6e054a8 | Iustin Pop | -- pico: 10^-12, micro: 10^-6, so we have to shift seconds left and |
334 | a6e054a8 | Iustin Pop | -- picoseconds right |
335 | a6e054a8 | Iustin Pop | return $ ctime * 1000000 + pico `div` 1000000 |
336 | a6e054a8 | Iustin Pop | |
337 | b6aeda4a | Dato Simó | -- | Convert a ClockTime into a (seconds-only) timestamp. |
338 | b6aeda4a | Dato Simó | clockTimeToString :: ClockTime -> String |
339 | b6aeda4a | Dato Simó | clockTimeToString (TOD t _) = show t |
340 | b009f682 | Dato Simó | |
341 | b009f682 | Dato Simó | {-| Strip a prefix from a string, allowing the last character of the prefix |
342 | b009f682 | Dato Simó | (which is assumed to be a separator) to be absent from the string if the string |
343 | b009f682 | Dato Simó | terminates there. |
344 | b009f682 | Dato Simó | |
345 | 94042ae4 | Michele Tartara | \>>> chompPrefix \"foo:bar:\" \"a:b:c\" |
346 | b009f682 | Dato Simó | Nothing |
347 | b009f682 | Dato Simó | |
348 | 94042ae4 | Michele Tartara | \>>> chompPrefix \"foo:bar:\" \"foo:bar:baz\" |
349 | 94042ae4 | Michele Tartara | Just \"baz\" |
350 | b009f682 | Dato Simó | |
351 | 94042ae4 | Michele Tartara | \>>> chompPrefix \"foo:bar:\" \"foo:bar:\" |
352 | 94042ae4 | Michele Tartara | Just \"\" |
353 | b009f682 | Dato Simó | |
354 | 94042ae4 | Michele Tartara | \>>> chompPrefix \"foo:bar:\" \"foo:bar\" |
355 | 94042ae4 | Michele Tartara | Just \"\" |
356 | b009f682 | Dato Simó | |
357 | 94042ae4 | Michele Tartara | \>>> chompPrefix \"foo:bar:\" \"foo:barbaz\" |
358 | b009f682 | Dato Simó | Nothing |
359 | b009f682 | Dato Simó | -} |
360 | b009f682 | Dato Simó | chompPrefix :: String -> String -> Maybe String |
361 | b009f682 | Dato Simó | chompPrefix pfx str = |
362 | b009f682 | Dato Simó | if pfx `isPrefixOf` str || str == init pfx |
363 | b009f682 | Dato Simó | then Just $ drop (length pfx) str |
364 | b009f682 | Dato Simó | else Nothing |
365 | 9fb621af | Yiannis Tsiouris | |
366 | 9fb621af | Yiannis Tsiouris | -- | Breaks a string in lines with length \<= maxWidth. |
367 | 9fb621af | Yiannis Tsiouris | -- |
368 | 9fb621af | Yiannis Tsiouris | -- NOTE: The split is OK if: |
369 | 9fb621af | Yiannis Tsiouris | -- |
370 | 9fb621af | Yiannis Tsiouris | -- * It doesn't break a word, i.e. the next line begins with space |
371 | 9fb621af | Yiannis Tsiouris | -- (@isSpace . head $ rest@) or the current line ends with space |
372 | 9fb621af | Yiannis Tsiouris | -- (@null revExtra@); |
373 | 9fb621af | Yiannis Tsiouris | -- |
374 | 9fb621af | Yiannis Tsiouris | -- * It breaks a very big word that doesn't fit anyway (@null revLine@). |
375 | 9fb621af | Yiannis Tsiouris | wrap :: Int -- ^ maxWidth |
376 | 9fb621af | Yiannis Tsiouris | -> String -- ^ string that needs wrapping |
377 | 9fb621af | Yiannis Tsiouris | -> [String] -- ^ string \"broken\" in lines |
378 | 9fb621af | Yiannis Tsiouris | wrap maxWidth = filter (not . null) . map trim . wrap0 |
379 | 9fb621af | Yiannis Tsiouris | where wrap0 :: String -> [String] |
380 | 9fb621af | Yiannis Tsiouris | wrap0 text |
381 | 9fb621af | Yiannis Tsiouris | | length text <= maxWidth = [text] |
382 | 9fb621af | Yiannis Tsiouris | | isSplitOK = line : wrap0 rest |
383 | 9fb621af | Yiannis Tsiouris | | otherwise = line' : wrap0 rest' |
384 | 9fb621af | Yiannis Tsiouris | where (line, rest) = splitAt maxWidth text |
385 | 9fb621af | Yiannis Tsiouris | (revExtra, revLine) = break isSpace . reverse $ line |
386 | 9fb621af | Yiannis Tsiouris | (line', rest') = (reverse revLine, reverse revExtra ++ rest) |
387 | 9fb621af | Yiannis Tsiouris | isSplitOK = |
388 | 9fb621af | Yiannis Tsiouris | null revLine || null revExtra || startsWithSpace rest |
389 | 9fb621af | Yiannis Tsiouris | startsWithSpace (x:_) = isSpace x |
390 | 9fb621af | Yiannis Tsiouris | startsWithSpace _ = False |
391 | 9fb621af | Yiannis Tsiouris | |
392 | 9fb621af | Yiannis Tsiouris | -- | Removes surrounding whitespace. Should only be used in small |
393 | 9fb621af | Yiannis Tsiouris | -- strings. |
394 | 9fb621af | Yiannis Tsiouris | trim :: String -> String |
395 | 9fb621af | Yiannis Tsiouris | trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace |
396 | 72747d91 | Iustin Pop | |
397 | 72747d91 | Iustin Pop | -- | A safer head version, with a default value. |
398 | 72747d91 | Iustin Pop | defaultHead :: a -> [a] -> a |
399 | 72747d91 | Iustin Pop | defaultHead def [] = def |
400 | 72747d91 | Iustin Pop | defaultHead _ (x:_) = x |
401 | 72747d91 | Iustin Pop | |
402 | 72747d91 | Iustin Pop | -- | A 'head' version in the I/O monad, for validating parameters |
403 | 72747d91 | Iustin Pop | -- without which we cannot continue. |
404 | 72747d91 | Iustin Pop | exitIfEmpty :: String -> [a] -> IO a |
405 | 72747d91 | Iustin Pop | exitIfEmpty _ (x:_) = return x |
406 | 72747d91 | Iustin Pop | exitIfEmpty s [] = exitErr s |
407 | da9e2aff | Iustin Pop | |
408 | da9e2aff | Iustin Pop | -- | Split an 'Either' list into two separate lists (containing the |
409 | da9e2aff | Iustin Pop | -- 'Left' and 'Right' elements, plus a \"trail\" list that allows |
410 | da9e2aff | Iustin Pop | -- recombination later. |
411 | da9e2aff | Iustin Pop | -- |
412 | da9e2aff | Iustin Pop | -- This is splitter; for recombination, look at 'recombineEithers'. |
413 | da9e2aff | Iustin Pop | -- The sum of \"left\" and \"right\" lists should be equal to the |
414 | da9e2aff | Iustin Pop | -- original list length, and the trail list should be the same length |
415 | da9e2aff | Iustin Pop | -- as well. The entries in the resulting lists are reversed in |
416 | da9e2aff | Iustin Pop | -- comparison with the original list. |
417 | da9e2aff | Iustin Pop | splitEithers :: [Either a b] -> ([a], [b], [Bool]) |
418 | da9e2aff | Iustin Pop | splitEithers = foldl' splitter ([], [], []) |
419 | da9e2aff | Iustin Pop | where splitter (l, r, t) e = |
420 | da9e2aff | Iustin Pop | case e of |
421 | da9e2aff | Iustin Pop | Left v -> (v:l, r, False:t) |
422 | da9e2aff | Iustin Pop | Right v -> (l, v:r, True:t) |
423 | da9e2aff | Iustin Pop | |
424 | da9e2aff | Iustin Pop | -- | Recombines two \"left\" and \"right\" lists using a \"trail\" |
425 | da9e2aff | Iustin Pop | -- list into a single 'Either' list. |
426 | da9e2aff | Iustin Pop | -- |
427 | da9e2aff | Iustin Pop | -- This is the counterpart to 'splitEithers'. It does the opposite |
428 | da9e2aff | Iustin Pop | -- transformation, and the output list will be the reverse of the |
429 | da9e2aff | Iustin Pop | -- input lists. Since 'splitEithers' also reverses the lists, calling |
430 | da9e2aff | Iustin Pop | -- these together will result in the original list. |
431 | da9e2aff | Iustin Pop | -- |
432 | da9e2aff | Iustin Pop | -- Mismatches in the structure of the lists (e.g. inconsistent |
433 | da9e2aff | Iustin Pop | -- lengths) are represented via 'Bad'; normally this function should |
434 | da9e2aff | Iustin Pop | -- not fail, if lists are passed as generated by 'splitEithers'. |
435 | da9e2aff | Iustin Pop | recombineEithers :: (Show a, Show b) => |
436 | da9e2aff | Iustin Pop | [a] -> [b] -> [Bool] -> Result [Either a b] |
437 | da9e2aff | Iustin Pop | recombineEithers lefts rights trail = |
438 | da9e2aff | Iustin Pop | foldM recombiner ([], lefts, rights) trail >>= checker |
439 | da9e2aff | Iustin Pop | where checker (eithers, [], []) = Ok eithers |
440 | da9e2aff | Iustin Pop | checker (_, lefts', rights') = |
441 | da9e2aff | Iustin Pop | Bad $ "Inconsistent results after recombination, l'=" ++ |
442 | da9e2aff | Iustin Pop | show lefts' ++ ", r'=" ++ show rights' |
443 | da9e2aff | Iustin Pop | recombiner (es, l:ls, rs) False = Ok (Left l:es, ls, rs) |
444 | da9e2aff | Iustin Pop | recombiner (es, ls, r:rs) True = Ok (Right r:es, ls, rs) |
445 | da9e2aff | Iustin Pop | recombiner (_, ls, rs) t = Bad $ "Inconsistent trail log: l=" ++ |
446 | da9e2aff | Iustin Pop | show ls ++ ", r=" ++ show rs ++ ",t=" ++ |
447 | da9e2aff | Iustin Pop | show t |
448 | a39cd547 | Michele Tartara | |
449 | a39cd547 | Michele Tartara | -- | Set the owner and the group of a file (given as names, not numeric id). |
450 | a39cd547 | Michele Tartara | setOwnerAndGroupFromNames :: FilePath -> GanetiDaemon -> GanetiGroup -> IO () |
451 | a39cd547 | Michele Tartara | setOwnerAndGroupFromNames filename daemon dGroup = do |
452 | a39cd547 | Michele Tartara | -- TODO: it would be nice to rework this (or getEnts) so that runtimeEnts |
453 | a39cd547 | Michele Tartara | -- is read only once per daemon startup, and then cached for further usage. |
454 | a39cd547 | Michele Tartara | runtimeEnts <- getEnts |
455 | a39cd547 | Michele Tartara | ents <- exitIfBad "Can't find required user/groups" runtimeEnts |
456 | a39cd547 | Michele Tartara | -- note: we use directly ! as lookup failures shouldn't happen, due |
457 | a39cd547 | Michele Tartara | -- to the map construction |
458 | a39cd547 | Michele Tartara | let uid = fst ents M.! daemon |
459 | a39cd547 | Michele Tartara | let gid = snd ents M.! dGroup |
460 | a39cd547 | Michele Tartara | setOwnerAndGroup filename uid gid |