PackageIndex.hs 19.8 KB
Newer Older
1
2
-----------------------------------------------------------------------------
-- |
3
-- Module      :  Distribution.Simple.PackageIndex
4
5
-- Copyright   :  (c) David Himmelstrup 2005,
--                    Bjorn Bringert 2007,
Duncan Coutts's avatar
Duncan Coutts committed
6
--                    Duncan Coutts 2008-2009
7
--
Duncan Coutts's avatar
Duncan Coutts committed
8
-- Maintainer  :  cabal-devel@haskell.org
9
10
-- Portability :  portable
--
11
-- An index of packages.
Duncan Coutts's avatar
Duncan Coutts committed
12
--
13
14
15
module Distribution.Simple.PackageIndex (
  -- * Package index data type
  PackageIndex,
16

17
  -- * Creating an index
18
19
  fromList,

20
  -- * Updates
21
  merge,
Duncan Coutts's avatar
Duncan Coutts committed
22

23
  insert,
Duncan Coutts's avatar
Duncan Coutts committed
24
25
26

  deleteInstalledPackageId,
  deleteSourcePackageId,
27
  deletePackageName,
Duncan Coutts's avatar
Duncan Coutts committed
28
--  deleteDependency,
29
30
31
32

  -- * Queries

  -- ** Precise lookups
Duncan Coutts's avatar
Duncan Coutts committed
33
34
  lookupInstalledPackageId,
  lookupSourcePackageId,
35
  lookupPackageName,
36
37
38
39
40
41
42
43
44
45
  lookupDependency,

  -- ** Case-insensitive searches
  searchByName,
  SearchResult(..),
  searchByNameSubstring,

  -- ** Bulk queries
  allPackages,
  allPackagesByName,
46
  allPackagesBySourcePackageId,
47
48
49
50
51
52
53
54
55
56

  -- ** Special queries
  brokenPackages,
  dependencyClosure,
  reverseDependencyClosure,
  topologicalOrder,
  reverseTopologicalOrder,
  dependencyInconsistencies,
  dependencyCycles,
  dependencyGraph,
Duncan Coutts's avatar
Duncan Coutts committed
57
  moduleNameIndex,
58
59
60
61
62
63
  ) where

import Prelude hiding (lookup)
import Control.Exception (assert)
import qualified Data.Map as Map
import Data.Map (Map)
64
65
66
67
import qualified Data.Tree  as Tree
import qualified Data.Graph as Graph
import qualified Data.Array as Array
import Data.Array ((!))
Duncan Coutts's avatar
Duncan Coutts committed
68
import Data.List as List
69
         ( null, foldl', sort
Duncan Coutts's avatar
Duncan Coutts committed
70
         , groupBy, sortBy, find, isInfixOf, nubBy, deleteBy, deleteFirstsBy )
71
import Data.Monoid (Monoid(..))
72
import Data.Maybe (isNothing, fromMaybe)
73

74
import Distribution.Package
Duncan Coutts's avatar
Duncan Coutts committed
75
         ( PackageName(..), PackageId
76
         , Package(..), packageName, packageVersion
Duncan Coutts's avatar
Duncan Coutts committed
77
         , Dependency(Dependency)--, --PackageFixedDeps(..)
78
         , InstalledPackageId(..) )
Duncan Coutts's avatar
Duncan Coutts committed
79
80
import Distribution.ModuleName
         ( ModuleName )
81
import Distribution.InstalledPackageInfo
Duncan Coutts's avatar
Duncan Coutts committed
82
         ( InstalledPackageInfo, installedPackageId )
83
import qualified Distribution.InstalledPackageInfo as IPI
84
import Distribution.Version
85
         ( Version, withinRange )
Duncan Coutts's avatar
Duncan Coutts committed
86
import Distribution.Simple.Utils (lowercase, comparing, equating)
87

88

