]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/MultipleWorkspace.py
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 - 2016, 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
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.
17 import Common
.LongFilePathOs
as os
18 from Common
.DataType
import TAB_WORKSPACE
22 # This class manage multiple workspace behavior
26 # @var WORKSPACE: defined the current WORKSPACE
27 # @var PACKAGES_PATH: defined the other WORKSAPCE, if current WORKSPACE is invalid, search valid WORKSPACE from PACKAGES_PATH
29 class MultipleWorkspace(object):
33 ## convertPackagePath()
35 # Convert path to match workspace.
37 # @param cls The class pointer
38 # @param Ws The current WORKSPACE
39 # @param Path Path to be converted to match workspace.
42 def convertPackagePath(cls
, Ws
, Path
):
43 if str(os
.path
.normcase (Path
)).startswith(Ws
):
44 return os
.path
.join(Ws
, os
.path
.relpath(Path
, Ws
))
49 # set WORKSPACE and PACKAGES_PATH environment
51 # @param cls The class pointer
52 # @param Ws initialize WORKSPACE variable
53 # @param PackagesPath initialize PackagesPath variable
56 def setWs(cls
, Ws
, PackagesPath
=None):
59 cls
.PACKAGES_PATH
= [cls
.convertPackagePath (Ws
, os
.path
.normpath(Path
.strip())) for Path
in PackagesPath
.split(os
.pathsep
)]
61 cls
.PACKAGES_PATH
= []
65 # rewrite os.path.join function
67 # @param cls The class pointer
68 # @param Ws the current WORKSPACE
69 # @param *p path of the inf/dec/dsc/fdf/conf file
70 # @retval Path the absolute path of specified file
73 def join(cls
, Ws
, *p
):
74 Path
= os
.path
.join(Ws
, *p
)
75 if not os
.path
.exists(Path
):
76 for Pkg
in cls
.PACKAGES_PATH
:
77 Path
= os
.path
.join(Pkg
, *p
)
78 if os
.path
.exists(Path
):
80 Path
= os
.path
.join(Ws
, *p
)
85 # rewrite os.path.relpath function
87 # @param cls The class pointer
88 # @param Path path of the inf/dec/dsc/fdf/conf file
89 # @param Ws the current WORKSPACE
90 # @retval Path the relative path of specified file
93 def relpath(cls
, Path
, Ws
):
94 for Pkg
in cls
.PACKAGES_PATH
:
95 if Path
.lower().startswith(Pkg
.lower()):
96 Path
= os
.path
.relpath(Path
, Pkg
)
98 if Path
.lower().startswith(Ws
.lower()):
99 Path
= os
.path
.relpath(Path
, Ws
)
104 # get valid workspace for the path
106 # @param cls The class pointer
107 # @param Ws the current WORKSPACE
108 # @param Path path of the inf/dec/dsc/fdf/conf file
109 # @retval Ws the valid workspace relative to the specified file path
112 def getWs(cls
, Ws
, Path
):
113 absPath
= os
.path
.join(Ws
, Path
)
114 if not os
.path
.exists(absPath
):
115 for Pkg
in cls
.PACKAGES_PATH
:
116 absPath
= os
.path
.join(Pkg
, Path
)
117 if os
.path
.exists(absPath
):
123 # handle the $(WORKSPACE) tag, if current workspace is invalid path relative the tool, replace it.
125 # @param cls The class pointer
126 # @retval PathStr Path string include the $(WORKSPACE)
129 def handleWsMacro(cls
, PathStr
):
130 if TAB_WORKSPACE
in PathStr
:
131 PathList
= PathStr
.split()
133 for i
, str in enumerate(PathList
):
134 MacroStartPos
= str.find(TAB_WORKSPACE
)
135 if MacroStartPos
!= -1:
136 Substr
= str[MacroStartPos
:]
137 Path
= Substr
.replace(TAB_WORKSPACE
, cls
.WORKSPACE
).strip()
138 if not os
.path
.exists(Path
):
139 for Pkg
in cls
.PACKAGES_PATH
:
140 Path
= Substr
.replace(TAB_WORKSPACE
, Pkg
).strip()
141 if os
.path
.exists(Path
):
143 PathList
[i
] = str[0:MacroStartPos
] + Path
144 PathStr
= ' '.join(PathList
)
149 # get all package pathes.
151 # @param cls The class pointer
155 return cls
.PACKAGES_PATH