summaryrefslogtreecommitdiff
path: root/BaseTools/Source/Python/PackagingTool/DependencyRules.py
blob: 402694054ed4c138028ca5a4d49fd2f0dc28dbb6 (plain)
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
## @file
# This file is for installed package information database operations
#
# Copyright (c) 2007 ~ 2008, Intel Corporation
# All rights reserved. This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution.  The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

##
# Import Modules
#
import sqlite3
import os

import Common.EdkLogger as EdkLogger
import IpiDb

(DEPEX_CHECK_SUCCESS, DEPEX_CHECK_MODULE_NOT_FOUND, \
DEPEX_CHECK_PACKAGE_NOT_FOUND, DEPEX_CHECK_DP_NOT_FOUND) = (0, 1, 2, 3)

## IpiDb
#
# This class represents the installed package information databse
# Add/Remove/Get installed distribution package information here.
# 
# 
# @param object:      Inherited from object class
# @param DbPath:      A string for the path of the database
#
# @var Conn:          Connection of the database
# @var Cur:           Cursor of the connection
#
class DependencyRules(object):
    def __init__(self, Db):
        self.IpiDb = Db
    
    ## Check whether a module exists in current workspace.
    #
    # @param Guid:  
    # @param Version:
    #
    def CheckModuleExists(self, Guid, Version, ReturnCode = DEPEX_CHECK_SUCCESS):
        EdkLogger.verbose("\nCheck module exists in workspace started ...")
        ModuleList = []
        ModuleList = self.IpiDb.GetModInPackage(Guid, Version)
        ModuleList.extend(self.IpiDb.GetStandaloneModule(Guid, Version))
        EdkLogger.verbose("Check module exists in workspace ... DONE!")
        if len(ModuleList) > 0:
            return True
        else:
            ReturnCode = DEPEX_CHECK_MODULE_NOT_FOUND
            return False
        

    ## Check whether a module depex satified by current workspace.
    #
    # @param ModuleObj:  
    # @param DpObj:
    #
    def CheckModuleDepexSatisfied(self, ModuleObj, DpObj = None, ReturnCode = DEPEX_CHECK_SUCCESS):
        EdkLogger.verbose("\nCheck module depex met by workspace started ...")
        for Dep in ModuleObj.PackageDependencies:
            Exist = self.CheckPackageExists(Dep.PackageGuid, Dep.PackageVersion, ReturnCode)
            if not Exist:
                if DpObj == None:
                    ReturnCode = DEPEX_CHECK_PACKAGE_NOT_FOUND
                    return False
                for GuidVerPair in DpObj.PackageSurfaceArea.keys():
                    if Dep.PackageGuid == GuidVerPair[0]:
                        if Dep.PackageVersion == None or len(Dep.PackageVersion) == 0:
                            break
                        if Dep.PackageVersion == GuidVerPair[1]:
                            break
                        else:
                            ReturnCode = DEPEX_CHECK_PACKAGE_NOT_FOUND
                            return False
                else:
                    ReturnCode = DEPEX_CHECK_PACKAGE_NOT_FOUND
                    return False
        return True
        
        EdkLogger.verbose("Check module depex met by workspace ... DONE!")
    
    ## Check whether a package exists in current workspace.
    #
    # @param Guid:  
    # @param Version:
    #
    def CheckPackageExists(self, Guid, Version, ReturnCode = DEPEX_CHECK_SUCCESS):
        EdkLogger.verbose("\nCheck package exists in workspace started ...")
        PkgList = []
        PkgList = self.IpiDb.GetPackage(Guid, Version)
        if len(PkgList) > 0:
            return True
        else:
            ReturnCode = DEPEX_CHECK_PACKAGE_NOT_FOUND
            return False
        
        EdkLogger.verbose("Check package exists in workspace ... DONE!")
        
    ## Check whether a package depex satified by current workspace.
    #
    # @param ModuleObj:  
    # @param DpObj:
    #
    def CheckPackageDepexSatisfied(self, PkgObj, DpObj = None, ReturnCode = DEPEX_CHECK_SUCCESS):
        
        for ModKey in PkgObj.Modules.keys():
            ModObj = PkgObj.Modules[ModKey]
            if self.CheckModuleDepexSatisfied(ModObj, DpObj, ReturnCode):
                continue
            else:
                return False
        return True
        
    ## Check whether a DP exists in current workspace.
    #
    # @param Guid:  
    # @param Version:
    #
    def CheckDpExists(self, Guid, Version, ReturnCode = DEPEX_CHECK_SUCCESS):
        EdkLogger.verbose("\nCheck DP exists in workspace started ...")
        DpList = []
        DpList = self.IpiDb.GetDp(Guid, Version)
        if len(DpList) > 0:
            return True
        else:
            ReturnCode = DEPEX_CHECK_DP_NOT_FOUND
            return False
        
        EdkLogger.verbose("Check DP exists in workspace ... DONE!")
        
    ## Check whether a DP depex satified by current workspace.
    #
    # @param ModuleObj:  
    # @param DpObj:
    #
    def CheckDpDepexSatisfied(self, DpObj, ReturnCode = DEPEX_CHECK_SUCCESS):
        
        for PkgKey in DpObj.PackageSurfaceArea.keys():
            PkgObj = DpObj.PackageSurfaceArea[PkgKey]
            if self.CheckPackageDepexSatisfied(PkgObj, DpObj, ReturnCode):
                continue
            else:
                return False
            
        for ModKey in DpObj.ModuleSurfaceArea.keys():
            ModObj = PkgObj.ModuleSurfaceArea[ModKey]
            if self.CheckModuleDepexSatisfied(ModObj, DpObj, ReturnCode):
                continue
            else:
                return False
        
        return True
    
    ## Check whether a DP depex satified by current workspace.
    #
    # @param ModuleObj:  
    # @param DpObj:
    #
    def CheckDpDepexForRemove(self, DpGuid, DpVersion, ReturnCode = DEPEX_CHECK_SUCCESS):
        
        # Get mod list that is dependent on pkg installed from this DP.
        ModList = self.IpiDb.GetDpDependentModuleList(DpGuid, DpVersion)
        
        if len(ModList) > 0:
            return False
        
        return True
##
#
# This acts like the main() function for the script, unless it is 'import'ed into another
# script.
#
if __name__ == '__main__':
    EdkLogger.Initialize()
    EdkLogger.SetLevel(EdkLogger.DEBUG_0)