+## @file\r
+# manage multiple workspace file.\r
+#\r
+# This file is required to make Python interpreter treat the directory\r
+# as containing package.\r
+#\r
+# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+# This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+import Common.LongFilePathOs as os\r
+from Common.DataType import TAB_WORKSPACE\r
+\r
+## MultipleWorkspace\r
+#\r
+# This class manage multiple workspace behavior\r
+# \r
+# @param class:\r
+#\r
+# @var WORKSPACE: defined the current WORKSPACE\r
+# @var PACKAGES_PATH: defined the other WORKSAPCE, if current WORKSPACE is invalid, search valid WORKSPACE from PACKAGES_PATH\r
+# \r
+class MultipleWorkspace(object):\r
+ WORKSPACE = ''\r
+ PACKAGES_PATH = None\r
+ \r
+ ## convertPackagePath()\r
+ #\r
+ # Convert path to match workspace.\r
+ #\r
+ # @param cls The class pointer\r
+ # @param Ws The current WORKSPACE\r
+ # @param Path Path to be converted to match workspace.\r
+ #\r
+ @classmethod\r
+ def convertPackagePath(cls, Ws, Path):\r
+ if str(os.path.normcase (Path)).startswith(Ws):\r
+ return os.path.join(Ws, Path[len(Ws) + 1:])\r
+ return Path\r
+\r
+ ## setWs()\r
+ #\r
+ # set WORKSPACE and PACKAGES_PATH environment\r
+ #\r
+ # @param cls The class pointer\r
+ # @param Ws initialize WORKSPACE variable\r
+ # @param PackagesPath initialize PackagesPath variable\r
+ #\r
+ @classmethod\r
+ def setWs(cls, Ws, PackagesPath=None):\r
+ cls.WORKSPACE = Ws\r
+ if PackagesPath:\r
+ cls.PACKAGES_PATH = [cls.convertPackagePath (Ws, os.path.normpath(Path.strip())) for Path in PackagesPath.split(os.pathsep)]\r
+ else:\r
+ cls.PACKAGES_PATH = []\r
+ \r
+ ## join()\r
+ #\r
+ # rewrite os.path.join function\r
+ #\r
+ # @param cls The class pointer\r
+ # @param Ws the current WORKSPACE\r
+ # @param *p path of the inf/dec/dsc/fdf/conf file\r
+ # @retval Path the absolute path of specified file\r
+ #\r
+ @classmethod\r
+ def join(cls, Ws, *p):\r
+ Path = os.path.join(Ws, *p)\r
+ if not os.path.exists(Path):\r
+ for Pkg in cls.PACKAGES_PATH:\r
+ Path = os.path.join(Pkg, *p)\r
+ if os.path.exists(Path):\r
+ return Path\r
+ Path = os.path.join(Ws, *p)\r
+ return Path\r
+ \r
+ ## relpath()\r
+ #\r
+ # rewrite os.path.relpath function\r
+ #\r
+ # @param cls The class pointer\r
+ # @param Path path of the inf/dec/dsc/fdf/conf file\r
+ # @param Ws the current WORKSPACE\r
+ # @retval Path the relative path of specified file\r
+ #\r
+ @classmethod\r
+ def relpath(cls, Path, Ws):\r
+ for Pkg in cls.PACKAGES_PATH:\r
+ if Path.lower().startswith(Pkg.lower()):\r
+ Path = os.path.relpath(Path, Pkg)\r
+ return Path\r
+ if Path.lower().startswith(Ws.lower()):\r
+ Path = os.path.relpath(Path, Ws)\r
+ return Path\r
+ \r
+ ## getWs()\r
+ #\r
+ # get valid workspace for the path\r
+ #\r
+ # @param cls The class pointer\r
+ # @param Ws the current WORKSPACE\r
+ # @param Path path of the inf/dec/dsc/fdf/conf file\r
+ # @retval Ws the valid workspace relative to the specified file path\r
+ #\r
+ @classmethod\r
+ def getWs(cls, Ws, Path):\r
+ absPath = os.path.join(Ws, Path)\r
+ if not os.path.exists(absPath):\r
+ for Pkg in cls.PACKAGES_PATH:\r
+ absPath = os.path.join(Pkg, Path)\r
+ if os.path.exists(absPath):\r
+ return Pkg\r
+ return Ws\r
+ \r
+ ## handleWsMacro()\r
+ #\r
+ # handle the $(WORKSPACE) tag, if current workspace is invalid path relative the tool, replace it.\r
+ #\r
+ # @param cls The class pointer\r
+ # @retval PathStr Path string include the $(WORKSPACE)\r
+ #\r
+ @classmethod\r
+ def handleWsMacro(cls, PathStr):\r
+ if TAB_WORKSPACE in PathStr:\r
+ Path = PathStr.replace(TAB_WORKSPACE, cls.WORKSPACE).strip()\r
+ if not os.path.exists(Path):\r
+ for Pkg in cls.PACKAGES_PATH:\r
+ Path = PathStr.replace(TAB_WORKSPACE, Pkg).strip()\r
+ if os.path.exists(Path):\r
+ return Path\r
+ return PathStr\r
+ \r
+ ## getPkgPath()\r
+ #\r
+ # get all package pathes.\r
+ #\r
+ # @param cls The class pointer\r
+ #\r
+ @classmethod\r
+ def getPkgPath(cls):\r
+ return cls.PACKAGES_PATH\r
+
\ No newline at end of file