2015-10-08 11:27:14 +02:00
|
|
|
## @file
|
|
|
|
# manage multiple workspace file.
|
|
|
|
#
|
|
|
|
# This file is required to make Python interpreter treat the directory
|
|
|
|
# as containing package.
|
|
|
|
#
|
2018-07-05 11:40:04 +02:00
|
|
|
# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:03:11 +02:00
|
|
|
# SPDX-License-Identifier: BSD-2-Clause-Patent
|
2015-10-08 11:27:14 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
import Common.LongFilePathOs as os
|
|
|
|
from Common.DataType import TAB_WORKSPACE
|
|
|
|
|
|
|
|
## MultipleWorkspace
|
|
|
|
#
|
|
|
|
# This class manage multiple workspace behavior
|
2018-07-05 11:40:04 +02:00
|
|
|
#
|
2015-10-08 11:27:14 +02:00
|
|
|
# @param class:
|
|
|
|
#
|
|
|
|
# @var WORKSPACE: defined the current WORKSPACE
|
2019-02-06 08:44:39 +01:00
|
|
|
# @var PACKAGES_PATH: defined the other WORKSPACE, if current WORKSPACE is invalid, search valid WORKSPACE from PACKAGES_PATH
|
2018-07-05 11:40:04 +02:00
|
|
|
#
|
2015-10-08 11:27:14 +02:00
|
|
|
class MultipleWorkspace(object):
|
|
|
|
WORKSPACE = ''
|
|
|
|
PACKAGES_PATH = None
|
2018-07-05 11:40:04 +02:00
|
|
|
|
2015-10-08 11:27:14 +02:00
|
|
|
## convertPackagePath()
|
|
|
|
#
|
|
|
|
# Convert path to match workspace.
|
|
|
|
#
|
|
|
|
# @param cls The class pointer
|
|
|
|
# @param Ws The current WORKSPACE
|
|
|
|
# @param Path Path to be converted to match workspace.
|
|
|
|
#
|
|
|
|
@classmethod
|
|
|
|
def convertPackagePath(cls, Ws, Path):
|
|
|
|
if str(os.path.normcase (Path)).startswith(Ws):
|
2015-10-14 11:43:43 +02:00
|
|
|
return os.path.join(Ws, os.path.relpath(Path, Ws))
|
2015-10-08 11:27:14 +02:00
|
|
|
return Path
|
|
|
|
|
|
|
|
## setWs()
|
|
|
|
#
|
|
|
|
# set WORKSPACE and PACKAGES_PATH environment
|
|
|
|
#
|
|
|
|
# @param cls The class pointer
|
|
|
|
# @param Ws initialize WORKSPACE variable
|
|
|
|
# @param PackagesPath initialize PackagesPath variable
|
|
|
|
#
|
|
|
|
@classmethod
|
|
|
|
def setWs(cls, Ws, PackagesPath=None):
|
|
|
|
cls.WORKSPACE = Ws
|
|
|
|
if PackagesPath:
|
|
|
|
cls.PACKAGES_PATH = [cls.convertPackagePath (Ws, os.path.normpath(Path.strip())) for Path in PackagesPath.split(os.pathsep)]
|
|
|
|
else:
|
|
|
|
cls.PACKAGES_PATH = []
|
2018-07-05 11:40:04 +02:00
|
|
|
|
2015-10-08 11:27:14 +02:00
|
|
|
## join()
|
|
|
|
#
|
|
|
|
# rewrite os.path.join function
|
|
|
|
#
|
|
|
|
# @param cls The class pointer
|
|
|
|
# @param Ws the current WORKSPACE
|
|
|
|
# @param *p path of the inf/dec/dsc/fdf/conf file
|
|
|
|
# @retval Path the absolute path of specified file
|
|
|
|
#
|
|
|
|
@classmethod
|
|
|
|
def join(cls, Ws, *p):
|
|
|
|
Path = os.path.join(Ws, *p)
|
|
|
|
if not os.path.exists(Path):
|
|
|
|
for Pkg in cls.PACKAGES_PATH:
|
|
|
|
Path = os.path.join(Pkg, *p)
|
|
|
|
if os.path.exists(Path):
|
|
|
|
return Path
|
|
|
|
Path = os.path.join(Ws, *p)
|
|
|
|
return Path
|
2018-07-05 11:40:04 +02:00
|
|
|
|
2015-10-08 11:27:14 +02:00
|
|
|
## relpath()
|
|
|
|
#
|
|
|
|
# rewrite os.path.relpath function
|
|
|
|
#
|
|
|
|
# @param cls The class pointer
|
|
|
|
# @param Path path of the inf/dec/dsc/fdf/conf file
|
|
|
|
# @param Ws the current WORKSPACE
|
|
|
|
# @retval Path the relative path of specified file
|
|
|
|
#
|
|
|
|
@classmethod
|
|
|
|
def relpath(cls, Path, Ws):
|
|
|
|
for Pkg in cls.PACKAGES_PATH:
|
|
|
|
if Path.lower().startswith(Pkg.lower()):
|
|
|
|
Path = os.path.relpath(Path, Pkg)
|
|
|
|
return Path
|
|
|
|
if Path.lower().startswith(Ws.lower()):
|
|
|
|
Path = os.path.relpath(Path, Ws)
|
|
|
|
return Path
|
2018-07-05 11:40:04 +02:00
|
|
|
|
2015-10-08 11:27:14 +02:00
|
|
|
## getWs()
|
|
|
|
#
|
|
|
|
# get valid workspace for the path
|
|
|
|
#
|
|
|
|
# @param cls The class pointer
|
|
|
|
# @param Ws the current WORKSPACE
|
|
|
|
# @param Path path of the inf/dec/dsc/fdf/conf file
|
|
|
|
# @retval Ws the valid workspace relative to the specified file path
|
|
|
|
#
|
|
|
|
@classmethod
|
|
|
|
def getWs(cls, Ws, Path):
|
|
|
|
absPath = os.path.join(Ws, Path)
|
|
|
|
if not os.path.exists(absPath):
|
|
|
|
for Pkg in cls.PACKAGES_PATH:
|
|
|
|
absPath = os.path.join(Pkg, Path)
|
|
|
|
if os.path.exists(absPath):
|
|
|
|
return Pkg
|
|
|
|
return Ws
|
2018-07-05 11:40:04 +02:00
|
|
|
|
2015-10-08 11:27:14 +02:00
|
|
|
## handleWsMacro()
|
|
|
|
#
|
|
|
|
# handle the $(WORKSPACE) tag, if current workspace is invalid path relative the tool, replace it.
|
|
|
|
#
|
|
|
|
# @param cls The class pointer
|
|
|
|
# @retval PathStr Path string include the $(WORKSPACE)
|
|
|
|
#
|
|
|
|
@classmethod
|
|
|
|
def handleWsMacro(cls, PathStr):
|
|
|
|
if TAB_WORKSPACE in PathStr:
|
2016-04-13 10:27:05 +02:00
|
|
|
PathList = PathStr.split()
|
|
|
|
if PathList:
|
|
|
|
for i, str in enumerate(PathList):
|
2016-04-14 17:03:45 +02:00
|
|
|
MacroStartPos = str.find(TAB_WORKSPACE)
|
|
|
|
if MacroStartPos != -1:
|
|
|
|
Substr = str[MacroStartPos:]
|
|
|
|
Path = Substr.replace(TAB_WORKSPACE, cls.WORKSPACE).strip()
|
|
|
|
if not os.path.exists(Path):
|
|
|
|
for Pkg in cls.PACKAGES_PATH:
|
|
|
|
Path = Substr.replace(TAB_WORKSPACE, Pkg).strip()
|
|
|
|
if os.path.exists(Path):
|
|
|
|
break
|
|
|
|
PathList[i] = str[0:MacroStartPos] + Path
|
2016-04-13 10:27:05 +02:00
|
|
|
PathStr = ' '.join(PathList)
|
2015-10-08 11:27:14 +02:00
|
|
|
return PathStr
|
2018-07-05 11:40:04 +02:00
|
|
|
|
2015-10-08 11:27:14 +02:00
|
|
|
## getPkgPath()
|
|
|
|
#
|
2019-02-06 08:44:39 +01:00
|
|
|
# get all package paths.
|
2015-10-08 11:27:14 +02:00
|
|
|
#
|
|
|
|
# @param cls The class pointer
|
|
|
|
#
|
|
|
|
@classmethod
|
|
|
|
def getPkgPath(cls):
|
|
|
|
return cls.PACKAGES_PATH
|
2018-07-05 11:40:04 +02:00
|
|
|
|