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