89
90
-- | The collection of information about packages from one or more 'PackageDB's.
--
Duncan Coutts's avatar
Duncan Coutts committed
91
92
-- Packages are uniquely identified in by their 'InstalledPackageId', they can
-- also be effeciently looked up by package name or by name and version.
93
--
Duncan Coutts's avatar
Duncan Coutts committed
94
95
96
97
98
99
100
101
102
data PackageIndex = PackageIndex
  -- The primary index. Each InstalledPackageInfo record is uniquely identified
  -- by its InstalledPackageId.
  --
  !(Map InstalledPackageId InstalledPackageInfo)

  -- This auxillary index maps package names (case-sensitively) to all the
  -- versions and instances of that package. This allows us to find all
  -- versions satisfying a dependency.
103
  --
Duncan Coutts's avatar
Duncan Coutts committed
104
105
106
107
  -- It is a three-level index. The first level is the package name,
  -- the second is the package version and the final level is instances
  -- of the same package version. These are unique by InstalledPackageId
  -- and are kept in preference order.
108
  --
Duncan Coutts's avatar
Duncan Coutts committed
109
  !(Map PackageName (Map Version [InstalledPackageInfo]))
110
111

  deriving (Show, Read)
Duncan Coutts's avatar
Duncan Coutts committed
112
113
114

instance Monoid PackageIndex where
  mempty  = PackageIndex Map.empty Map.empty
115
116
117
118
119
  mappend = merge
  --save one mappend with empty in the common case:
  mconcat [] = mempty
  mconcat xs = foldr1 mappend xs

Duncan Coutts's avatar
Duncan Coutts committed
120
121
122
invariant :: PackageIndex -> Bool
invariant (PackageIndex pids pnames) =
     map installedPackageId (Map.elems pids)
