]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/MultipleWorkspace.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / Common / MultipleWorkspace.py
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 # SPDX-License-Identifier: BSD-2-Clause-Patent
9 #
10
11 import Common.LongFilePathOs as os
12 from Common.DataType import TAB_WORKSPACE
13
14 ## MultipleWorkspace
15 #
16 # This class manage multiple workspace behavior
17 #
18 # @param class:
19 #
20 # @var WORKSPACE: defined the current WORKSPACE
21 # @var PACKAGES_PATH: defined the other WORKSPACE, if current WORKSPACE is invalid, search valid WORKSPACE from PACKAGES_PATH
22 #
23 class MultipleWorkspace(object):
24 WORKSPACE = ''
25 PACKAGES_PATH = None
26
27 ## convertPackagePath()
28 #
29 # Convert path to match workspace.
30 #
31 # @param cls The class pointer
32 # @param Ws The current WORKSPACE
33 # @param Path Path to be converted to match workspace.
34 #
35 @classmethod
36 def convertPackagePath(cls, Ws, Path):
37 if str(os.path.normcase (Path)).startswith(Ws):
38 return os.path.join(Ws, os.path.relpath(Path, Ws))
39 return Path
40
41 ## setWs()
42 #
43 # set WORKSPACE and PACKAGES_PATH environment
44 #
45 # @param cls The class pointer
46 # @param Ws initialize WORKSPACE variable
47 # @param PackagesPath initialize PackagesPath variable
48 #
49 @classmethod
50 def setWs(cls, Ws, PackagesPath=None):
51 cls.WORKSPACE = Ws
52 if PackagesPath:
53 cls.PACKAGES_PATH = [cls.convertPackagePath (Ws, os.path.normpath(Path.strip())) for Path in PackagesPath.split(os.pathsep)]
54 else:
55 cls.PACKAGES_PATH = []
56
57 ## join()
58 #
59 # rewrite os.path.join function
60 #
61 # @param cls The class pointer
62 # @param Ws the current WORKSPACE
63 # @param *p path of the inf/dec/dsc/fdf/conf file
64 # @retval Path the absolute path of specified file
65 #
66 @classmethod
67 def join(cls, Ws, *p):
68 Path = os.path.join(Ws, *p)
69 if not os.path.exists(Path):
70 for Pkg in cls.PACKAGES_PATH:
71 Path = os.path.join(Pkg, *p)
72 if os.path.exists(Path):
73 return Path
74 Path = os.path.join(Ws, *p)
75 return Path
76
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
96 ## getWs()
97 #
98 # get valid workspace for the path
99 #
100 # @param cls The class pointer
101 # @param Ws the current WORKSPACE
102 # @param Path path of the inf/dec/dsc/fdf/conf file
103 # @retval Ws the valid workspace relative to the specified file path
104 #
105 @classmethod
106 def getWs(cls, Ws, Path):
107 absPath = os.path.join(Ws, Path)
108 if not os.path.exists(absPath):
109 for Pkg in cls.PACKAGES_PATH:
110 absPath = os.path.join(Pkg, Path)
111 if os.path.exists(absPath):
112 return Pkg
113 return Ws
114
115 ## handleWsMacro()
116 #
117 # handle the $(WORKSPACE) tag, if current workspace is invalid path relative the tool, replace it.
118 #
119 # @param cls The class pointer
120 # @retval PathStr Path string include the $(WORKSPACE)
121 #
122 @classmethod
123 def handleWsMacro(cls, PathStr):
124 if TAB_WORKSPACE in PathStr:
125 PathList = PathStr.split()
126 if PathList:
127 for i, str in enumerate(PathList):
128 MacroStartPos = str.find(TAB_WORKSPACE)
129 if MacroStartPos != -1:
130 Substr = str[MacroStartPos:]
131 Path = Substr.replace(TAB_WORKSPACE, cls.WORKSPACE).strip()
132 if not os.path.exists(Path):
133 for Pkg in cls.PACKAGES_PATH:
134 Path = Substr.replace(TAB_WORKSPACE, Pkg).strip()
135 if os.path.exists(Path):
136 break
137 PathList[i] = str[0:MacroStartPos] + Path
138 PathStr = ' '.join(PathList)
139 return PathStr
140
141 ## getPkgPath()
142 #
143 # get all package paths.
144 #
145 # @param cls The class pointer
146 #
147 @classmethod
148 def getPkgPath(cls):
149 return cls.PACKAGES_PATH
150