def DoExecute(self) -> None: """ Copy a file or directory to the destination """ # Try to do a lookup srcPath = self.TryResolveSrcPathString(self.__SourceCommand.From) if srcPath is None: srcPath = IOUtil.Join(self.Info.SrcRootPath, self.__SourceCommand.From) dstPath = self.TryResolveDstPathString(self.__SourceCommand.To) if dstPath is None: dstPath = IOUtil.Join( self.Info.DstRootPath, self.__SourceCommand.To) if len( self.__SourceCommand.To) > 0 else self.Info.DstRootPath fileExist = IOUtil.Exists(dstPath) if not IOUtil.Exists(dstPath) or self.__Overwrite: if fileExist: self.LogPrint( "Copying from '{0}' to '{1}' overwriting the existing file" .format(srcPath, dstPath)) else: self.LogPrint("Copying from '{0}' to '{1}'".format( srcPath, dstPath)) if IOUtil.IsFile(srcPath): self._CreateDirectory(IOUtil.GetDirectoryName(dstPath)) shutil.copy2(srcPath, dstPath) elif IOUtil.IsDirectory(srcPath): self._CreateDirectory(IOUtil.GetDirectoryName(dstPath)) shutil.copytree(srcPath, dstPath) else: raise Exception("Copy source '{0}' not found".format(srcPath)) else: self.LogPrint( "Copying from '{0}' to '{1}' skipped as target exist".format( srcPath, dstPath))
def AddPackage(log: Log, toolConfig: ToolConfig, toolVersionOutputFile: str, writer: Writer, package: Package, outputFolder: str, toolProjectContextsDict: Dict[ProjectId, ToolConfigProjectContext], customPackageFileFilter: Optional[CustomPackageFileFilter], clangFormatConfiguration: ClangFormatConfiguration) -> None: outputFolder = IOUtil.Join(outputFolder, PackagePathUtil.GetPackagePath(package, toolProjectContextsDict)) filteredFiles = GetFilteredFiles(log, customPackageFileFilter, clangFormatConfiguration, package) formatPackageConfig = FormatPackageConfig(log, package, clangFormatConfiguration, filteredFiles) for fileEntry in formatPackageConfig.AllFiles: outputFile = "{0}.dmy".format(IOUtil.Join(outputFolder, fileEntry.SourcePath)) # Locate the closest clang format configuration file so we can add it as a implicit package dependency packageClosestFormatPath = FileFinder.FindClosestFileInRoot(log, toolConfig, fileEntry.ResolvedPath, clangFormatConfiguration.CustomFormatFile) packageClosestFormatFile = IOUtil.Join(packageClosestFormatPath, clangFormatConfiguration.CustomFormatFile) implicitDeps = [packageClosestFormatFile, toolVersionOutputFile] writer.build(outputs=outputFile, rule=PerformClangFormatHelper2.RULE_FORMAT, implicit=implicitDeps, inputs=fileEntry.ResolvedPath) # Write a dummy file so ninja finds a file (this is because clang format overwrites the existing file, so we need a dummy file to track progress) if not IOUtil.Exists(outputFile): parentDir = IOUtil.GetDirectoryName(outputFile) if not IOUtil.Exists(parentDir): IOUtil.SafeMakeDirs(parentDir) IOUtil.WriteFileIfChanged(outputFile, "")
def __GetEnvironmentVariable(self, name: str) -> str: # For cache entries we allow the variable to not be defined, but if it is defned we retrieve is as normal value = IOUtil.TryGetEnvironmentVariable(name) if value is None: raise EnvironmentError( "{0} environment variable not set".format(name)) value = IOUtil.NormalizePath(value) if value is None: raise EnvironmentError( "{0} environment variable not set".format(name)) if not IOUtil.IsAbsolutePath(value): raise EnvironmentError( "{0} environment path '{1}' is not absolute".format( name, value)) if value.endswith("/"): raise EnvironmentError( "{0} environment path '{1}' not allowed to end with '/' or '\'" .format(name, value)) # Create the directory if it didnt exist if not IOUtil.IsDirectory(value) and not IOUtil.Exists(value): self.__Log.LogPrint( "The directory '{0}' did not exist, creating it".format(value)) IOUtil.SafeMakeDirs(value) if not IOUtil.IsDirectory(value): raise EnvironmentError( "The {0} environment variable content '{1}' does not point to a valid directory" .format(name, value)) return value
def __DoValidatePath(self, rErrorRecordList: List[ErrorRecord], installationPath: Optional[str], command: XmlRecipeValidateCommandPath) -> Tuple[bool, Optional[str]]: if self.__BasicConfig.Verbosity >= 4: self.__BasicConfig.LogPrint("ValidatePath '{0}'".format(command.Name)) result, path = self.__TryResolvePath(rErrorRecordList, installationPath, command.Name) if not result or path is None: return False, path if self.__BasicConfig.Verbosity >= 4: self.__BasicConfig.LogPrint("Resolving to '{0}'".format(path)) if command.Method == BuildRecipeValidateMethod.IsDirectory: if not IOUtil.IsDirectory(path): rErrorRecordList.append(ErrorRecord(ErrorClassification.Critical, "Path '{0}' resolved to '{1}' which is not a directory.".format(command.Name, path))) return False, path elif command.Method == BuildRecipeValidateMethod.IsFile: if not IOUtil.IsFile(path): fileHelp = self.__GetFailedFileCheckExtraHelpString(path) rErrorRecordList.append(ErrorRecord(ErrorClassification.Critical, "Path '{0}' resolved to '{1}' which is not a file.{2}".format(command.Name, path, fileHelp))) return False, path elif command.Method == BuildRecipeValidateMethod.Exists: if not IOUtil.Exists(path): rErrorRecordList.append(ErrorRecord(ErrorClassification.Critical, "Path '{0}' resolved to '{1}' which is a path that dont exist.".format(command.Name, path))) return False, path else: raise Exception("Unsupported BuildRecipeValidateMethod '{0}'".format(command.Method)) return True, path
def __DoValidateEnvironmentVariable(self, rErrorRecordList: List[ErrorRecord], command: XmlRecipeValidateCommandEnvironmentVariable) -> Tuple[bool, Optional[str]]: value = os.environ.get(command.Name) if not value: rErrorRecordList.append(ErrorRecord(ErrorClassification.Environment, "Environment variable '{0}' is not defined, please define it as required.".format(command.Name))) return False, value if self.__BasicConfig.Verbosity >= 4: self.__BasicConfig.LogPrint("ValidateEnvironmentVariable: '{0}'='{1}'".format(command.Name, value)) if command.Method == BuildRecipeValidateMethod.IsDirectory: if not IOUtil.IsDirectory(value): rErrorRecordList.append(ErrorRecord(ErrorClassification.Environment, "Environment variable '{0}' contained '{1}' which is not a directory.".format(command.Name, value))) return False, value elif command.Method == BuildRecipeValidateMethod.IsFile: if not IOUtil.IsFile(value): fileHelp = self.__GetFailedFileCheckExtraHelpString(value) rErrorRecordList.append(ErrorRecord(ErrorClassification.Environment, "Environment variable '{0}' contained '{1}' which is not a file.{2}".format(command.Name, value, fileHelp))) return False, value elif command.Method == BuildRecipeValidateMethod.Exists: if not IOUtil.Exists(value): rErrorRecordList.append(ErrorRecord(ErrorClassification.Environment, "Environment variable '{0}' contained '{1}' which is a path that dont exist.".format(command.Name, value))) return False, value else: raise Exception("Unsupported BuildRecipeValidateMethod '{0}'".format(command.Method)) if not command.AllowEndSlash and (value.endswith('/') or value.endswith('\\')): rErrorRecordList.append(ErrorRecord(ErrorClassification.Environment, "Environment variable '{0}' content '{1}' can not end with a slash '/' or backslash '\\'.".format(command.Name, value))) return False, value return True, IOUtil.NormalizePath(value)
def __init__(self, log: Log, basedUponXML: Optional[XmlConfigFileAddRootDirectory], projectId: ProjectId, dynamicSourceRootDir: Union[ Optional[XmlConfigFileAddRootDirectory], Optional['ToolConfigRootDirectory']] = None, dynamicRootName: Optional[str] = None, dynamicPath: Optional[str] = None) -> None: super().__init__() dirMustExist = True self.ProjectId = projectId if basedUponXML is not None: self.BasedOn = basedUponXML # type: Union[XmlConfigFileAddRootDirectory, 'ToolConfigRootDirectory'] self.Name = basedUponXML.Name # type: str self.DynamicName = basedUponXML.Name # type: str dirMustExist = not basedUponXML.Create else: if dynamicSourceRootDir is None: raise Exception("dynamicSourceRootDir can not be none") if dynamicRootName is None: raise Exception("dynamicRootName can not be none") if dynamicPath is None: raise Exception("dynamicPath can not be none") self.BasedOn = dynamicSourceRootDir self.Name = dynamicRootName self.DynamicName = dynamicPath variableProcessor = VariableProcessor(log) # NOTE: workaround Union of tuples not being iterable bug in mypy https://github.com/python/mypy/issues/1575 tupleResult = variableProcessor.TryExtractLeadingEnvironmentVariableNameAndPath( self.DynamicName, dynamicSourceRootDir != None) env = tupleResult[0] remainingPath = tupleResult[1] if env is None: raise Exception( "Root dirs are expected to contain environment variables '{0}'" .format(self.DynamicName)) remainingPath = remainingPath if remainingPath is not None else "" resolvedPath = IOUtil.GetEnvironmentVariableForDirectory( env, dirMustExist) if not IOUtil.Exists(resolvedPath): IOUtil.SafeMakeDirs(resolvedPath) if not IOUtil.IsDirectory(resolvedPath): raise EnvironmentError( "The {0} environment variable content '{1}' does not point to a valid directory" .format(env, resolvedPath)) resolvedPath = resolvedPath + remainingPath self.BashName = '${0}{1}'.format(env, remainingPath) # type: str self.DosName = '%{0}%{1}'.format(env, remainingPath) # type: str self.ResolvedPath = IOUtil.ToUnixStylePath(resolvedPath) # type: str self.ResolvedPathEx = "{0}/".format(self.ResolvedPath) if len( self.ResolvedPath) > 0 else "" # type: str self.__EnvironmentVariableName = env # type: str
def IsCompleted(self) -> bool: return IOUtil.Exists(self.Info.DstRootPath)