123
124
  == sort
     [ assert pinstOk (installedPackageId pinst)
Duncan Coutts's avatar
Duncan Coutts committed
125
126
127
128
129
130
131
132
133
134
135
     | (pname, pvers)  <- Map.toList pnames
     , let pversOk = not (Map.null pvers)
     , (pver,  pinsts) <- assert pversOk $ Map.toList pvers
     , let pinsts'  = sortBy (comparing installedPackageId) pinsts
           pinstsOk = all (\g -> length g == 1)
                          (groupBy (equating installedPackageId) pinsts')
     , pinst           <- assert pinstsOk $ pinsts'
     , let pinstOk = packageName    pinst == pname
                  && packageVersion pinst == pver
     ]

136

137
138
139
140
--
-- * Internal helpers
--

Duncan Coutts's avatar
Duncan Coutts committed
141
142
143
144
145
mkPackageIndex :: Map InstalledPackageId InstalledPackageInfo
               -> Map PackageName (Map Version [InstalledPackageInfo])
               -> PackageIndex
mkPackageIndex pids pnames = assert (invariant index) index
  where index = PackageIndex pids pnames
146

147
148
149

--
-- * Construction
150
151
--

152
-- | Build an index out of a bunch of packages.
153
--
Duncan Coutts's avatar
Duncan Coutts committed
154
155
-- If there are duplicates by 'InstalledPackageId' then later ones mask earlier
-- ones.
156
--
Duncan Coutts's avatar
Duncan Coutts committed
157
158
fromList :: [InstalledPackageInfo] -> PackageIndex
fromList pkgs = mkPackageIndex pids pnames
159
  where
Duncan Coutts's avatar
Duncan Coutts committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    pids      = Map.fromList [ (installedPackageId pkg, pkg) | pkg <- pkgs ]
    pnames    =
      Map.fromList
        [ (packageName (head pkgsN), pvers)
        | pkgsN <- groupBy (equating  packageName)
                 . sortBy  (comparing packageId)
                 $ pkgs
        , let pvers =
                Map.fromList
                [ (packageVersion (head pkgsNV),
                   nubBy (equating installedPackageId) (reverse pkgsNV))
                | pkgsNV <- groupBy (equating packageVersion) pkgsN
                ]
        ]
174

175
176
177
178
--
-- * Updates
--

179
180
-- | Merge two indexes.
--
Duncan Coutts's avatar
Duncan Coutts committed
181
182
-- Packages from the second mask packages from the first if they have the exact
-- same 'InstalledPackageId'.
183
--
Duncan Coutts's avatar
Duncan Coutts committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
-- For packages with the same source 'PackageId', packages from the second are
-- \"preferred\" over those from the first. Being preferred means they are top
-- result when we do a lookup by source 'PackageId'. This is the mechanism we
-- use to prefer user packages over global packages.
--
merge :: PackageIndex -> PackageIndex -> PackageIndex
merge (PackageIndex pids1 pnames1) (PackageIndex pids2 pnames2) =
  mkPackageIndex (Map.union pids1 pids2)
                 (Map.unionWith (Map.unionWith mergeBuckets) pnames1 pnames2)
  where
    -- Packages in the second list mask those in the first, however preferred
    -- packages go first in the list.
    mergeBuckets xs ys = ys ++ (xs \\ ys)
    (\\) = deleteFirstsBy (equating installedPackageId)
198

199

Duncan Coutts's avatar
Duncan Coutts committed
200
-- | Inserts a single package into the index.
201
202
203
204
--
-- This is equivalent to (but slightly quicker than) using 'mappend' or
-- 'merge' with a singleton index.
--
Duncan Coutts's avatar
Duncan Coutts committed
205
206
207
208
insert :: InstalledPackageInfo -> PackageIndex -> PackageIndex
insert pkg (PackageIndex pids pnames) =
    mkPackageIndex pids' pnames'

209
  where
Duncan Coutts's avatar
Duncan Coutts committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    pids'   = Map.insert (installedPackageId pkg) pkg pids
    pnames' = insertPackageName pnames
    insertPackageName =
      Map.insertWith' (\_ -> insertPackageVersion)
                     (packageName pkg)
                     (Map.singleton (packageVersion pkg) [pkg])

    insertPackageVersion =
      Map.insertWith' (\_ -> insertPackageInstance)
                     (packageVersion pkg) [pkg]

    insertPackageInstance pkgs =
      pkg : deleteBy (equating installedPackageId) pkg pkgs


-- | Removes a single installed package from the index.
--
deleteInstalledPackageId :: InstalledPackageId -> PackageIndex -> PackageIndex
deleteInstalledPackageId ipkgid original@(PackageIndex pids pnames) =
  case Map.updateLookupWithKey (\_ _ -> Nothing) ipkgid pids of
    (Nothing,     _)     -> original
    (Just spkgid, pids') -> mkPackageIndex pids'
                                          (deletePkgName spkgid pnames)

Duncan Coutts's avatar
Duncan Coutts committed
234
  where
Duncan Coutts's avatar
Duncan Coutts committed
235
236
237
238
239
240
    deletePkgName spkgid =
      Map.update (deletePkgVersion spkgid) (packageName spkgid)

    deletePkgVersion spkgid =
        (\m -> if Map.null m then Nothing else Just m)
      . Map.update deletePkgInstance (packageVersion spkgid)
Duncan Coutts's avatar
Duncan Coutts committed
241

Duncan Coutts's avatar
Duncan Coutts committed
242
243
244
245
246
247
    deletePkgInstance =
        (\xs -> if List.null xs then Nothing else Just xs)
      . List.deleteBy (\_ pkg -> installedPackageId pkg == ipkgid) undefined


-- | Removes all packages with this source 'PackageId' from the index.
248
--
Duncan Coutts's avatar
Duncan Coutts committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
deleteSourcePackageId :: PackageId -> PackageIndex -> PackageIndex
deleteSourcePackageId pkgid original@(PackageIndex pids pnames) =
  case Map.lookup (packageName pkgid) pnames of
    Nothing     -> original
    Just pvers  -> case Map.lookup (packageVersion pkgid) pvers of
      Nothing   -> original
      Just pkgs -> mkPackageIndex
                     (foldl' (flip (Map.delete . installedPackageId)) pids pkgs)
                     (deletePkgName pnames)
  where
    deletePkgName =
      Map.update deletePkgVersion (packageName pkgid)

    deletePkgVersion =
        (\m -> if Map.null m then Nothing else Just m)
      . Map.delete (packageVersion pkgid)

266
267
268

-- | Removes all packages with this (case-sensitive) name from the index.
--
Duncan Coutts's avatar
Duncan Coutts committed
269
270
271
272
273
274
275
276
deletePackageName :: PackageName -> PackageIndex -> PackageIndex
deletePackageName name original@(PackageIndex pids pnames) =
  case Map.lookup name pnames of
    Nothing     -> original
    Just pvers  -> mkPackageIndex
                     (foldl' (flip (Map.delete . installedPackageId)) pids
                             (concat (Map.elems pvers)))
                     (Map.delete name pnames)
277

Duncan Coutts's avatar
Duncan Coutts committed
278
{-
279
280
-- | Removes all packages satisfying this dependency from the index.
--
Duncan Coutts's avatar
Duncan Coutts committed
281
deleteDependency :: Dependency -> PackageIndex -> PackageIndex
282
deleteDependency (Dependency name verstionRange) =
Duncan Coutts's avatar
Duncan Coutts committed
283
284
  delete' name (\pkg -> packageVersion pkg `withinRange` verstionRange)
-}
285

286
287
288
289
--
-- * Bulk queries
--

290
291
-- | Get all the packages from the index.
--
Duncan Coutts's avatar
Duncan Coutts committed
292
293
allPackages :: PackageIndex -> [InstalledPackageInfo]
allPackages (PackageIndex pids _) = Map.elems pids
294
295
296

-- | Get all the packages from the index.
--
297
-- They are grouped by package name (case-sensitively).
298
--
299
allPackagesByName :: PackageIndex -> [(PackageName, [InstalledPackageInfo])]
Duncan Coutts's avatar
Duncan Coutts committed
300
allPackagesByName (PackageIndex _ pnames) =
301
302
303
304
305
306
307
308
309
310
311
312
  [ (pkgname, concat (Map.elems pvers))
  | (pkgname, pvers) <- Map.toList pnames ]

-- | Get all the packages from the index.
--
-- They are grouped by source package id (package name and version).
--
allPackagesBySourcePackageId :: PackageIndex -> [(PackageId, [InstalledPackageInfo])]
allPackagesBySourcePackageId (PackageIndex _ pnames) =
  [ (packageId ipkg, ipkgs)
  | pvers <- Map.elems pnames
  , ipkgs@(ipkg:_) <- Map.elems pvers ]
313
314
315
316
317

--
-- * Lookups
--

Duncan Coutts's avatar
Duncan Coutts committed
318
-- | Does a lookup by source package id (name & version).
319
--
Duncan Coutts's avatar
Duncan Coutts committed
320
-- Since multiple package DBs mask each other by 'InstalledPackageId',
321
322
-- then we get back at most one package.
--
Duncan Coutts's avatar
Duncan Coutts committed
323
324
325
326
lookupInstalledPackageId :: PackageIndex -> InstalledPackageId
                         -> Maybe InstalledPackageInfo
lookupInstalledPackageId (PackageIndex pids _) pid = Map.lookup pid pids

327

Duncan Coutts's avatar
Duncan Coutts committed
328
329
330
331
332
-- | Does a lookup by source package id (name & version).
--
-- There can be multiple installed packages with the same source 'PackageId'
-- but different 'InstalledPackageId'. They are returned in order of
-- preference, with the most preferred first.
333
--
Duncan Coutts's avatar
Duncan Coutts committed
334
335
336
337
338
339
340
lookupSourcePackageId :: PackageIndex -> PackageId -> [InstalledPackageInfo]
lookupSourcePackageId (PackageIndex _ pnames) pkgid =
  case Map.lookup (packageName pkgid) pnames of
    Nothing     -> []
    Just pvers  -> case Map.lookup (packageVersion pkgid) pvers of
      Nothing   -> []
      Just pkgs -> pkgs -- in preference order
341

Duncan Coutts's avatar
Duncan Coutts committed
342
343
344
345
346
347
348
349
350
351
352
353

-- | Does a lookup by source package name.
--
lookupPackageName :: PackageIndex -> PackageName
                  -> [(Version, [InstalledPackageInfo])]
lookupPackageName (PackageIndex _ pnames) name =
  case Map.lookup name pnames of
    Nothing     -> []
    Just pvers  -> Map.toList pvers


-- | Does a lookup by source package name and a range of versions.
354
355
356
357
--
-- We get back any number of versions of the specified package name, all
-- satisfying the version range constraint.
--
Duncan Coutts's avatar
Duncan Coutts committed
358
359
360
361
362
363
364
365
lookupDependency :: PackageIndex -> Dependency
                 -> [(Version, [InstalledPackageInfo])]
lookupDependency (PackageIndex _ pnames) (Dependency name versionRange) =
  case Map.lookup name pnames of
    Nothing    -> []
    Just pvers -> [ entry
                  | entry@(ver, _) <- Map.toList pvers
                  , ver `withinRange` versionRange ]
366
367
368
369

--
-- * Case insensitive name lookups
--
370
371
372
373
374
375
376
377
378
379
380
381
382

-- | Does a case-insensitive search by package name.
--
-- If there is only one package that compares case-insentiviely to this name
-- then the search is unambiguous and we get back all versions of that package.
-- If several match case-insentiviely but one matches exactly then it is also
-- unambiguous.
--
-- If however several match case-insentiviely and none match exactly then we
-- have an ambiguous result, and we get back all the versions of all the
-- packages. The list of ambiguous results is split by exact package name. So
-- it is a non-empty list of non-empty lists.
--
Duncan Coutts's avatar
Duncan Coutts committed
383
384
385
searchByName :: PackageIndex -> String -> SearchResult [InstalledPackageInfo]
searchByName (PackageIndex _ pnames) name =
  case [ pkgs | pkgs@(PackageName name',_) <- Map.toList pnames
386
              , lowercase name' == lname ] of
Duncan Coutts's avatar
Duncan Coutts committed
387
388
389
390
391
    []               -> None
    [(_,pvers)]      -> Unambiguous (concat (Map.elems pvers))
    pkgss            -> case find ((PackageName name==) . fst) pkgss of
      Just (_,pvers) -> Unambiguous (concat (Map.elems pvers))
      Nothing        -> Ambiguous (map (concat . Map.elems . snd) pkgss)
392
  where lname = lowercase name
393
394
395
396
397
398
399

data SearchResult a = None | Unambiguous a | Ambiguous [a]

-- | Does a case-insensitive substring search by package name.
--
-- That is, all packages that contain the given string in their name.
--
Duncan Coutts's avatar
Duncan Coutts committed
400
401
searchByNameSubstring :: PackageIndex -> String -> [InstalledPackageInfo]
searchByNameSubstring (PackageIndex _ pnames) searchterm =
402
  [ pkg
Duncan Coutts's avatar
Duncan Coutts committed
403
  | (PackageName name, pvers) <- Map.toList pnames
404
  , lsearchterm `isInfixOf` lowercase name
Duncan Coutts's avatar
Duncan Coutts committed
405
  , pkgs <- Map.elems pvers
406
  , pkg <- pkgs ]
407
  where lsearchterm = lowercase searchterm
408

Duncan Coutts's avatar
Duncan Coutts committed
409
410
411
412
413
414
415
416

--
-- * Special queries
--

-- None of the stuff below depends on the internal representation of the index.
--

417
418
419
420
421
422
423
-- | Find if there are any cycles in the dependency graph. If there are no
-- cycles the result is @[]@.
--
-- This actually computes the strongly connected components. So it gives us a
-- list of groups of packages where within each group they all depend on each
-- other, directly or indirectly.
--
Duncan Coutts's avatar
Duncan Coutts committed
424
dependencyCycles :: PackageIndex -> [[InstalledPackageInfo]]
425
426
427
dependencyCycles index =
  [ vs | Graph.CyclicSCC vs <- Graph.stronglyConnComp adjacencyList ]
  where
Duncan Coutts's avatar
Duncan Coutts committed
428
    adjacencyList = [ (pkg, installedPackageId pkg, IPI.depends pkg)
429
430
431
                    | pkg <- allPackages index ]


Duncan Coutts's avatar
Duncan Coutts committed
432
-- | All packages that have immediate dependencies that are not in the index.
433
--
434
435
-- Returns such packages along with the dependencies that they're missing.
--
Duncan Coutts's avatar
Duncan Coutts committed
436
brokenPackages :: PackageIndex -> [(InstalledPackageInfo, [InstalledPackageId])]
437
438
brokenPackages index =
  [ (pkg, missing)
Duncan Coutts's avatar
Duncan Coutts committed
439
  | pkg  <- allPackages index
440
  , let missing = [ pkg' | pkg' <- IPI.depends pkg
Duncan Coutts's avatar
Duncan Coutts committed
441
                         , isNothing (lookupInstalledPackageId index pkg') ]
442
443
  , not (null missing) ]

444

445
-- | Tries to take the transitive closure of the package dependencies.
446
--
447
-- If the transitive closure is complete then it returns that subset of the
448
449
450
-- index. Otherwise it returns the broken packages as in 'brokenPackages'.
--
-- * Note that if the result is @Right []@ it is because at least one of
Duncan Coutts's avatar
Duncan Coutts committed
451
-- the original given 'PackageId's do not occur in the index.
452
--
Duncan Coutts's avatar
Duncan Coutts committed
453
dependencyClosure :: PackageIndex
454
                  -> [InstalledPackageId]
Duncan Coutts's avatar
Duncan Coutts committed
455
                  -> Either PackageIndex
456
                            [(InstalledPackageInfo, [InstalledPackageId])]
457
458
459
dependencyClosure index pkgids0 = case closure mempty [] pkgids0 of
  (completed, []) -> Left completed
  (completed, _)  -> Right (brokenPackages completed)
460
 where
461
    closure completed failed []             = (completed, failed)
Duncan Coutts's avatar
Duncan Coutts committed
462
    closure completed failed (pkgid:pkgids) = case lookupInstalledPackageId index pkgid of
463
      Nothing   -> closure completed (pkgid:failed) pkgids
Duncan Coutts's avatar
Duncan Coutts committed
464
      Just pkg  -> case lookupInstalledPackageId completed (installedPackageId pkg) of
465
466
        Just _  -> closure completed  failed pkgids
        Nothing -> closure completed' failed pkgids'
Duncan Coutts's avatar
Duncan Coutts committed
467
          where completed' = insert pkg completed
468
                pkgids'    = IPI.depends pkg ++ pkgids
469

470
-- | Takes the transitive closure of the packages reverse dependencies.
471
--
Duncan Coutts's avatar
Duncan Coutts committed
472
-- * The given 'PackageId's must be in the index.
473
--
Duncan Coutts's avatar
Duncan Coutts committed
474
reverseDependencyClosure :: PackageIndex
475
476
                         -> [InstalledPackageId]
                         -> [InstalledPackageInfo]
477
478
479
480
481
reverseDependencyClosure index =
    map vertexToPkg
  . concatMap Tree.flatten
  . Graph.dfs reverseDepGraph
  . map (fromMaybe noSuchPkgId . pkgIdToVertex)
482

483
484
485
486
487
  where
    (depGraph, vertexToPkg, pkgIdToVertex) = dependencyGraph index
    reverseDepGraph = Graph.transposeG depGraph
    noSuchPkgId = error "reverseDependencyClosure: package is not in the graph"

Duncan Coutts's avatar
Duncan Coutts committed
488
topologicalOrder :: PackageIndex -> [InstalledPackageInfo]
489
490
491
492
493
topologicalOrder index = map toPkgId
                       . Graph.topSort
                       $ graph
  where (graph, toPkgId, _) = dependencyGraph index

Duncan Coutts's avatar
Duncan Coutts committed
494
reverseTopologicalOrder :: PackageIndex -> [InstalledPackageInfo]
495
496
497
498
499
500
501
502
503
504
505
reverseTopologicalOrder index = map toPkgId
                              . Graph.topSort
                              . Graph.transposeG
                              $ graph
  where (graph, toPkgId, _) = dependencyGraph index

-- | Builds a graph of the package dependencies.
--
-- Dependencies on other packages that are not in the index are discarded.
-- You can check if there are any such dependencies with 'brokenPackages'.
--
Duncan Coutts's avatar
Duncan Coutts committed
506
dependencyGraph :: PackageIndex
507
                -> (Graph.Graph,
508
509
510
                    Graph.Vertex -> InstalledPackageInfo,
                    InstalledPackageId -> Maybe Graph.Vertex)
dependencyGraph index = (graph, vertex_to_pkg, id_to_vertex)
511
512
  where
    graph = Array.listArray bounds
513
              [ [ v | Just v <- map id_to_vertex (IPI.depends pkg) ]
514
              | pkg <- pkgs ]
515

Duncan Coutts's avatar
Duncan Coutts committed
516
    pkgs             = sortBy (comparing packageId) (allPackages index)
517
518
519
520
521
    vertices         = zip (map installedPackageId pkgs) [0..]
    vertex_map       = Map.fromList vertices
    id_to_vertex pid = Map.lookup pid vertex_map

    vertex_to_pkg vertex = pkgTable ! vertex
522
523
524
525
526

    pkgTable   = Array.listArray bounds pkgs
    topBound = length pkgs - 1
    bounds = (0, topBound)

527
528
529
530
531
532
533
534
535
536
-- | Given a package index where we assume we want to use all the packages
-- (use 'dependencyClosure' if you need to get such a index subset) find out
-- if the dependencies within it use consistent versions of each package.
-- Return all cases where multiple packages depend on different versions of
-- some other package.
--
-- Each element in the result is a package name along with the packages that
-- depend on it and the versions they require. These are guaranteed to be
-- distinct.
--
Duncan Coutts's avatar
Duncan Coutts committed
537
538
dependencyInconsistencies :: PackageIndex
                          -> [(PackageName, [(PackageId, Version)])]
539
540
541
542
543
544
545
546
547
548
549
550
dependencyInconsistencies index =
  [ (name, [ (pid,packageVersion dep) | (dep,pids) <- uses, pid <- pids])
  | (name, ipid_map) <- Map.toList inverseIndex
  , let uses = Map.elems ipid_map
  , reallyIsInconsistent (map fst uses) ]

  where -- for each PackageName,
        --   for each package with that name,
        --     the InstalledPackageInfo and the package Ids of packages
        --     that depend on it.
        inverseIndex :: Map PackageName
                            (Map InstalledPackageId
Duncan Coutts's avatar
Duncan Coutts committed
551
                                 (InstalledPackageInfo, [PackageId]))
552
553
554
        inverseIndex = Map.fromListWith (Map.unionWith (\(a,b) (_,b') -> (a,b++b')))
          [ (packageName dep,
             Map.fromList [(ipid,(dep,[packageId pkg]))])
Duncan Coutts's avatar
Duncan Coutts committed
555
          | pkg <- allPackages index
556
          , ipid <- IPI.depends pkg
Duncan Coutts's avatar
Duncan Coutts committed
557
          , Just dep <- [lookupInstalledPackageId index ipid]
558
559
560
561
562
563
564
565
566
          ]

        reallyIsInconsistent :: [InstalledPackageInfo] -> Bool
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
             installedPackageId p1 `notElem` IPI.depends p2
          && installedPackageId p2 `notElem` IPI.depends p1
        reallyIsInconsistent _ = True
Duncan Coutts's avatar
Duncan Coutts committed
567
568
569
570
571
572
573
574


moduleNameIndex :: PackageIndex -> Map ModuleName [InstalledPackageInfo]
moduleNameIndex index =
  Map.fromListWith (++)
    [ (moduleName, [pkg])
    | pkg        <- allPackages index
    , moduleName <- IPI.exposedModules pkg ]