]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/PackagingTool/IpiDb.py
2 # This file is for installed package information database operations
4 # Copyright (c) 2007 ~ 2008, Intel Corporation
5 # All rights reserved. This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 import Common
.EdkLogger
as EdkLogger
22 from CommonDataClass
import DistributionPackageClass
26 # This class represents the installed package information databse
27 # Add/Remove/Get installed distribution package information here.
30 # @param object: Inherited from object class
31 # @param DbPath: A string for the path of the database
33 # @var Conn: Connection of the database
34 # @var Cur: Cursor of the connection
36 class IpiDatabase(object):
37 def __init__(self
, DbPath
):
38 Dir
= os
.path
.dirname(DbPath
)
39 if not os
.path
.isdir(Dir
):
41 self
.Conn
= sqlite3
.connect(DbPath
, isolation_level
= 'DEFERRED')
42 self
.Conn
.execute("PRAGMA page_size=4096")
43 self
.Conn
.execute("PRAGMA synchronous=OFF")
44 self
.Cur
= self
.Conn
.cursor()
45 self
.DpTable
= 'DpInfo'
46 self
.PkgTable
= 'PkgInfo'
47 self
.ModInPkgTable
= 'ModInPkgInfo'
48 self
.StandaloneModTable
= 'StandaloneModInfo'
49 self
.ModDepexTable
= 'ModDepexInfo'
50 self
.DpFileListTable
= 'DpFileListInfo'
52 ## Initialize build database
55 def InitDatabase(self
):
56 EdkLogger
.verbose("\nInitialize IPI database started ...")
61 SqlCommand
= """create table IF NOT EXISTS %s (DpGuid TEXT NOT NULL,
62 DpVersion TEXT NOT NULL,
63 InstallTime REAL NOT NULL,
65 PRIMARY KEY (DpGuid, DpVersion)
67 self
.Cur
.execute(SqlCommand
)
69 SqlCommand
= """create table IF NOT EXISTS %s (FilePath TEXT NOT NULL,
72 PRIMARY KEY (FilePath)
73 )""" % self
.DpFileListTable
74 self
.Cur
.execute(SqlCommand
)
76 SqlCommand
= """create table IF NOT EXISTS %s (PackageGuid TEXT NOT NULL,
77 PackageVersion TEXT NOT NULL,
78 InstallTime REAL NOT NULL,
81 InstallPath TEXT NOT NULL,
82 PRIMARY KEY (PackageGuid, PackageVersion, InstallPath)
84 self
.Cur
.execute(SqlCommand
)
86 SqlCommand
= """create table IF NOT EXISTS %s (ModuleGuid TEXT NOT NULL,
87 ModuleVersion TEXT NOT NULL,
88 InstallTime REAL NOT NULL,
91 InstallPath TEXT NOT NULL,
92 PRIMARY KEY (ModuleGuid, ModuleVersion, InstallPath)
93 )""" % self
.ModInPkgTable
94 self
.Cur
.execute(SqlCommand
)
96 SqlCommand
= """create table IF NOT EXISTS %s (ModuleGuid TEXT NOT NULL,
97 ModuleVersion TEXT NOT NULL,
98 InstallTime REAL NOT NULL,
101 InstallPath TEXT NOT NULL,
102 PRIMARY KEY (ModuleGuid, ModuleVersion, InstallPath)
103 )""" % self
.StandaloneModTable
104 self
.Cur
.execute(SqlCommand
)
106 SqlCommand
= """create table IF NOT EXISTS %s (ModuleGuid TEXT NOT NULL,
107 ModuleVersion TEXT NOT NULL,
108 InstallPath TEXT NOT NULL,
111 )""" % self
.ModDepexTable
112 self
.Cur
.execute(SqlCommand
)
116 EdkLogger
.verbose("Initialize IPI database ... DONE!")
118 ## Add a distribution install information from DpObj
122 def AddDPObject(self
, DpObj
):
124 for PkgKey
in DpObj
.PackageSurfaceArea
.keys():
126 PkgVersion
= PkgKey
[1]
127 PkgInstallPath
= PkgKey
[2]
128 self
.AddPackage(PkgGuid
, PkgVersion
, DpObj
.Header
.Guid
, DpObj
.Header
.Version
, PkgInstallPath
)
129 PkgObj
= DpObj
.PackageSurfaceArea
[PkgKey
]
130 for ModKey
in PkgObj
.Modules
.keys():
132 ModVersion
= ModKey
[1]
133 ModInstallPath
= ModKey
[2]
134 self
.AddModuleInPackage(ModGuid
, ModVersion
, PkgGuid
, PkgVersion
, ModInstallPath
)
135 ModObj
= PkgObj
.Modules
[ModKey
]
136 for Dep
in ModObj
.PackageDependencies
:
137 DepexGuid
= Dep
.PackageGuid
138 DepexVersion
= Dep
.PackageVersion
139 self
.AddModuleDepex(ModGuid
, ModVersion
, ModInstallPath
, DepexGuid
, DepexVersion
)
140 for FilePath
in PkgObj
.FileList
:
141 self
.AddDpFilePathList(DpObj
.Header
.Guid
, DpObj
.Header
.Version
, FilePath
)
143 for ModKey
in DpObj
.ModuleSurfaceArea
.keys():
145 ModVersion
= ModKey
[1]
146 ModInstallPath
= ModKey
[2]
147 self
.AddStandaloneModule(ModGuid
, ModVersion
, DpObj
.Header
.Guid
, DpObj
.Header
.Version
, ModInstallPath
)
148 ModObj
= DpObj
.ModuleSurfaceArea
[ModKey
]
149 for Dep
in ModObj
.PackageDependencies
:
150 DepexGuid
= Dep
.PackageGuid
151 DepexVersion
= Dep
.PackageVersion
152 self
.AddModuleDepex(ModGuid
, ModVersion
, ModInstallPath
, DepexGuid
, DepexVersion
)
153 for FilePath
in ModObj
.FileList
:
154 self
.AddDpFilePathList(DpObj
.Header
.Guid
, DpObj
.Header
.Version
, FilePath
)
156 self
.AddDp(DpObj
.Header
.Guid
, DpObj
.Header
.Version
, DpObj
.Header
.FileName
)
157 ## Add a distribution install information
161 # @param PkgFileName:
163 def AddDp(self
, Guid
, Version
, PkgFileName
= None):
165 if Version
== None or len(Version
.strip()) == 0:
169 # Add newly installed DP information to DB.
171 if PkgFileName
== None or len(PkgFileName
.strip()) == 0:
173 (Guid
, Version
, PkgFileName
) = (Guid
, Version
, PkgFileName
)
174 CurrentTime
= time
.time()
175 SqlCommand
= """insert into %s values('%s', '%s', %s, '%s')""" % (self
.DpTable
, Guid
, Version
, CurrentTime
, PkgFileName
)
176 self
.Cur
.execute(SqlCommand
)
179 ## Add a file list from DP
185 def AddDpFilePathList(self
, DpGuid
, DpVersion
, Path
):
187 SqlCommand
= """insert into %s values('%s', '%s', '%s')""" % (self
.DpFileListTable
, Path
, DpGuid
, DpVersion
)
188 self
.Cur
.execute(SqlCommand
)
191 ## Add a package install information
199 def AddPackage(self
, Guid
, Version
, DpGuid
= None, DpVersion
= None, Path
= ''):
201 if Version
== None or len(Version
.strip()) == 0:
204 if DpGuid
== None or len(DpGuid
.strip()) == 0:
207 if DpVersion
== None or len(DpVersion
.strip()) == 0:
211 # Add newly installed package information to DB.
214 CurrentTime
= time
.time()
215 SqlCommand
= """insert into %s values('%s', '%s', %s, '%s', '%s', '%s')""" % (self
.PkgTable
, Guid
, Version
, CurrentTime
, DpGuid
, DpVersion
, Path
)
216 self
.Cur
.execute(SqlCommand
)
219 ## Add a module that from a package install information
223 # @param PkgFileName:
225 def AddModuleInPackage(self
, Guid
, Version
, PkgGuid
= None, PkgVersion
= None, Path
= ''):
227 if Version
== None or len(Version
.strip()) == 0:
230 if PkgGuid
== None or len(PkgGuid
.strip()) == 0:
233 if PkgVersion
== None or len(PkgVersion
.strip()) == 0:
237 # Add module from package information to DB.
239 CurrentTime
= time
.time()
240 SqlCommand
= """insert into %s values('%s', '%s', %s, '%s', '%s', '%s')""" % (self
.ModInPkgTable
, Guid
, Version
, CurrentTime
, PkgGuid
, PkgVersion
, Path
)
241 self
.Cur
.execute(SqlCommand
)
244 ## Add a module that is standalone install information
248 # @param PkgFileName:
250 def AddStandaloneModule(self
, Guid
, Version
, DpGuid
= None, DpVersion
= None, Path
= ''):
252 if Version
== None or len(Version
.strip()) == 0:
255 if DpGuid
== None or len(DpGuid
.strip()) == 0:
258 if DpVersion
== None or len(DpVersion
.strip()) == 0:
262 # Add module standalone information to DB.
264 CurrentTime
= time
.time()
265 SqlCommand
= """insert into %s values('%s', '%s', %s, '%s', '%s', '%s')""" % (self
.StandaloneModTable
, Guid
, Version
, CurrentTime
, DpGuid
, DpVersion
, Path
)
266 self
.Cur
.execute(SqlCommand
)
269 ## Add a module depex
274 # @param DepexVersion:
276 def AddModuleDepex(self
, Guid
, Version
, Path
, DepexGuid
= None, DepexVersion
= None):
278 if DepexGuid
== None or len(DepexGuid
.strip()) == 0:
281 if DepexVersion
== None or len(DepexVersion
.strip()) == 0:
285 # Add module depex information to DB.
288 SqlCommand
= """insert into %s values('%s', '%s', '%s', '%s', '%s')""" % (self
.ModDepexTable
, Guid
, Version
, Path
, DepexGuid
, DepexVersion
)
289 self
.Cur
.execute(SqlCommand
)
292 ## Remove a distribution install information, if no version specified, remove all DPs with this Guid.
296 def RemoveDpObj(self
, DpGuid
, DpVersion
):
298 PkgList
= self
.GetPackageListFromDp(DpGuid
, DpVersion
)
300 # delete from ModDepex the standalone module's dependency
301 SqlCommand
= """delete from ModDepexInfo where ModDepexInfo.ModuleGuid in
302 (select ModuleGuid from StandaloneModInfo as B where B.DpGuid = '%s' and B.DpVersion = '%s')
303 and ModDepexInfo.ModuleVersion in
304 (select ModuleVersion from StandaloneModInfo as B where B.DpGuid = '%s' and B.DpVersion = '%s')
305 and ModDepexInfo.InstallPath in
306 (select InstallPath from StandaloneModInfo as B where B.DpGuid = '%s' and B.DpVersion = '%s') """ \
307 %(DpGuid
, DpVersion
, DpGuid
, DpVersion
, DpGuid
, DpVersion
)
309 # SqlCommand = """delete from %s where %s.DpGuid ='%s' and %s.DpVersion = '%s' and
310 # %s.ModuleGuid = %s.ModuleGuid and %s.ModuleVersion = %s.ModuleVersion and
311 # %s.InstallPath = %s.InstallPath""" \
312 # % (self.ModDepexTable, self.StandaloneModTable, DpGuid, self.StandaloneModTable, DpVersion, self.ModDepexTable, self.StandaloneModTable, self.ModDepexTable, self.StandaloneModTable, self.ModDepexTable, self.StandaloneModTable)
314 self
.Cur
.execute(SqlCommand
)
316 # delete from ModDepex the from pkg module's dependency
318 # SqlCommand = """delete from %s where %s.PackageGuid ='%s' and %s.PackageVersion = '%s' and
319 # %s.ModuleGuid = %s.ModuleGuid and %s.ModuleVersion = %s.ModuleVersion and
320 # %s.InstallPath = %s.InstallPath""" \
321 # % (self.ModDepexTable, self.ModInPkgTable, Pkg[0], self.ModInPkgTable, Pkg[1], self.ModDepexTable, self.ModInPkgTable, self.ModDepexTable, self.ModInPkgTable, self.ModDepexTable, self.ModInPkgTable)
322 SqlCommand
= """delete from ModDepexInfo where ModDepexInfo.ModuleGuid in
323 (select ModuleGuid from ModInPkgInfo where ModInPkgInfo.PackageGuid ='%s' and ModInPkgInfo.PackageVersion = '%s')
324 and ModDepexInfo.ModuleVersion in
325 (select ModuleVersion from ModInPkgInfo where ModInPkgInfo.PackageGuid ='%s' and ModInPkgInfo.PackageVersion = '%s')
326 and ModDepexInfo.InstallPath in
327 (select InstallPath from ModInPkgInfo where ModInPkgInfo.PackageGuid ='%s' and ModInPkgInfo.PackageVersion = '%s')""" \
328 % (Pkg
[0], Pkg
[1],Pkg
[0], Pkg
[1],Pkg
[0], Pkg
[1])
330 self
.Cur
.execute(SqlCommand
)
332 # delete the standalone module
333 SqlCommand
= """delete from %s where DpGuid ='%s' and DpVersion = '%s'""" % (self
.StandaloneModTable
, DpGuid
, DpVersion
)
334 self
.Cur
.execute(SqlCommand
)
336 # delete the from pkg module
338 SqlCommand
= """delete from %s where %s.PackageGuid ='%s' and %s.PackageVersion = '%s'""" \
339 % (self
.ModInPkgTable
, self
.ModInPkgTable
, Pkg
[0], self
.ModInPkgTable
, Pkg
[1])
340 self
.Cur
.execute(SqlCommand
)
343 SqlCommand
= """delete from %s where DpGuid ='%s' and DpVersion = '%s'""" % (self
.PkgTable
, DpGuid
, DpVersion
)
344 self
.Cur
.execute(SqlCommand
)
346 # delete file list from DP
347 SqlCommand
= """delete from %s where DpGuid ='%s' and DpVersion = '%s'""" % (self
.DpFileListTable
, DpGuid
, DpVersion
)
348 self
.Cur
.execute(SqlCommand
)
351 SqlCommand
= """delete from %s where DpGuid ='%s' and DpVersion = '%s'""" % (self
.DpTable
, DpGuid
, DpVersion
)
352 self
.Cur
.execute(SqlCommand
)
356 ## Get a list of distribution install information.
361 def GetDp(self
, Guid
, Version
):
363 if Version
== None or len(Version
.strip()) == 0:
365 EdkLogger
.verbose("\nGetting list of DP install information started ...")
366 (DpGuid
, DpVersion
) = (Guid
, Version
)
367 SqlCommand
= """select * from %s where DpGuid ='%s'""" % (self
.DpTable
, DpGuid
)
368 self
.Cur
.execute(SqlCommand
)
371 EdkLogger
.verbose("\nGetting DP install information started ...")
372 (DpGuid
, DpVersion
) = (Guid
, Version
)
373 SqlCommand
= """select * from %s where DpGuid ='%s' and DpVersion = '%s'""" % (self
.DpTable
, DpGuid
, DpVersion
)
374 self
.Cur
.execute(SqlCommand
)
377 for DpInfo
in self
.Cur
:
379 DpVersion
= DpInfo
[1]
380 InstallTime
= DpInfo
[2]
381 PkgFileName
= DpInfo
[3]
382 DpList
.append((DpGuid
, DpVersion
, InstallTime
, PkgFileName
))
384 EdkLogger
.verbose("Getting DP install information ... DONE!")
387 ## Get a list of distribution install file path information.
392 def GetDpFileList(self
, Guid
, Version
):
394 (DpGuid
, DpVersion
) = (Guid
, Version
)
395 SqlCommand
= """select * from %s where DpGuid ='%s' and DpVersion = '%s'""" % (self
.DpFileListTable
, DpGuid
, DpVersion
)
396 self
.Cur
.execute(SqlCommand
)
399 for Result
in self
.Cur
:
401 PathList
.append(Path
)
405 ## Get a list of package information.
410 def GetPackage(self
, Guid
, Version
, DpGuid
= '', DpVersion
= ''):
412 if DpVersion
== '' or DpGuid
== '':
414 (PackageGuid
, PackageVersion
) = (Guid
, Version
)
415 SqlCommand
= """select * from %s where PackageGuid ='%s' and PackageVersion = '%s'""" % (self
.PkgTable
, PackageGuid
, PackageVersion
)
416 self
.Cur
.execute(SqlCommand
)
418 elif Version
== None or len(Version
.strip()) == 0:
420 SqlCommand
= """select * from %s where PackageGuid ='%s'""" % (self
.PkgTable
, Guid
)
421 self
.Cur
.execute(SqlCommand
)
423 (PackageGuid
, PackageVersion
) = (Guid
, Version
)
424 SqlCommand
= """select * from %s where PackageGuid ='%s' and PackageVersion = '%s'
425 and DpGuid = '%s' and DpVersion = '%s'""" % (self
.PkgTable
, PackageGuid
, PackageVersion
, DpGuid
, DpVersion
)
426 self
.Cur
.execute(SqlCommand
)
429 for PkgInfo
in self
.Cur
:
431 PkgVersion
= PkgInfo
[1]
432 InstallTime
= PkgInfo
[2]
433 InstallPath
= PkgInfo
[5]
434 PkgList
.append((PkgGuid
, PkgVersion
, InstallTime
, DpGuid
, DpVersion
, InstallPath
))
438 ## Get a list of module in package information.
443 def GetModInPackage(self
, Guid
, Version
, PkgGuid
= '', PkgVersion
= ''):
445 if PkgVersion
== '' or PkgGuid
== '':
447 (ModuleGuid
, ModuleVersion
) = (Guid
, Version
)
448 SqlCommand
= """select * from %s where ModuleGuid ='%s' and ModuleVersion = '%s'""" % (self
.ModInPkgTable
, ModuleGuid
, ModuleVersion
)
449 self
.Cur
.execute(SqlCommand
)
452 (ModuleGuid
, ModuleVersion
) = (Guid
, Version
)
453 SqlCommand
= """select * from %s where ModuleGuid ='%s' and ModuleVersion = '%s' and PackageGuid ='%s' and PackageVersion = '%s'
454 """ % (self
.ModInPkgTable
, ModuleGuid
, ModuleVersion
, PkgGuid
, PkgVersion
)
455 self
.Cur
.execute(SqlCommand
)
458 for ModInfo
in self
.Cur
:
460 ModVersion
= ModInfo
[1]
461 InstallTime
= ModInfo
[2]
462 InstallPath
= ModInfo
[5]
463 ModList
.append((ModGuid
, ModVersion
, InstallTime
, PkgGuid
, PkgVersion
, InstallPath
))
467 ## Get a list of module standalone.
472 def GetStandaloneModule(self
, Guid
, Version
, DpGuid
= '', DpVersion
= ''):
476 (ModuleGuid
, ModuleVersion
) = (Guid
, Version
)
477 SqlCommand
= """select * from %s where ModuleGuid ='%s' and ModuleVersion = '%s'""" % (self
.StandaloneModTable
, ModuleGuid
, ModuleVersion
)
478 self
.Cur
.execute(SqlCommand
)
481 (ModuleGuid
, ModuleVersion
) = (Guid
, Version
)
482 SqlCommand
= """select * from %s where ModuleGuid ='%s' and ModuleVersion = '%s' and DpGuid ='%s' and DpVersion = '%s'
483 """ % (self
.StandaloneModTable
, ModuleGuid
, ModuleVersion
, DpGuid
, DpVersion
)
484 self
.Cur
.execute(SqlCommand
)
487 for ModInfo
in self
.Cur
:
489 ModVersion
= ModInfo
[1]
490 InstallTime
= ModInfo
[2]
491 InstallPath
= ModInfo
[5]
492 ModList
.append((ModGuid
, ModVersion
, InstallTime
, DpGuid
, DpVersion
, InstallPath
))
496 ## Get a list of module information that comes from DP.
501 def GetStandaloneModuleInstallPathListFromDp(self
, DpGuid
, DpVersion
):
504 SqlCommand
= """select t1.InstallPath from %s t1 where t1.DpGuid ='%s' and t1.DpVersion = '%s'
505 """ % (self
.StandaloneModTable
, DpGuid
, DpVersion
)
506 self
.Cur
.execute(SqlCommand
)
508 for Result
in self
.Cur
:
509 InstallPath
= Result
[0]
510 PathList
.append(InstallPath
)
514 ## Get a list of package information.
519 def GetPackageListFromDp(self
, DpGuid
, DpVersion
):
522 SqlCommand
= """select * from %s where DpGuid ='%s' and DpVersion = '%s'
523 """ % (self
.PkgTable
, DpGuid
, DpVersion
)
524 self
.Cur
.execute(SqlCommand
)
527 for PkgInfo
in self
.Cur
:
529 PkgVersion
= PkgInfo
[1]
530 InstallPath
= PkgInfo
[5]
531 PkgList
.append((PkgGuid
, PkgVersion
, InstallPath
))
535 ## Get a list of modules that depends on package information from a DP.
540 def GetDpDependentModuleList(self
, DpGuid
, DpVersion
):
543 PkgList
= self
.GetPackageListFromDp(DpGuid
, DpVersion
)
548 SqlCommand
= """select t1.ModuleGuid, t1.ModuleVersion, t1.InstallPath
549 from %s as t1, %s as t2, where t1.ModuleGuid = t2.ModuleGuid and
550 t1.ModuleVersion = t2.ModuleVersion and t2.DepexGuid ='%s' and (t2.DepexVersion = '%s' or t2.DepexVersion = 'N/A') and
551 t1.PackageGuid != '%s' and t1.PackageVersion != '%s'
552 """ % (self
.ModInPkgTable
, self
.ModDepexTable
, Pkg
[0], Pkg
[1], Pkg
[0], Pkg
[1])
553 self
.Cur
.execute(SqlCommand
)
554 for ModInfo
in self
.Cur
:
556 ModVersion
= ModInfo
[1]
557 InstallPath
= ModInfo
[2]
558 ModList
.append((ModGuid
, ModVersion
, InstallPath
))
560 SqlCommand
= """select t1.ModuleGuid, t1.ModuleVersion, t1.InstallPath
561 from %s as t1, %s as t2, where t1.ModuleGuid = t2.ModuleGuid and
562 t1.ModuleVersion = t2.ModuleVersion and t2.DepexGuid ='%s' and (t2.DepexVersion = '%s' or t2.DepexVersion = 'N/A') and
563 t1.DpGuid != '%s' and t1.DpVersion != '%s'
564 """ % (self
.StandaloneModTable
, self
.ModDepexTable
, Pkg
[0], Pkg
[1], DpGuid
, DpVersion
)
565 self
.Cur
.execute(SqlCommand
)
566 for ModInfo
in self
.Cur
:
568 ModVersion
= ModInfo
[1]
569 InstallPath
= ModInfo
[2]
570 ModList
.append((ModGuid
, ModVersion
, InstallPath
))
575 ## Get a module depex
581 def GetModuleDepex(self
, Guid
, Version
, Path
):
584 # Get module depex information to DB.
587 SqlCommand
= """select * from %s where ModuleGuid ='%s' and ModuleVersion = '%s' and InstallPath ='%s'
588 """ % (self
.ModDepexTable
, Guid
, Version
, Path
)
589 self
.Cur
.execute(SqlCommand
)
593 for DepInfo
in self
.Cur
:
594 DepexGuid
= DepInfo
[3]
595 DepexVersion
= DepInfo
[4]
596 DepexList
.append((DepexGuid
, DepexVersion
))
600 ## Close entire database
602 # Close the connection and cursor
609 ## Convert To Sql String
611 # 1. Replace "'" with "''" in each item of StringList
613 # @param StringList: A list for strings to be converted
615 def __ConvertToSqlString(self
, StringList
):
616 return map(lambda s
: s
.replace("'", "''") , StringList
)
619 # This acts like the main() function for the script, unless it is 'import'ed into another
622 if __name__
== '__main__':
623 EdkLogger
.Initialize()
624 EdkLogger
.SetLevel(EdkLogger
.DEBUG_0
)
625 DATABASE_PATH
= "C://MyWork//Conf//.cache//XML.db"
626 Db
= IpiDatabase(DATABASE_PATH
)