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