]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/MultipleWorkspace.py
ad5d48588b483c9797bc8ee20fe9689d3567e9c1
2 # manage multiple workspace file.
4 # This file is required to make Python interpreter treat the directory
5 # as containing package.
7 # Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
8 # SPDX-License-Identifier: BSD-2-Clause-Patent
11 import Common
.LongFilePathOs
as os
12 from Common
.DataType
import TAB_WORKSPACE
16 # This class manage multiple workspace behavior
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
23 class MultipleWorkspace(object):
27 ## convertPackagePath()
29 # Convert path to match workspace.
31 # @param cls The class pointer
32 # @param Ws The current WORKSPACE
33 # @param Path Path to be converted to match workspace.
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
))
43 # set WORKSPACE and PACKAGES_PATH environment
45 # @param cls The class pointer
46 # @param Ws initialize WORKSPACE variable
47 # @param PackagesPath initialize PackagesPath variable
50 def setWs(cls
, Ws
, PackagesPath
=None):
53 cls
.PACKAGES_PATH
= [cls
.convertPackagePath (Ws
, os
.path
.normpath(Path
.strip())) for Path
in PackagesPath
.split(os
.pathsep
)]
55 cls
.PACKAGES_PATH
= []
59 # rewrite os.path.join function
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
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
):
74 Path
= os
.path
.join(Ws
, *p
)
79 # rewrite os.path.relpath function
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
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
)
92 if Path
.lower().startswith(Ws
.lower()):
93 Path
= os
.path
.relpath(Path
, Ws
)
98 # get valid workspace for the path
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
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
):
117 # handle the $(WORKSPACE) tag, if current workspace is invalid path relative the tool, replace it.
119 # @param cls The class pointer
120 # @retval PathStr Path string include the $(WORKSPACE)
123 def handleWsMacro(cls
, PathStr
):
124 if TAB_WORKSPACE
in PathStr
:
125 PathList
= PathStr
.split()
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
):
137 PathList
[i
] = str[0:MacroStartPos
] + Path
138 PathStr
= ' '.join(PathList
)
143 # get all package paths.
145 # @param cls The class pointer
149 return cls
.PACKAGES_PATH