/
Dpkg.hs
84 lines (74 loc) · 3.23 KB
/
Dpkg.hs
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
{- arch-tag: dpkg querying
Copyright (C) 2004 John Goerzen <jgoerzen@complete.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
module Dpkg where
-- Interface to Dpkg/Apt
import System.Cmd.Utils
import System.Debian.ControlParser
import Data.Maybe.Utils
import System.Log.Logger
import Data.String.Utils
import Text.Regex
import Utils
-- | Gets the installed version of a package, if any
getInstalledVer :: String -> IO (Maybe String)
getInstalledVer package =
do d <- readdata $ "dpkg -s " ++ package
case d of
Nothing -> return Nothing
Just x -> return $
if endswith "installed" $ forceMaybe $ lookup "Status" (parseControl x)
then lookup "Version" (parseControl x)
else Nothing
-- | Gets the available version of a package, if any
getAvailableVer :: String -> IO (Maybe String)
getAvailableVer package =
do d <- readdata $ "apt-cache showsrc " ++ package
case d of
Nothing -> return Nothing
Just x -> return $ lookup "Version" (parseControl x)
-- | Gets the build-deps of a package, if any
getBuildDeps :: String -> IO [String]
getBuildDeps package =
do d <- readdata $ "apt-cache showsrc " ++ package
case d of
Nothing -> return []
Just x -> let parsed = parseControl x
splitted y = map strip . split "," . filter (/= ' ')
$ y
find y = case lookup y parsed of
Nothing -> []
Just z -> splitted z
in
return $ find "Build-Depends" ++
find "Build-Depends-Indep"
pkgVerToFilename :: String -> String -> String
pkgVerToFilename package version =
let newvers = subRegex (mkRegex "^[0-9]+:") version ""
in
package ++ "_" ++ newvers ++ "_*.deb"
-- | Gets the deps for a .deb, if any
getDebDeps :: String -> String -> IO [String]
getDebDeps package version =
do d <- readdata $ "dpkg -f " ++ (pkgVerToFilename package version)
case d of
Nothing -> return []
Just x -> let parsed = parseControl x
splitted y = map strip . split "," .
filter (/= ' ')$ y
find y = case lookup y parsed of
Nothing -> []
Just z -> splitted z
in
return $ find "Depends"