def CheckForExistingDp(Path): if os.path.exists(Path): Logger.Info(ST.MSG_DISTRIBUTION_PACKAGE_FILE_EXISTS % Path) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') if Input.upper() != "Y": Logger.Error("\nMkPkg", ABORT_ERROR, ST.ERR_USER_ABORT, RaiseError=True)
def InstallNewModule(WorkspaceDir, Path, PathList=None): if PathList is None: PathList = [] Path = ConvertPath(Path) Path = os.path.normpath(Path) FullPath = os.path.normpath(os.path.join(WorkspaceDir, Path)) if os.path.exists(FullPath) and FullPath not in PathList: Logger.Info(ST.ERR_DIR_ALREADY_EXIST % Path) elif Path == FullPath: Logger.Info(ST.MSG_RELATIVE_PATH_ONLY % FullPath) else: return Path Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') if Input == '': Logger.Error("InstallPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT) Input = Input.replace('\r', '').replace('\n', '') return InstallNewModule(WorkspaceDir, Input, PathList)
def GenToolMisc(DistPkg, WorkspaceDir, ContentZipFile): ToolObject = DistPkg.Tools MiscObject = DistPkg.MiscellaneousFiles DistPkg.FileList = [] FileList = [] ToolFileNum = 0 FileNum = 0 RootDir = WorkspaceDir # # FileList stores both tools files and misc files # Misc file list must be appended to FileList *AFTER* Tools file list # if ToolObject: FileList += ToolObject.GetFileList() ToolFileNum = len(ToolObject.GetFileList()) if 'EDK_TOOLS_PATH' in os.environ: RootDir = os.environ['EDK_TOOLS_PATH'] if MiscObject: FileList += MiscObject.GetFileList() for FileObject in FileList: FileNum += 1 if FileNum > ToolFileNum: # # Misc files, root should be changed to WORKSPACE # RootDir = WorkspaceDir File = ConvertPath(FileObject.GetURI()) ToFile = os.path.normpath(os.path.join(RootDir, File)) if os.path.exists(ToFile): Logger.Info( ST.WRN_FILE_EXISTED % ToFile ) # # ask for user input the new file name # Logger.Info( ST.MSG_NEW_FILE_NAME) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') OrigPath = os.path.split(ToFile)[0] ToFile = os.path.normpath(os.path.join(OrigPath, Input)) FromFile = os.path.join(FileObject.GetURI()) Md5Sum = InstallFile(ContentZipFile, FromFile, ToFile, DistPkg.Header.ReadOnly, FileObject.GetExecutable()) DistPkg.FileList.append((ToFile, Md5Sum))
def InstallNewPackage(WorkspaceDir, Path, CustomPath=False): if os.path.isabs(Path): Logger.Info(ST.MSG_RELATIVE_PATH_ONLY % Path) elif CustomPath: Logger.Info(ST.MSG_NEW_PKG_PATH) else: Path = ConvertPath(Path) Path = os.path.normpath(Path) FullPath = os.path.normpath(os.path.join(WorkspaceDir, Path)) if os.path.exists(FullPath): Logger.Info(ST.ERR_DIR_ALREADY_EXIST % FullPath) else: return Path Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') if Input == '': Logger.Error("InstallPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT) Input = Input.replace('\r', '').replace('\n', '') return InstallNewPackage(WorkspaceDir, Input, False)
def InstallNewFile(WorkspaceDir, File): FullPath = os.path.normpath(os.path.join(WorkspaceDir, File)) if os.path.exists(FullPath): Logger.Info(ST.ERR_FILE_ALREADY_EXIST % File) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') if Input == '': Logger.Error("InstallPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT) Input = Input.replace('\r', '').replace('\n', '') return InstallNewFile(WorkspaceDir, Input) else: return File
def InventoryDistInstalled(DataBase): DistInstalled = DataBase.InventoryDistInstalled() # # find the max length for each item # DpNameStr = "DpName" DpGuidStr = "DpGuid" DpVerStr = "DpVer" DpOriginalNameStr = "DpOriginalName" MaxGuidlen = len(DpGuidStr) MaxVerlen = len(DpVerStr) MaxDpAliasFileNameLen = len(DpNameStr) MaxDpOrigFileNamelen = len(DpOriginalNameStr) for (DpGuid, DpVersion, DpOriginalName, DpAliasFileName) in DistInstalled: MaxGuidlen = max(MaxGuidlen, len(DpGuid)) MaxVerlen = max(MaxVerlen, len(DpVersion)) MaxDpAliasFileNameLen = max(MaxDpAliasFileNameLen, len(DpAliasFileName)) MaxDpOrigFileNamelen = max(MaxDpOrigFileNamelen, len(DpOriginalName)) OutMsgFmt = "%-*s\t%-*s\t%-*s\t%-s" OutMsg = OutMsgFmt % (MaxDpAliasFileNameLen, DpNameStr, MaxGuidlen, DpGuidStr, MaxVerlen, DpVerStr, DpOriginalNameStr) Logger.Info(OutMsg) for (DpGuid, DpVersion, DpFileName, DpAliasFileName) in DistInstalled: OutMsg = OutMsgFmt % (MaxDpAliasFileNameLen, DpAliasFileName, MaxGuidlen, DpGuid, MaxVerlen, DpVersion, DpFileName) Logger.Info(OutMsg)
def GetPackageList(DistPkg, Dep, WorkspaceDir, Options, ContentZipFile, ModuleList, PackageList): NewDict = Sdict() for Guid, Version, Path in DistPkg.PackageSurfaceArea: PackagePath = Path Package = DistPkg.PackageSurfaceArea[Guid, Version, Path] Logger.Info(ST.MSG_INSTALL_PACKAGE % Package.GetName()) if Dep.CheckPackageExists(Guid, Version): Logger.Info(ST.WRN_PACKAGE_EXISTED % (Guid, Version)) if Options.UseGuidedPkgPath: GuidedPkgPath = "%s_%s_%s" % (Package.GetName(), Guid, Version) NewPackagePath = InstallNewPackage(WorkspaceDir, GuidedPkgPath, Options.CustomPath) else: NewPackagePath = InstallNewPackage(WorkspaceDir, PackagePath, Options.CustomPath) InstallPackageContent(PackagePath, NewPackagePath, Package, ContentZipFile, Dep, WorkspaceDir, ModuleList, DistPkg.Header.ReadOnly) PackageList.append(Package) NewDict[Guid, Version, Package.GetPackagePath()] = Package # # Now generate meta-data files, first generate all dec for package # dec should be generated before inf, and inf should be generated after # all packages installed, else hard to resolve modules' package # dependency (Hard to get the location of the newly installed package) # for Package in PackageList: FilePath = PackageToDec(Package, DistPkg.Header) Md5Sigature = md5.new(__FileHookOpen__(str(FilePath), 'rb').read()) Md5Sum = Md5Sigature.hexdigest() if (FilePath, Md5Sum) not in Package.FileList: Package.FileList.append((FilePath, Md5Sum)) return NewDict
def CheckDpDepex(Dep, Guid, Version, WorkspaceDir): (Removable, DependModuleList) = Dep.CheckDpDepexForRemove(Guid, Version) if not Removable: Logger.Info(ST.MSG_CONFIRM_REMOVE) Logger.Info(ST.MSG_USER_DELETE_OP) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') if Input.upper() != 'Y': Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_USER_INTERRUPT) return 1 else: # # report list of modules that are not valid due to force # remove, # also generate a log file for reference # Logger.Info(ST.MSG_INVALID_MODULE_INTRODUCED) LogFilePath = os.path.normpath( os.path.join(WorkspaceDir, GlobalData.gINVALID_MODULE_FILE)) Logger.Info(ST.MSG_CHECK_LOG_FILE % LogFilePath) try: LogFile = open(LogFilePath, 'w') try: for ModulePath in DependModuleList: LogFile.write("%s\n" % ModulePath) Logger.Info(ModulePath) except IOError: Logger.Warn("\nRmPkg", ST.ERR_FILE_WRITE_FAILURE, File=LogFilePath) except IOError: Logger.Warn("\nRmPkg", ST.ERR_FILE_OPEN_FAILURE, File=LogFilePath) finally: LogFile.close()
def VerifyRemoveModuleDep(Path, DpPackagePathList): try: for Item in GetPackagePath(Path): if Item in DpPackagePathList: DecPath = os.path.normpath( os.path.join(GlobalData.gWORKSPACE, Item)) Logger.Info(ST.MSG_MODULE_DEPEND_ON % (Path, DecPath)) return False else: return True except FatalError as ErrCode: if ErrCode.message == EDK1_INF_ERROR: Logger.Warn("UPT", ST.WRN_EDK1_INF_FOUND % Path) return True else: return True
def PackFile(self, File, ArcName=None): try: # # avoid packing same file multiple times # if platform.system() != 'Windows': File = File.replace('\\', '/') ZipedFilesNameList = self._ZipFile.namelist() for ZipedFile in ZipedFilesNameList: if File == os.path.normpath(ZipedFile): return Logger.Info("packing ..." + File) self._ZipFile.write(File, ArcName) except BaseException, Xstr: Logger.Error("PackagingTool", FILE_COMPRESS_FAILURE, ExtraData="%s (%s)" % (File, str(Xstr)))
def VerifyReplaceModuleDep(Path, DpPackagePathList, OtherPkgList): try: for Item in GetPackagePath(Path): if Item in DpPackagePathList: DecPath = os.path.normpath( os.path.join(GlobalData.gWORKSPACE, Item)) Name, Guid, Version = GetPkgInfoFromDec(DecPath) if (Guid, Version) not in OtherPkgList: Logger.Info(ST.MSG_MODULE_DEPEND_ON % (Path, DecPath)) return False else: return True except FatalError as ErrCode: if ErrCode.message == EDK1_INF_ERROR: Logger.Warn("UPT", ST.WRN_EDK1_INF_FOUND % Path) return True else: return True
def VerifyRemoveModuleDep(Path, DpPackagePathList): WorkSP = GlobalData.gWORKSPACE try: PomAli = InfPomAlignment(Path, WorkSP, Skip=True) for Item in PomAli.GetPackageDependencyList(): if Item.GetPackageFilePath() in DpPackagePathList: Logger.Info(ST.MSG_MODULE_DEPEND_ON % (Path, Item.GetPackageFilePath())) return False else: return True except FatalError, ErrCode: if ErrCode.message == EDK1_INF_ERROR: Logger.Warn("UPT", ST.WRN_EDK1_INF_FOUND % Path) return True else: return True
def BackupDist(DpPkgFileName, Guid, Version, WorkspaceDir): DistFileName = os.path.split(DpPkgFileName)[1] DestDir = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gUPT_DIR)) CreateDirectory(DestDir) DestFile = os.path.normpath(os.path.join(DestDir, DistFileName)) if os.path.exists(DestFile): FileName, Ext = os.path.splitext(DistFileName) NewFileName = FileName + '_' + Guid + '_' + Version + Ext DestFile = os.path.normpath(os.path.join(DestDir, NewFileName)) if os.path.exists(DestFile): # # ask for user input the new file name # Logger.Info(ST.MSG_NEW_FILE_NAME_FOR_DIST) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') DestFile = os.path.normpath(os.path.join(DestDir, Input)) copyfile(DpPkgFileName, DestFile) NewDpPkgFileName = DestFile[DestFile.find(DestDir) + len(DestDir) + 1:] return NewDpPkgFileName
def Unpack(self, ToDest): for FileN in self._ZipFile.namelist(): ToFile = os.path.normpath(os.path.join(ToDest, FileN)) Msg = "%s -> %s" % (FileN, ToFile) Logger.Info(Msg) self.Extract(FileN, ToFile)
def Main(Options = None): try: DataBase = GlobalData.gDB if not Options.DistributionFile: Logger.Error("RmPkg", OPTION_MISSING, ExtraData=ST.ERR_SPECIFY_PACKAGE) CheckEnvVariable() WorkspaceDir = GlobalData.gWORKSPACE # # Prepare check dependency # Dep = DependencyRules(DataBase) if Options.DistributionFile: (Guid, Version, NewDpFileName) = \ DataBase.GetDpByName(os.path.split(Options.DistributionFile)[1]) if not Guid: Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_PACKAGE_NOT_INSTALLED % Options.DistributionFile) else: Guid = Options.PackageGuid Version = Options.PackageVersion # # Check Dp existing # if not Dep.CheckDpExists(Guid, Version): Logger.Error("RmPkg", UNKNOWN_ERROR, ST.ERR_DISTRIBUTION_NOT_INSTALLED) # # Check for Distribution files existence in /conf/upt, if not exist, # Warn user and go on. # StoredDistFile = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gUPT_DIR, NewDpFileName)) if not os.path.isfile(StoredDistFile): Logger.Warn("RmPkg", ST.WRN_DIST_NOT_FOUND%StoredDistFile) StoredDistFile = None # # Check Dp depex # CheckDpDepex(Dep, Guid, Version, WorkspaceDir) # # Get Current File List # NewFileList = GetCurrentFileList(DataBase, Guid, Version, WorkspaceDir) # # Remove all files # MissingFileList = [] for (Path, Md5Sum) in DataBase.GetDpFileList(Guid, Version): if os.path.isfile(Path): if Path in NewFileList: NewFileList.remove(Path) if not Options.Yes: # # check whether modified by users # Md5Sigature = md5.new(open(str(Path), 'rb').read()) if Md5Sum != Md5Sigature.hexdigest(): Logger.Info(ST.MSG_CONFIRM_REMOVE2 % Path) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') if Input.upper() != 'Y': continue RemovePath(Path) else: MissingFileList.append(Path) for Path in NewFileList: if os.path.isfile(Path): if (not Options.Yes) and (not os.path.split(Path)[1].startswith('.')): Logger.Info(ST.MSG_CONFIRM_REMOVE3 % Path) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') if Input.upper() != 'Y': continue RemovePath(Path) # # Remove distribution files in /Conf/.upt # if StoredDistFile is not None: os.remove(StoredDistFile) # # update database # Logger.Quiet(ST.MSG_UPDATE_PACKAGE_DATABASE) DataBase.RemoveDpObj(Guid, Version) Logger.Quiet(ST.MSG_FINISH) ReturnCode = 0 except FatalError, XExcept: ReturnCode = XExcept.args[0] if Logger.GetLevel() <= Logger.DEBUG_9: Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + \ format_exc())
def Main(Options = None): ContentZipFile, DistFile = None, None try: DataBase = GlobalData.gDB CheckEnvVariable() WorkspaceDir = GlobalData.gWORKSPACE if not Options.PackageFile: Logger.Error("InstallPkg", OPTION_MISSING, ExtraData=ST.ERR_SPECIFY_PACKAGE) # # unzip dist.pkg file # DistPkg, Dep, ContentZipFile, DpPkgFileName = UnZipDp(WorkspaceDir, Options, DataBase) # # PackageList, ModuleList record the information for the meta-data # files that need to be generated later # PackageList = [] ModuleList = [] DistPkg.PackageSurfaceArea = GetPackageList(DistPkg, Dep, WorkspaceDir, Options, ContentZipFile, ModuleList, PackageList) DistPkg.ModuleSurfaceArea = GetModuleList(DistPkg, Dep, WorkspaceDir, ContentZipFile, ModuleList) GenToolMisc(DistPkg, WorkspaceDir, ContentZipFile) # # copy "Distribution File" to directory $(WORKSPACE)/conf/upt # DistFileName = os.path.split(DpPkgFileName)[1] DestDir = os.path.normpath(os.path.join(WorkspaceDir, GlobalData.gUPT_DIR)) CreateDirectory(DestDir) DestFile = os.path.normpath(os.path.join(DestDir, DistFileName)) if os.path.exists(DestFile): FileName, Ext = os.path.splitext(DistFileName) NewFileName = FileName + '_' + DistPkg.Header.GetGuid() + '_' + DistPkg.Header.GetVersion() + Ext DestFile = os.path.normpath(os.path.join(DestDir, NewFileName)) if os.path.exists(DestFile): # # ask for user input the new file name # Logger.Info( ST.MSG_NEW_FILE_NAME_FOR_DIST) Input = stdin.readline() Input = Input.replace('\r', '').replace('\n', '') DestFile = os.path.normpath(os.path.join(DestDir, Input)) copyfile(DpPkgFileName, DestFile) NewDpPkgFileName = DestFile[DestFile.find(DestDir) + len(DestDir) + 1:] # # update database # Logger.Quiet(ST.MSG_UPDATE_PACKAGE_DATABASE) DataBase.AddDPObject(DistPkg, NewDpPkgFileName, DistFileName, DistPkg.Header.RePackage) ReturnCode = 0 except FatalError, XExcept: ReturnCode = XExcept.args[0] if Logger.GetLevel() <= Logger.DEBUG_9: Logger.Quiet(ST.MSG_PYTHON_ON % (python_version(), platform) + format_exc())
def GetModuleList(DistPkg, Dep, WorkspaceDir, ContentZipFile, ModuleList): # # ModulePathList will keep track of the standalone module path that # we just installed. If a new module's path in that list # (only multiple INF in one directory will be so), we will # install them directly. If not, we will try to create a new directory # for it. # ModulePathList = [] # # Check module exist and install # Module = None NewDict = Sdict() for Guid, Version, Name, Path in DistPkg.ModuleSurfaceArea: ModulePath = Path Module = DistPkg.ModuleSurfaceArea[Guid, Version, Name, Path] Logger.Info(ST.MSG_INSTALL_MODULE % Module.GetName()) if Dep.CheckModuleExists(Guid, Version, Name, Path): Logger.Quiet(ST.WRN_MODULE_EXISTED % Path) # # here check for the multiple inf share the same module path cases: # they should be installed into the same directory # ModuleFullPath = \ os.path.normpath(os.path.join(WorkspaceDir, ModulePath)) if ModuleFullPath not in ModulePathList: NewModulePath = InstallNewModule(WorkspaceDir, ModulePath, ModulePathList) NewModuleFullPath = os.path.normpath( os.path.join(WorkspaceDir, NewModulePath)) ModulePathList.append(NewModuleFullPath) else: NewModulePath = ModulePath InstallModuleContent(ModulePath, NewModulePath, '', Module, ContentZipFile, WorkspaceDir, ModuleList, None, DistPkg.Header.ReadOnly) # # Update module # Module.SetModulePath(Module.GetModulePath().replace( Path, NewModulePath, 1)) NewDict[Guid, Version, Name, Module.GetModulePath()] = Module # # generate all inf for modules # for (Module, Package) in ModuleList: CheckCNameInModuleRedefined(Module, DistPkg) FilePath = ModuleToInf(Module, Package, DistPkg.Header) Md5Sigature = md5.new(__FileHookOpen__(str(FilePath), 'rb').read()) Md5Sum = Md5Sigature.hexdigest() if Package: if (FilePath, Md5Sum) not in Package.FileList: Package.FileList.append((FilePath, Md5Sum)) else: if (FilePath, Md5Sum) not in Module.FileList: Module.FileList.append((FilePath, Md5Sum)) # # append the module unicode files to Package FileList # for (FilePath, Md5Sum) in Module.FileList: if str(FilePath).endswith('.uni') and Package and ( FilePath, Md5Sum) not in Package.FileList: Package.FileList.append((FilePath, Md5Sum)) return NewDict