]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/InstallPkg.py
BaseTools: Enhance BaseTools supports FixedAtBuild usage in VFR file
[mirror_edk2.git] / BaseTools / Source / Python / UPT / InstallPkg.py
CommitLineData
4234283c
LG
1## @file\r
2# Install distribution package.\r
3#\r
af0c61ae 4# Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>\r
4234283c
LG
5#\r
6# This program and the accompanying materials are licensed and made available \r
7# under the terms and conditions of the BSD License which accompanies this \r
8# distribution. The full text of the license may be found at \r
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14"""\r
15Install a distribution package\r
16"""\r
17##\r
18# Import Modules\r
19#\r
421ccda3 20from Core.FileHook import __FileHookOpen__\r
4234283c
LG
21import os.path\r
22from os import chmod\r
23from os import SEEK_SET\r
24from os import SEEK_END\r
25import stat\r
26import md5\r
421ccda3 27import copy\r
4234283c
LG
28from sys import stdin\r
29from sys import platform\r
30from shutil import rmtree\r
31from shutil import copyfile\r
32from traceback import format_exc\r
33from platform import python_version\r
34\r
35from Logger import StringTable as ST\r
36from Logger.ToolError import UNKNOWN_ERROR\r
37from Logger.ToolError import FILE_UNKNOWN_ERROR\r
38from Logger.ToolError import OPTION_MISSING\r
39from Logger.ToolError import UPT_ALREADY_INSTALLED_ERROR\r
40from Logger.ToolError import FatalError\r
41from Logger.ToolError import ABORT_ERROR\r
42from Logger.ToolError import CODE_ERROR\r
43from Logger.ToolError import FORMAT_INVALID\r
44from Logger.ToolError import FILE_TYPE_MISMATCH\r
45import Logger.Log as Logger\r
46\r
4234283c
LG
47from Library.Misc import Sdict\r
48from Library.Misc import ConvertPath\r
49from Library.ParserValidate import IsValidInstallPath\r
50from Xml.XmlParser import DistributionPackageXml\r
51from GenMetaFile.GenDecFile import PackageToDec\r
52from GenMetaFile.GenInfFile import ModuleToInf\r
53from Core.PackageFile import PackageFile\r
54from Core.PackageFile import FILE_NOT_FOUND\r
55from Core.PackageFile import FILE_CHECKSUM_FAILURE\r
56from Core.PackageFile import CreateDirectory\r
57from Core.DependencyRules import DependencyRules\r
58from Library import GlobalData\r
59\r
60## InstallNewPackage\r
61#\r
62# @param WorkspaceDir: Workspace Directory\r
63# @param Path: Package Path\r
64# @param CustomPath: whether need to customize path at first\r
65#\r
66def InstallNewPackage(WorkspaceDir, Path, CustomPath = False):\r
67 if os.path.isabs(Path):\r
68 Logger.Info(ST.MSG_RELATIVE_PATH_ONLY%Path)\r
69 elif CustomPath:\r
70 Logger.Info(ST.MSG_NEW_PKG_PATH)\r
71 else:\r
72 Path = ConvertPath(Path)\r
73 Path = os.path.normpath(Path)\r
74 FullPath = os.path.normpath(os.path.join(WorkspaceDir, Path))\r
75 if os.path.exists(FullPath):\r
76 Logger.Info(ST.ERR_DIR_ALREADY_EXIST%FullPath)\r
77 else:\r
78 return Path\r
79\r
80 Input = stdin.readline()\r
81 Input = Input.replace('\r', '').replace('\n', '')\r
82 if Input == '':\r
83 Logger.Error("InstallPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT)\r
84 Input = Input.replace('\r', '').replace('\n', '')\r
85 return InstallNewPackage(WorkspaceDir, Input, False)\r
86\r
4234283c
LG
87## InstallNewModule\r
88#\r
89# @param WorkspaceDir: Workspace Directory\r
90# @param Path: Standalone Module Path\r
91# @param PathList: The already installed standalone module Path list\r
92#\r
93def InstallNewModule(WorkspaceDir, Path, PathList = None):\r
94 if PathList == None:\r
95 PathList = []\r
96 Path = ConvertPath(Path)\r
97 Path = os.path.normpath(Path)\r
98 FullPath = os.path.normpath(os.path.join(WorkspaceDir, Path))\r
99 if os.path.exists(FullPath) and FullPath not in PathList:\r
100 Logger.Info(ST.ERR_DIR_ALREADY_EXIST%Path)\r
101 elif Path == FullPath:\r
102 Logger.Info(ST.MSG_RELATIVE_PATH_ONLY%FullPath)\r
103 else:\r
104 return Path\r
105 \r
106 Input = stdin.readline()\r
107 Input = Input.replace('\r', '').replace('\n', '')\r
108 if Input == '':\r
109 Logger.Error("InstallPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT)\r
110 Input = Input.replace('\r', '').replace('\n', '')\r
111 return InstallNewModule(WorkspaceDir, Input, PathList)\r
112\r
113 \r
114## InstallNewFile\r
115#\r
116# @param WorkspaceDir: Workspace Direction\r
117# @param File: File\r
118#\r
119def InstallNewFile(WorkspaceDir, File):\r
120 FullPath = os.path.normpath(os.path.join(WorkspaceDir, File))\r
121 if os.path.exists(FullPath):\r
122 Logger.Info(ST.ERR_FILE_ALREADY_EXIST %File)\r
123 Input = stdin.readline()\r
124 Input = Input.replace('\r', '').replace('\n', '')\r
125 if Input == '':\r
126 Logger.Error("InstallPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT)\r
127 Input = Input.replace('\r', '').replace('\n', '')\r
128 return InstallNewFile(WorkspaceDir, Input)\r
129 else:\r
130 return File\r
131\r
132## UnZipDp\r
133#\r
134# UnZipDp\r
135#\r
421ccda3 136def UnZipDp(WorkspaceDir, DpPkgFileName):\r
4234283c
LG
137 ContentZipFile = None\r
138 Logger.Quiet(ST.MSG_UZIP_PARSE_XML)\r
4234283c
LG
139 DistFile = PackageFile(DpPkgFileName)\r
140 \r
141 DpDescFileName, ContentFileName = GetDPFile(DistFile.GetZipFile())\r
142 \r
143 GlobalData.gUNPACK_DIR = os.path.normpath(os.path.join(WorkspaceDir, ".tmp"))\r
144 DistPkgFile = DistFile.UnpackFile(DpDescFileName,\r
145 os.path.normpath(os.path.join(GlobalData.gUNPACK_DIR, DpDescFileName)))\r
146 if not DistPkgFile:\r
147 Logger.Error("InstallPkg", FILE_NOT_FOUND, ST.ERR_FILE_BROKEN %DpDescFileName)\r
148 \r
149 #\r
150 # Generate distpkg\r
151 #\r
152 DistPkgObj = DistributionPackageXml()\r
153 DistPkg = DistPkgObj.FromXml(DistPkgFile)\r
154 if DistPkg.Header.RePackage == '':\r
155 DistPkg.Header.RePackage = False\r
156 if DistPkg.Header.ReadOnly == '':\r
157 DistPkg.Header.ReadOnly = False\r
421ccda3 158\r
4234283c
LG
159 #\r
160 # unzip contents.zip file\r
161 #\r
162 ContentFile = DistFile.UnpackFile(ContentFileName,\r
163 os.path.normpath(os.path.join(GlobalData.gUNPACK_DIR, ContentFileName)))\r
164 if not ContentFile:\r
165 Logger.Error("InstallPkg", FILE_NOT_FOUND,\r
166 ST.ERR_FILE_BROKEN % ContentFileName)\r
167\r
421ccda3 168 FilePointer = __FileHookOpen__(ContentFile, "rb")\r
4234283c
LG
169 #\r
170 # Assume no archive comment.\r
171 #\r
172 FilePointer.seek(0, SEEK_SET)\r
173 FilePointer.seek(0, SEEK_END)\r
174 #\r
175 # Get file size\r
176 # \r
177 FileSize = FilePointer.tell()\r
178 FilePointer.close()\r
179 \r
180 if FileSize != 0: \r
181 ContentZipFile = PackageFile(ContentFile)\r
182\r
183 #\r
184 # verify MD5 signature when existed\r
185 #\r
186 if DistPkg.Header.Signature != '':\r
421ccda3 187 Md5Sigature = md5.new(__FileHookOpen__(ContentFile, 'rb').read())\r
4234283c
LG
188 if DistPkg.Header.Signature != Md5Sigature.hexdigest():\r
189 ContentZipFile.Close()\r
190 Logger.Error("InstallPkg", FILE_CHECKSUM_FAILURE,\r
191 ExtraData=ContentFile)\r
192\r
421ccda3 193 return DistPkg, ContentZipFile, DpPkgFileName, DistFile\r
4234283c
LG
194\r
195## GetPackageList\r
196#\r
197# GetPackageList\r
198#\r
199def GetPackageList(DistPkg, Dep, WorkspaceDir, Options, ContentZipFile, ModuleList, PackageList):\r
200 NewDict = Sdict()\r
201 for Guid, Version, Path in DistPkg.PackageSurfaceArea:\r
202 PackagePath = Path\r
203 Package = DistPkg.PackageSurfaceArea[Guid, Version, Path]\r
204 Logger.Info(ST.MSG_INSTALL_PACKAGE % Package.GetName())\r
205 if Dep.CheckPackageExists(Guid, Version):\r
206 Logger.Info(ST.WRN_PACKAGE_EXISTED %(Guid, Version))\r
421ccda3
HC
207 if Options.UseGuidedPkgPath:\r
208 GuidedPkgPath = "%s_%s_%s" % (Package.GetName(), Guid, Version)\r
209 NewPackagePath = InstallNewPackage(WorkspaceDir, GuidedPkgPath, Options.CustomPath)\r
210 else:\r
211 NewPackagePath = InstallNewPackage(WorkspaceDir, PackagePath, Options.CustomPath)\r
4234283c
LG
212 InstallPackageContent(PackagePath, NewPackagePath, Package, ContentZipFile, Dep, WorkspaceDir, ModuleList, \r
213 DistPkg.Header.ReadOnly)\r
214 PackageList.append(Package)\r
215 \r
216 NewDict[Guid, Version, Package.GetPackagePath()] = Package\r
217 \r
218 #\r
219 # Now generate meta-data files, first generate all dec for package\r
220 # dec should be generated before inf, and inf should be generated after\r
221 # all packages installed, else hard to resolve modules' package\r
222 # dependency (Hard to get the location of the newly installed package)\r
223 #\r
224 for Package in PackageList:\r
421ccda3
HC
225 FilePath = PackageToDec(Package, DistPkg.Header)\r
226 Md5Sigature = md5.new(__FileHookOpen__(str(FilePath), 'rb').read())\r
4234283c
LG
227 Md5Sum = Md5Sigature.hexdigest()\r
228 if (FilePath, Md5Sum) not in Package.FileList:\r
229 Package.FileList.append((FilePath, Md5Sum))\r
230 \r
231 return NewDict\r
232\r
233## GetModuleList\r
234#\r
235# GetModuleList\r
236#\r
237def GetModuleList(DistPkg, Dep, WorkspaceDir, ContentZipFile, ModuleList):\r
238 #\r
239 # ModulePathList will keep track of the standalone module path that\r
240 # we just installed. If a new module's path in that list \r
241 # (only multiple INF in one directory will be so), we will \r
242 # install them directly. If not, we will try to create a new directory \r
243 # for it.\r
244 #\r
421ccda3 245 ModulePathList = []\r
4234283c
LG
246 \r
247 #\r
248 # Check module exist and install\r
249 #\r
250 Module = None\r
251 NewDict = Sdict() \r
d0acc87a 252 for Guid, Version, Name, Path in DistPkg.ModuleSurfaceArea:\r
4234283c 253 ModulePath = Path\r
d0acc87a 254 Module = DistPkg.ModuleSurfaceArea[Guid, Version, Name, Path]\r
4234283c 255 Logger.Info(ST.MSG_INSTALL_MODULE % Module.GetName())\r
d0acc87a 256 if Dep.CheckModuleExists(Guid, Version, Name, Path):\r
4234283c
LG
257 Logger.Quiet(ST.WRN_MODULE_EXISTED %Path)\r
258 #\r
259 # here check for the multiple inf share the same module path cases:\r
260 # they should be installed into the same directory\r
261 #\r
262 ModuleFullPath = \\r
263 os.path.normpath(os.path.join(WorkspaceDir, ModulePath))\r
264 if ModuleFullPath not in ModulePathList:\r
265 NewModulePath = InstallNewModule(WorkspaceDir, ModulePath, ModulePathList)\r
266 NewModuleFullPath = os.path.normpath(os.path.join(WorkspaceDir, NewModulePath))\r
267 ModulePathList.append(NewModuleFullPath)\r
268 else:\r
269 NewModulePath = ModulePath\r
270 \r
271 InstallModuleContent(ModulePath, NewModulePath, '', Module, ContentZipFile, WorkspaceDir, ModuleList, None, \r
272 DistPkg.Header.ReadOnly)\r
273 #\r
274 # Update module\r
275 #\r
276 Module.SetModulePath(Module.GetModulePath().replace(Path, NewModulePath, 1))\r
277 \r
d0acc87a 278 NewDict[Guid, Version, Name, Module.GetModulePath()] = Module\r
4234283c
LG
279\r
280 #\r
281 # generate all inf for modules\r
282 #\r
283 for (Module, Package) in ModuleList:\r
421ccda3
HC
284 CheckCNameInModuleRedefined(Module, DistPkg)\r
285 FilePath = ModuleToInf(Module, Package, DistPkg.Header)\r
286 Md5Sigature = md5.new(__FileHookOpen__(str(FilePath), 'rb').read())\r
4234283c
LG
287 Md5Sum = Md5Sigature.hexdigest()\r
288 if Package:\r
289 if (FilePath, Md5Sum) not in Package.FileList:\r
290 Package.FileList.append((FilePath, Md5Sum))\r
291 else:\r
292 if (FilePath, Md5Sum) not in Module.FileList:\r
293 Module.FileList.append((FilePath, Md5Sum))\r
421ccda3
HC
294 #\r
295 # append the module unicode files to Package FileList\r
296 #\r
297 for (FilePath, Md5Sum) in Module.FileList:\r
298 if str(FilePath).endswith('.uni') and Package and (FilePath, Md5Sum) not in Package.FileList:\r
299 Package.FileList.append((FilePath, Md5Sum))\r
4234283c
LG
300 \r
301 return NewDict\r
302\r
421ccda3
HC
303##\r
304# Get all protocol/ppi/guid CNames and pcd name from all dependent DEC file\r
305#\r
306def GetDepProtocolPpiGuidPcdNames(DePackageObjList):\r
307 #\r
308 # [[Dec1Protocol1, Dec1Protocol2...], [Dec2Protocols...],...]\r
309 #\r
310 DependentProtocolCNames = []\r
311 DependentPpiCNames = []\r
312 DependentGuidCNames = []\r
313 DependentPcdNames = []\r
314 \r
315 for PackageObj in DePackageObjList:\r
316 #\r
317 # Get protocol CName list from all dependent DEC file\r
318 #\r
319 ProtocolCNames = []\r
320 for Protocol in PackageObj.GetProtocolList():\r
321 if Protocol.GetCName() not in ProtocolCNames:\r
322 ProtocolCNames.append(Protocol.GetCName())\r
323 \r
324 DependentProtocolCNames.append(ProtocolCNames)\r
325 \r
326 #\r
327 # Get Ppi CName list from all dependent DEC file\r
328 # \r
329 PpiCNames = []\r
330 for Ppi in PackageObj.GetPpiList():\r
331 if Ppi.GetCName() not in PpiCNames:\r
332 PpiCNames.append(Ppi.GetCName())\r
333\r
334 DependentPpiCNames.append(PpiCNames)\r
335 \r
336 #\r
337 # Get Guid CName list from all dependent DEC file\r
338 # \r
339 GuidCNames = []\r
340 for Guid in PackageObj.GetGuidList():\r
341 if Guid.GetCName() not in GuidCNames:\r
342 GuidCNames.append(Guid.GetCName())\r
343 \r
344 DependentGuidCNames.append(GuidCNames)\r
345 \r
346 #\r
347 # Get PcdName list from all dependent DEC file\r
348 #\r
349 PcdNames = []\r
350 for Pcd in PackageObj.GetPcdList():\r
351 PcdName = '.'.join([Pcd.GetTokenSpaceGuidCName(), Pcd.GetCName()])\r
352 if PcdName not in PcdNames:\r
353 PcdNames.append(PcdName)\r
354 \r
355 DependentPcdNames.append(PcdNames)\r
356 \r
357 \r
358 return DependentProtocolCNames, DependentPpiCNames, DependentGuidCNames, DependentPcdNames\r
359\r
360##\r
361# Check if protocol CName is redefined\r
362#\r
363def CheckProtoclCNameRedefined(Module, DependentProtocolCNames):\r
364 for ProtocolInModule in Module.GetProtocolList():\r
365 IsCNameDefined = False\r
366 for PackageProtocolCNames in DependentProtocolCNames:\r
367 if ProtocolInModule.GetCName() in PackageProtocolCNames:\r
368 if IsCNameDefined:\r
369 Logger.Error("\nUPT", FORMAT_INVALID, \r
370 File = Module.GetFullPath(), \r
371 ExtraData = \\r
372 ST.ERR_INF_PARSER_ITEM_DUPLICATE_IN_DEC % ProtocolInModule.GetCName())\r
373 else:\r
374 IsCNameDefined = True\r
375\r
376##\r
377# Check if Ppi CName is redefined\r
378#\r
379def CheckPpiCNameRedefined(Module, DependentPpiCNames):\r
380 for PpiInModule in Module.GetPpiList():\r
381 IsCNameDefined = False\r
382 for PackagePpiCNames in DependentPpiCNames:\r
383 if PpiInModule.GetCName() in PackagePpiCNames:\r
384 if IsCNameDefined:\r
385 Logger.Error("\nUPT", FORMAT_INVALID, \r
386 File = Module.GetFullPath(), \r
387 ExtraData = ST.ERR_INF_PARSER_ITEM_DUPLICATE_IN_DEC % PpiInModule.GetCName())\r
388 else:\r
389 IsCNameDefined = True \r
390\r
391##\r
392# Check if Guid CName is redefined\r
393#\r
394def CheckGuidCNameRedefined(Module, DependentGuidCNames):\r
395 for GuidInModule in Module.GetGuidList():\r
396 IsCNameDefined = False\r
397 for PackageGuidCNames in DependentGuidCNames:\r
398 if GuidInModule.GetCName() in PackageGuidCNames:\r
399 if IsCNameDefined:\r
400 Logger.Error("\nUPT", FORMAT_INVALID, \r
401 File = Module.GetFullPath(), \r
402 ExtraData = \\r
403 ST.ERR_INF_PARSER_ITEM_DUPLICATE_IN_DEC % GuidInModule.GetCName())\r
404 else:\r
405 IsCNameDefined = True\r
406\r
407##\r
408# Check if PcdName is redefined\r
409#\r
410def CheckPcdNameRedefined(Module, DependentPcdNames):\r
411 PcdObjs = []\r
412 if not Module.GetBinaryFileList():\r
413 PcdObjs += Module.GetPcdList()\r
414 else:\r
415 Binary = Module.GetBinaryFileList()[0]\r
416 for AsBuild in Binary.GetAsBuiltList():\r
417 PcdObjs += AsBuild.GetPatchPcdList() + AsBuild.GetPcdExList()\r
418\r
419 for PcdObj in PcdObjs:\r
420 PcdName = '.'.join([PcdObj.GetTokenSpaceGuidCName(), PcdObj.GetCName()])\r
421 IsPcdNameDefined = False\r
422 for PcdNames in DependentPcdNames:\r
423 if PcdName in PcdNames:\r
424 if IsPcdNameDefined:\r
425 Logger.Error("\nUPT", FORMAT_INVALID, \r
426 File = Module.GetFullPath(), \r
427 ExtraData = ST.ERR_INF_PARSER_ITEM_DUPLICATE_IN_DEC % PcdName)\r
428 else:\r
429 IsPcdNameDefined = True\r
430\r
431##\r
432# Check if any Protocol/Ppi/Guid and Pcd name is redefined in its dependent DEC files\r
433#\r
434def CheckCNameInModuleRedefined(Module, DistPkg):\r
435 DePackageObjList = []\r
436 #\r
437 # Get all dependent package objects\r
438 # \r
439 for Obj in Module.GetPackageDependencyList():\r
440 Guid = Obj.GetGuid()\r
441 Version = Obj.GetVersion()\r
442 for Key in DistPkg.PackageSurfaceArea:\r
443 if Key[0] == Guid and Key[1] == Version:\r
444 if DistPkg.PackageSurfaceArea[Key] not in DePackageObjList:\r
445 DePackageObjList.append(DistPkg.PackageSurfaceArea[Key])\r
446 \r
447 DependentProtocolCNames, DependentPpiCNames, DependentGuidCNames, DependentPcdNames = \\r
448 GetDepProtocolPpiGuidPcdNames(DePackageObjList)\r
449\r
450 CheckProtoclCNameRedefined(Module, DependentProtocolCNames)\r
451 CheckPpiCNameRedefined(Module, DependentPpiCNames)\r
452 CheckGuidCNameRedefined(Module, DependentGuidCNames)\r
453 CheckPcdNameRedefined(Module, DependentPcdNames)\r
454\r
4234283c
LG
455## GenToolMisc\r
456#\r
457# GenToolMisc\r
458#\r
459#\r
460def GenToolMisc(DistPkg, WorkspaceDir, ContentZipFile):\r
461 ToolObject = DistPkg.Tools\r
462 MiscObject = DistPkg.MiscellaneousFiles\r
463 DistPkg.FileList = []\r
464 FileList = []\r
465 ToolFileNum = 0\r
466 FileNum = 0\r
467 RootDir = WorkspaceDir\r
468 \r
469 #\r
470 # FileList stores both tools files and misc files\r
471 # Misc file list must be appended to FileList *AFTER* Tools file list\r
472 #\r
473 if ToolObject:\r
474 FileList += ToolObject.GetFileList()\r
475 ToolFileNum = len(ToolObject.GetFileList())\r
476 if 'EDK_TOOLS_PATH' in os.environ:\r
477 RootDir = os.environ['EDK_TOOLS_PATH']\r
478 if MiscObject:\r
479 FileList += MiscObject.GetFileList()\r
480 for FileObject in FileList:\r
481 FileNum += 1\r
482 if FileNum > ToolFileNum:\r
483 #\r
484 # Misc files, root should be changed to WORKSPACE\r
485 #\r
486 RootDir = WorkspaceDir\r
487 File = ConvertPath(FileObject.GetURI())\r
488 ToFile = os.path.normpath(os.path.join(RootDir, File))\r
489 if os.path.exists(ToFile):\r
490 Logger.Info( ST.WRN_FILE_EXISTED % ToFile )\r
491 #\r
492 # ask for user input the new file name\r
493 #\r
494 Logger.Info( ST.MSG_NEW_FILE_NAME)\r
495 Input = stdin.readline()\r
496 Input = Input.replace('\r', '').replace('\n', '')\r
497 OrigPath = os.path.split(ToFile)[0]\r
498 ToFile = os.path.normpath(os.path.join(OrigPath, Input))\r
499 FromFile = os.path.join(FileObject.GetURI())\r
500 Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, DistPkg.Header.ReadOnly, FileObject.GetExecutable())\r
501 DistPkg.FileList.append((ToFile, Md5Sum))\r
502\r
503## Tool entrance method\r
504#\r
505# This method mainly dispatch specific methods per the command line options.\r
506# If no error found, return zero value so the caller of this tool can know\r
507# if it's executed successfully or not.\r
508#\r
509# @param Options: command Options\r
510#\r
511def Main(Options = None):\r
512 ContentZipFile, DistFile = None, None\r
513\r
514 try:\r
421ccda3 515 DataBase = GlobalData.gDB\r
4234283c
LG
516 WorkspaceDir = GlobalData.gWORKSPACE\r
517 if not Options.PackageFile:\r
518 Logger.Error("InstallPkg", OPTION_MISSING, ExtraData=ST.ERR_SPECIFY_PACKAGE)\r
519 \r
520 #\r
521 # unzip dist.pkg file\r
522 #\r
421ccda3 523 DistPkg, ContentZipFile, DpPkgFileName, DistFile = UnZipDp(WorkspaceDir, Options.PackageFile)\r
4234283c 524\r
4234283c 525 #\r
421ccda3 526 # check dependency\r
4234283c 527 #\r
421ccda3
HC
528 Dep = DependencyRules(DataBase)\r
529 CheckInstallDpx(Dep, DistPkg)\r
4234283c
LG
530\r
531 #\r
421ccda3 532 # Install distribution\r
4234283c 533 #\r
421ccda3 534 InstallDp(DistPkg, DpPkgFileName, ContentZipFile, Options, Dep, WorkspaceDir, DataBase)\r
4234283c
LG
535 ReturnCode = 0\r
536 \r
537 except FatalError, XExcept:\r
538 ReturnCode = XExcept.args[0]\r
539 if Logger.GetLevel() <= Logger.DEBUG_9:\r
421ccda3
HC
540 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + format_exc())\r
541 \r
4234283c
LG
542 except KeyboardInterrupt:\r
543 ReturnCode = ABORT_ERROR\r
544 if Logger.GetLevel() <= Logger.DEBUG_9:\r
421ccda3
HC
545 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + format_exc())\r
546 \r
4234283c
LG
547 except:\r
548 ReturnCode = CODE_ERROR\r
549 Logger.Error(\r
550 "\nInstallPkg",\r
551 CODE_ERROR,\r
552 ST.ERR_UNKNOWN_FATAL_INSTALL_ERR % Options.PackageFile,\r
553 ExtraData=ST.MSG_SEARCH_FOR_HELP,\r
554 RaiseError=False\r
555 )\r
556 Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(),\r
557 platform) + format_exc())\r
4234283c 558 finally:\r
af0c61ae
HC
559 if ReturnCode != UPT_ALREADY_INSTALLED_ERROR:\r
560 Logger.Quiet(ST.MSG_REMOVE_TEMP_FILE_STARTED)\r
561 if DistFile:\r
562 DistFile.Close()\r
563 if ContentZipFile:\r
564 ContentZipFile.Close()\r
565 if GlobalData.gUNPACK_DIR:\r
566 rmtree(GlobalData.gUNPACK_DIR)\r
567 GlobalData.gUNPACK_DIR = None\r
568 Logger.Quiet(ST.MSG_REMOVE_TEMP_FILE_DONE)\r
4234283c
LG
569 if ReturnCode == 0:\r
570 Logger.Quiet(ST.MSG_FINISH)\r
4234283c
LG
571 return ReturnCode\r
572\r
421ccda3
HC
573# BackupDist method\r
574# \r
575# This method will backup the Distribution file into the $(WORKSPACE)/conf/upt, and rename it \r
576# if there is already a same-named distribution existed.\r
577#\r
578# @param DpPkgFileName: The distribution path\r
579# @param Guid: The distribution Guid\r
580# @param Version: The distribution Version\r
581# @param WorkspaceDir: The workspace directory\r
582# @retval NewDpPkgFileName: The exact backup file name\r
583#\r
584def BackupDist(DpPkgFileName, Guid, Version, WorkspaceDir):\r
585 DistFileName = os.path.split(DpPkgFileName)[1]\r
586 DestDir = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gUPT_DIR))\r
587 CreateDirectory(DestDir)\r
588 DestFile = os.path.normpath(os.path.join(DestDir, DistFileName))\r
589 if os.path.exists(DestFile):\r
590 FileName, Ext = os.path.splitext(DistFileName)\r
591 NewFileName = FileName + '_' + Guid + '_' + Version + Ext\r
592 DestFile = os.path.normpath(os.path.join(DestDir, NewFileName))\r
593 if os.path.exists(DestFile):\r
594 #\r
595 # ask for user input the new file name\r
596 #\r
597 Logger.Info( ST.MSG_NEW_FILE_NAME_FOR_DIST)\r
598 Input = stdin.readline()\r
599 Input = Input.replace('\r', '').replace('\n', '')\r
600 DestFile = os.path.normpath(os.path.join(DestDir, Input))\r
601 copyfile(DpPkgFileName, DestFile)\r
602 NewDpPkgFileName = DestFile[DestFile.find(DestDir) + len(DestDir) + 1:]\r
603 return NewDpPkgFileName\r
604\r
605## CheckInstallDpx method\r
606#\r
607# check whether distribution could be installed\r
608#\r
609# @param Dep: the DependencyRules instance that used to check dependency\r
610# @param DistPkg: the distribution object\r
611#\r
612def CheckInstallDpx(Dep, DistPkg):\r
613 #\r
614 # Check distribution package installed or not\r
615 #\r
616 if Dep.CheckDpExists(DistPkg.Header.GetGuid(),\r
617 DistPkg.Header.GetVersion()):\r
618 Logger.Error("InstallPkg", UPT_ALREADY_INSTALLED_ERROR,\r
619 ST.WRN_DIST_PKG_INSTALLED)\r
620 #\r
621 # Check distribution dependency (all module dependency should be\r
622 # satisfied)\r
623 #\r
624 if not Dep.CheckInstallDpDepexSatisfied(DistPkg):\r
625 Logger.Error("InstallPkg", UNKNOWN_ERROR,\r
626 ST.ERR_PACKAGE_NOT_MATCH_DEPENDENCY,\r
627 ExtraData=DistPkg.Header.Name)\r
628\r
4234283c
LG
629## InstallModuleContent method\r
630#\r
631# If this is standalone module, then Package should be none,\r
632# ModulePath should be ''\r
633# @param FromPath: FromPath\r
634# @param NewPath: NewPath\r
635# @param ModulePath: ModulePath\r
636# @param Module: Module\r
637# @param ContentZipFile: ContentZipFile\r
638# @param WorkspaceDir: WorkspaceDir\r
639# @param ModuleList: ModuleList\r
640# @param Package: Package\r
641#\r
642def InstallModuleContent(FromPath, NewPath, ModulePath, Module, ContentZipFile,\r
643 WorkspaceDir, ModuleList, Package = None, ReadOnly = False):\r
644 \r
645 if NewPath.startswith("\\") or NewPath.startswith("/"):\r
646 NewPath = NewPath[1:]\r
647 \r
648 if not IsValidInstallPath(NewPath):\r
649 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%NewPath) \r
650 \r
651 NewModuleFullPath = os.path.normpath(os.path.join(WorkspaceDir, NewPath,\r
652 ConvertPath(ModulePath)))\r
653 Module.SetFullPath(os.path.normpath(os.path.join(NewModuleFullPath,\r
654 ConvertPath(Module.GetName()) + '.inf')))\r
655 Module.FileList = []\r
656 \r
657 for MiscFile in Module.GetMiscFileList():\r
658 if not MiscFile:\r
659 continue\r
660 for Item in MiscFile.GetFileList():\r
661 File = Item.GetURI()\r
662 if File.startswith("\\") or File.startswith("/"):\r
663 File = File[1:]\r
664 \r
665 if not IsValidInstallPath(File):\r
666 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%File)\r
421ccda3 667\r
4234283c
LG
668 FromFile = os.path.join(FromPath, ModulePath, File)\r
669 Executable = Item.GetExecutable() \r
670 ToFile = os.path.normpath(os.path.join(NewModuleFullPath, ConvertPath(File)))\r
671 Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly, Executable)\r
672 if Package and ((ToFile, Md5Sum) not in Package.FileList):\r
673 Package.FileList.append((ToFile, Md5Sum))\r
674 elif Package:\r
675 continue\r
676 elif (ToFile, Md5Sum) not in Module.FileList:\r
677 Module.FileList.append((ToFile, Md5Sum))\r
678 for Item in Module.GetSourceFileList():\r
679 File = Item.GetSourceFile()\r
680 if File.startswith("\\") or File.startswith("/"):\r
681 File = File[1:]\r
682 \r
683 if not IsValidInstallPath(File):\r
684 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%File) \r
685 \r
686 FromFile = os.path.join(FromPath, ModulePath, File)\r
687 ToFile = os.path.normpath(os.path.join(NewModuleFullPath, ConvertPath(File)))\r
688 Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly)\r
689 if Package and ((ToFile, Md5Sum) not in Package.FileList):\r
690 Package.FileList.append((ToFile, Md5Sum))\r
691 elif Package:\r
692 continue\r
693 elif (ToFile, Md5Sum) not in Module.FileList:\r
694 Module.FileList.append((ToFile, Md5Sum))\r
695 for Item in Module.GetBinaryFileList():\r
696 FileNameList = Item.GetFileNameList()\r
697 for FileName in FileNameList: \r
698 File = FileName.GetFilename() \r
699 if File.startswith("\\") or File.startswith("/"):\r
700 File = File[1:]\r
701 \r
702 if not IsValidInstallPath(File):\r
703 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%File)\r
704\r
705 FromFile = os.path.join(FromPath, ModulePath, File)\r
706 ToFile = os.path.normpath(os.path.join(NewModuleFullPath, ConvertPath(File)))\r
707 Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly) \r
708 if Package and ((ToFile, Md5Sum) not in Package.FileList):\r
709 Package.FileList.append((ToFile, Md5Sum))\r
710 elif Package:\r
711 continue\r
712 elif (ToFile, Md5Sum) not in Module.FileList:\r
713 Module.FileList.append((ToFile, Md5Sum))\r
714 \r
715 InstallModuleContentZipFile(ContentZipFile, FromPath, ModulePath, WorkspaceDir, NewPath, Module, Package, ReadOnly,\r
716 ModuleList)\r
717\r
718## InstallModuleContentZipFile\r
719#\r
720# InstallModuleContentZipFile\r
721#\r
722def InstallModuleContentZipFile(ContentZipFile, FromPath, ModulePath, WorkspaceDir, NewPath, Module, Package, ReadOnly,\r
723 ModuleList):\r
724 #\r
725 # Extract other files under current module path in content Zip file but not listed in the description \r
726 #\r
727 if ContentZipFile:\r
728 for FileName in ContentZipFile.GetZipFile().namelist():\r
729 FileName = os.path.normpath(FileName)\r
730 CheckPath = os.path.normpath(os.path.join(FromPath, ModulePath))\r
731 if FileUnderPath(FileName, CheckPath):\r
732 if FileName.startswith("\\") or FileName.startswith("/"):\r
733 FileName = FileName[1:]\r
734 \r
735 if not IsValidInstallPath(FileName):\r
736 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%FileName)\r
737 \r
738 FromFile = FileName\r
739 ToFile = os.path.normpath(os.path.join(WorkspaceDir, \r
740 ConvertPath(FileName.replace(FromPath, NewPath, 1))))\r
421ccda3 741 CheckList = copy.copy(Module.FileList)\r
4234283c
LG
742 if Package:\r
743 CheckList += Package.FileList\r
744 for Item in CheckList:\r
745 if Item[0] == ToFile:\r
746 break\r
747 else:\r
748 Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly)\r
749 if Package and ((ToFile, Md5Sum) not in Package.FileList):\r
750 Package.FileList.append((ToFile, Md5Sum))\r
751 elif Package:\r
752 continue\r
753 elif (ToFile, Md5Sum) not in Module.FileList:\r
754 Module.FileList.append((ToFile, Md5Sum)) \r
755 \r
756 ModuleList.append((Module, Package))\r
757\r
758## FileUnderPath\r
759# Check whether FileName started with directory specified by CheckPath \r
760#\r
761# @param FileName: the FileName need to be checked\r
762# @param CheckPath: the path need to be checked against\r
763# @return: True or False \r
764#\r
765def FileUnderPath(FileName, CheckPath):\r
766 FileName = FileName.replace('\\', '/')\r
767 FileName = os.path.normpath(FileName)\r
768 CheckPath = CheckPath.replace('\\', '/')\r
769 CheckPath = os.path.normpath(CheckPath)\r
770 if FileName.startswith(CheckPath):\r
771 RemainingPath = os.path.normpath(FileName.replace(CheckPath, '', 1))\r
772 while RemainingPath.startswith('\\') or RemainingPath.startswith('/'):\r
773 RemainingPath = RemainingPath[1:]\r
774 if FileName == os.path.normpath(os.path.join(CheckPath, RemainingPath)):\r
775 return True\r
776 \r
777 return False\r
778\r
779## InstallFile\r
780# Extract File from Zipfile, set file attribute, and return the Md5Sum\r
781#\r
782# @return: True or False \r
783#\r
784def InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly, Executable=False):\r
421ccda3
HC
785 if os.path.exists(os.path.normpath(ToFile)):\r
786 pass\r
787 else:\r
788 if not ContentZipFile or not ContentZipFile.UnpackFile(FromFile, ToFile):\r
789 Logger.Error("UPT", FILE_NOT_FOUND, ST.ERR_INSTALL_FILE_FROM_EMPTY_CONTENT % FromFile)\r
4234283c 790\r
421ccda3
HC
791 if ReadOnly:\r
792 if not Executable:\r
793 chmod(ToFile, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)\r
794 else:\r
795 chmod(ToFile, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)\r
796 elif Executable:\r
797 chmod(ToFile, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR | stat.S_IWGRP |\r
798 stat.S_IWOTH | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)\r
4234283c 799 else:\r
421ccda3 800 chmod(ToFile, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)\r
4234283c 801 \r
421ccda3 802 Md5Sigature = md5.new(__FileHookOpen__(str(ToFile), 'rb').read())\r
4234283c 803 Md5Sum = Md5Sigature.hexdigest()\r
421ccda3 804\r
4234283c 805 return Md5Sum\r
421ccda3 806\r
4234283c
LG
807## InstallPackageContent method\r
808#\r
809# @param FromPath: FromPath\r
810# @param ToPath: ToPath\r
811# @param Package: Package\r
812# @param ContentZipFile: ContentZipFile\r
813# @param Dep: Dep\r
814# @param WorkspaceDir: WorkspaceDir\r
815# @param ModuleList: ModuleList\r
816#\r
817def InstallPackageContent(FromPath, ToPath, Package, ContentZipFile, Dep,\r
818 WorkspaceDir, ModuleList, ReadOnly = False):\r
819 if Dep:\r
820 pass\r
821 Package.FileList = []\r
822 \r
823 if ToPath.startswith("\\") or ToPath.startswith("/"):\r
824 ToPath = ToPath[1:]\r
825 \r
826 if not IsValidInstallPath(ToPath):\r
827 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%ToPath) \r
828\r
829 if FromPath.startswith("\\") or FromPath.startswith("/"):\r
830 FromPath = FromPath[1:]\r
831 \r
832 if not IsValidInstallPath(FromPath):\r
833 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%FromPath) \r
834 \r
835 PackageFullPath = os.path.normpath(os.path.join(WorkspaceDir, ToPath))\r
836 for MiscFile in Package.GetMiscFileList():\r
837 for Item in MiscFile.GetFileList():\r
838 FileName = Item.GetURI()\r
839 if FileName.startswith("\\") or FileName.startswith("/"):\r
840 FileName = FileName[1:]\r
841 \r
842 if not IsValidInstallPath(FileName):\r
843 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%FileName)\r
844 \r
845 FromFile = os.path.join(FromPath, FileName)\r
846 Executable = Item.GetExecutable()\r
847 ToFile = (os.path.join(PackageFullPath, ConvertPath(FileName)))\r
848 Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly, Executable)\r
849 if (ToFile, Md5Sum) not in Package.FileList:\r
850 Package.FileList.append((ToFile, Md5Sum))\r
851 PackageIncludeArchList = [] \r
852 for Item in Package.GetPackageIncludeFileList():\r
853 FileName = Item.GetFilePath()\r
854 if FileName.startswith("\\") or FileName.startswith("/"):\r
855 FileName = FileName[1:]\r
856 \r
857 if not IsValidInstallPath(FileName):\r
858 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%FileName) \r
859 \r
860 FromFile = os.path.join(FromPath, FileName)\r
861 ToFile = os.path.normpath(os.path.join(PackageFullPath, ConvertPath(FileName)))\r
862 RetFile = ContentZipFile.UnpackFile(FromFile, ToFile)\r
863 if RetFile == '':\r
864 #\r
865 # a non-exist path in Zipfile will return '', which means an include directory in our case\r
866 # save the information for later DEC creation usage and also create the directory\r
867 #\r
868 PackageIncludeArchList.append([Item.GetFilePath(), Item.GetSupArchList()])\r
869 CreateDirectory(ToFile)\r
870 continue\r
871 if ReadOnly:\r
421ccda3 872 chmod(ToFile, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)\r
4234283c 873 else:\r
421ccda3
HC
874 chmod(ToFile, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH|stat.S_IWUSR|stat.S_IWGRP|stat.S_IWOTH) \r
875 Md5Sigature = md5.new(__FileHookOpen__(str(ToFile), 'rb').read())\r
4234283c
LG
876 Md5Sum = Md5Sigature.hexdigest()\r
877 if (ToFile, Md5Sum) not in Package.FileList:\r
878 Package.FileList.append((ToFile, Md5Sum))\r
879 Package.SetIncludeArchList(PackageIncludeArchList)\r
880 \r
881 for Item in Package.GetStandardIncludeFileList():\r
882 FileName = Item.GetFilePath()\r
883 if FileName.startswith("\\") or FileName.startswith("/"):\r
884 FileName = FileName[1:]\r
885 \r
886 if not IsValidInstallPath(FileName):\r
887 Logger.Error("UPT", FORMAT_INVALID, ST.ERR_FILE_NAME_INVALIDE%FileName) \r
888 \r
889 FromFile = os.path.join(FromPath, FileName)\r
890 ToFile = os.path.normpath(os.path.join(PackageFullPath, ConvertPath(FileName)))\r
891 Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly)\r
892 if (ToFile, Md5Sum) not in Package.FileList:\r
893 Package.FileList.append((ToFile, Md5Sum))\r
894\r
895 #\r
896 # Update package\r
897 #\r
898 Package.SetPackagePath(Package.GetPackagePath().replace(FromPath,\r
899 ToPath, 1))\r
900 Package.SetFullPath(os.path.normpath(os.path.join(PackageFullPath,\r
901 ConvertPath(Package.GetName()) + '.dec')))\r
902\r
903 #\r
904 # Install files in module\r
905 #\r
906 Module = None\r
907 ModuleDict = Package.GetModuleDict()\r
d0acc87a
LG
908 for ModuleGuid, ModuleVersion, ModuleName, ModulePath in ModuleDict:\r
909 Module = ModuleDict[ModuleGuid, ModuleVersion, ModuleName, ModulePath]\r
4234283c
LG
910 InstallModuleContent(FromPath, ToPath, ModulePath, Module,\r
911 ContentZipFile, WorkspaceDir, ModuleList, Package, ReadOnly)\r
912\r
913## GetDPFile method\r
914#\r
915# @param ZipFile: A ZipFile\r
916#\r
917def GetDPFile(ZipFile):\r
918 ContentFile = ''\r
919 DescFile = ''\r
920 for FileName in ZipFile.namelist():\r
921 if FileName.endswith('.content'):\r
922 if not ContentFile:\r
923 ContentFile = FileName\r
924 continue\r
925 elif FileName.endswith('.pkg'):\r
926 if not DescFile:\r
927 DescFile = FileName\r
928 continue\r
929 else:\r
930 continue\r
931 \r
932 Logger.Error("PackagingTool", FILE_TYPE_MISMATCH,\r
933 ExtraData=ST.ERR_DIST_FILE_TOOMANY)\r
934 if not DescFile or not ContentFile:\r
935 Logger.Error("PackagingTool", FILE_UNKNOWN_ERROR,\r
936 ExtraData=ST.ERR_DIST_FILE_TOOFEW)\r
937 return DescFile, ContentFile\r
938\r
421ccda3
HC
939## InstallDp method\r
940#\r
941# Install the distribution to current workspace\r
942#\r
943def InstallDp(DistPkg, DpPkgFileName, ContentZipFile, Options, Dep, WorkspaceDir, DataBase):\r
944 #\r
945 # PackageList, ModuleList record the information for the meta-data\r
946 # files that need to be generated later\r
947 #\r
948 PackageList = []\r
949 ModuleList = []\r
950 DistPkg.PackageSurfaceArea = GetPackageList(DistPkg, Dep, WorkspaceDir, Options, \r
951 ContentZipFile, ModuleList, PackageList)\r
952\r
953 DistPkg.ModuleSurfaceArea = GetModuleList(DistPkg, Dep, WorkspaceDir, ContentZipFile, ModuleList)\r
954 \r
955 GenToolMisc(DistPkg, WorkspaceDir, ContentZipFile)\r
956 \r
957 #\r
958 # copy "Distribution File" to directory $(WORKSPACE)/conf/upt\r
959 #\r
960 DistFileName = os.path.split(DpPkgFileName)[1]\r
961 NewDpPkgFileName = BackupDist(DpPkgFileName, DistPkg.Header.GetGuid(), DistPkg.Header.GetVersion(), WorkspaceDir)\r
962\r
963 #\r
964 # update database\r
965 #\r
966 Logger.Quiet(ST.MSG_UPDATE_PACKAGE_DATABASE)\r
967 DataBase.AddDPObject(DistPkg, NewDpPkgFileName, DistFileName, \r
968 DistPkg.Header.RePackage)\r
969\r