MultipleWorkspace.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ## @file
  2. # manage multiple workspace file.
  3. #
  4. # This file is required to make Python interpreter treat the directory
  5. # as containing package.
  6. #
  7. # Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  8. # This program and the accompanying materials
  9. # are licensed and made available under the terms and conditions of the BSD License
  10. # which accompanies this distribution. The full text of the license may be found at
  11. # http://opensource.org/licenses/bsd-license.php
  12. #
  13. # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  15. #
  16. import Common.LongFilePathOs as os
  17. from Common.DataType import TAB_WORKSPACE
  18. ## MultipleWorkspace
  19. #
  20. # This class manage multiple workspace behavior
  21. #
  22. # @param class:
  23. #
  24. # @var WORKSPACE: defined the current WORKSPACE
  25. # @var PACKAGES_PATH: defined the other WORKSPACE, if current WORKSPACE is invalid, search valid WORKSPACE from PACKAGES_PATH
  26. #
  27. class MultipleWorkspace(object):
  28. WORKSPACE = ''
  29. PACKAGES_PATH = None
  30. ## convertPackagePath()
  31. #
  32. # Convert path to match workspace.
  33. #
  34. # @param cls The class pointer
  35. # @param Ws The current WORKSPACE
  36. # @param Path Path to be converted to match workspace.
  37. #
  38. @classmethod
  39. def convertPackagePath(cls, Ws, Path):
  40. if str(os.path.normcase (Path)).startswith(Ws):
  41. return os.path.join(Ws, os.path.relpath(Path, Ws))
  42. return Path
  43. ## setWs()
  44. #
  45. # set WORKSPACE and PACKAGES_PATH environment
  46. #
  47. # @param cls The class pointer
  48. # @param Ws initialize WORKSPACE variable
  49. # @param PackagesPath initialize PackagesPath variable
  50. #
  51. @classmethod
  52. def setWs(cls, Ws, PackagesPath=None):
  53. cls.WORKSPACE = Ws
  54. if PackagesPath:
  55. cls.PACKAGES_PATH = [cls.convertPackagePath (Ws, os.path.normpath(Path.strip())) for Path in PackagesPath.split(os.pathsep)]
  56. else:
  57. cls.PACKAGES_PATH = []
  58. ## join()
  59. #
  60. # rewrite os.path.join function
  61. #
  62. # @param cls The class pointer
  63. # @param Ws the current WORKSPACE
  64. # @param *p path of the inf/dec/dsc/fdf/conf file
  65. # @retval Path the absolute path of specified file
  66. #
  67. @classmethod
  68. def join(cls, Ws, *p):
  69. Path = os.path.join(Ws, *p)
  70. if not os.path.exists(Path):
  71. for Pkg in cls.PACKAGES_PATH:
  72. Path = os.path.join(Pkg, *p)
  73. if os.path.exists(Path):
  74. return Path
  75. Path = os.path.join(Ws, *p)
  76. return Path
  77. ## relpath()
  78. #
  79. # rewrite os.path.relpath function
  80. #
  81. # @param cls The class pointer
  82. # @param Path path of the inf/dec/dsc/fdf/conf file
  83. # @param Ws the current WORKSPACE
  84. # @retval Path the relative path of specified file
  85. #
  86. @classmethod
  87. def relpath(cls, Path, Ws):
  88. for Pkg in cls.PACKAGES_PATH:
  89. if Path.lower().startswith(Pkg.lower()):
  90. Path = os.path.relpath(Path, Pkg)
  91. return Path
  92. if Path.lower().startswith(Ws.lower()):
  93. Path = os.path.relpath(Path, Ws)
  94. return Path
  95. ## getWs()
  96. #
  97. # get valid workspace for the path
  98. #
  99. # @param cls The class pointer
  100. # @param Ws the current WORKSPACE
  101. # @param Path path of the inf/dec/dsc/fdf/conf file
  102. # @retval Ws the valid workspace relative to the specified file path
  103. #
  104. @classmethod
  105. def getWs(cls, Ws, Path):
  106. absPath = os.path.join(Ws, Path)
  107. if not os.path.exists(absPath):
  108. for Pkg in cls.PACKAGES_PATH:
  109. absPath = os.path.join(Pkg, Path)
  110. if os.path.exists(absPath):
  111. return Pkg
  112. return Ws
  113. ## handleWsMacro()
  114. #
  115. # handle the $(WORKSPACE) tag, if current workspace is invalid path relative the tool, replace it.
  116. #
  117. # @param cls The class pointer
  118. # @retval PathStr Path string include the $(WORKSPACE)
  119. #
  120. @classmethod
  121. def handleWsMacro(cls, PathStr):
  122. if TAB_WORKSPACE in PathStr:
  123. PathList = PathStr.split()
  124. if PathList:
  125. for i, str in enumerate(PathList):
  126. MacroStartPos = str.find(TAB_WORKSPACE)
  127. if MacroStartPos != -1:
  128. Substr = str[MacroStartPos:]
  129. Path = Substr.replace(TAB_WORKSPACE, cls.WORKSPACE).strip()
  130. if not os.path.exists(Path):
  131. for Pkg in cls.PACKAGES_PATH:
  132. Path = Substr.replace(TAB_WORKSPACE, Pkg).strip()
  133. if os.path.exists(Path):
  134. break
  135. PathList[i] = str[0:MacroStartPos] + Path
  136. PathStr = ' '.join(PathList)
  137. return PathStr
  138. ## getPkgPath()
  139. #
  140. # get all package paths.
  141. #
  142. # @param cls The class pointer
  143. #
  144. @classmethod
  145. def getPkgPath(cls):
  146. return cls.PACKAGES_PATH