## Regular expression used to find out place holders in string template\r
gPlaceholderPattern = re.compile("\$\{([^$()\s]+)\}", re.MULTILINE | re.UNICODE)\r
\r
+## regular expressions for map file processing\r
+startPatternGeneral = re.compile("^Start[' ']+Length[' ']+Name[' ']+Class")\r
+addressPatternGeneral = re.compile("^Address[' ']+Publics by Value[' ']+Rva\+Base")\r
+valuePatternGcc = re.compile('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$')\r
+pcdPatternGcc = re.compile('^([\da-fA-Fx]+) +([\da-fA-Fx]+)')\r
+secReGeneral = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
+\r
## Dictionary used to store file time stamp for quick re-access\r
gFileTimeStampCache = {} # {file path : file time stamp}\r
\r
## Dictionary used to store dependencies of files\r
gDependencyDatabase = {} # arch : {file path : [dependent files list]}\r
\r
+#\r
+# If a module is built more than once with different PCDs or library classes\r
+# a temporary INF file with same content is created, the temporary file is removed\r
+# when build exits.\r
+#\r
+_TempInfs = []\r
+\r
def GetVariableOffset(mapfilepath, efifilepath, varnames):\r
- """ Parse map file to get variable offset in current EFI file \r
+ """ Parse map file to get variable offset in current EFI file\r
@param mapfilepath Map file absolution path\r
@param efifilepath: EFI binary file full path\r
@param varnames iteratable container whose elements are variable names to be searched\r
- \r
+\r
@return List whos elements are tuple with variable name and raw offset\r
"""\r
lines = []\r
f.close()\r
except:\r
return None\r
- \r
+\r
if len(lines) == 0: return None\r
firstline = lines[0].strip()\r
if (firstline.startswith("Archive member included ") and\r
if status == 1 and len(line) != 0:\r
for varname in varnames:\r
if varname in line:\r
+ # cannot pregenerate this RegEx since it uses varname from varnames.\r
m = re.match('^([\da-fA-FxX]+)([\s\S]*)([_]*%s)$' % varname, line)\r
if m is not None:\r
ret.append((varname, m.group(1)))\r
\r
# status handler\r
if status == 3:\r
- m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line)\r
+ m = valuePatternGcc.match(line)\r
if m is not None:\r
sections.append(m.groups(0))\r
for varname in varnames:\r
else:\r
Str = line[len(".data.%s" % varname):]\r
if Str:\r
- m = re.match('^([\da-fA-Fx]+) +([\da-fA-Fx]+)', Str.strip())\r
+ m = pcdPatternGcc.match(Str.strip())\r
if m is not None:\r
- varoffset.append((varname, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
+ varoffset.append((varname, int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0]))\r
\r
if not varoffset:\r
return []\r
status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
secs = [] # key = section name\r
varoffset = []\r
- secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE)\r
\r
for line in lines:\r
line = line.strip()\r
- if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line):\r
+ if startPatternGeneral.match(line):\r
status = 1\r
continue\r
- if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line):\r
+ if addressPatternGeneral.match(line):\r
status = 2\r
continue\r
- if re.match("^entry point at", line):\r
+ if line.startswith("entry point at"):\r
status = 3\r
- continue \r
+ continue\r
if status == 1 and len(line) != 0:\r
- m = secRe.match(line)\r
+ m = secReGeneral.match(line)\r
assert m is not None, "Fail to parse the section in map file , line is %s" % line\r
sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)\r
secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])\r
sec_no = int(sec_no, 16)\r
sym_offset = int(sym_offset, 16)\r
vir_addr = int(vir_addr, 16)\r
+ # cannot pregenerate this RegEx since it uses varname from varnames.\r
m2 = re.match('^[_]*(%s)' % varname, sym_name)\r
if m2 is not None:\r
# fond a binary pcd entry in map file\r
#\r
# A temporary INF is copied to database path which must have write permission\r
# The temporary will be removed at the end of build\r
- # In case of name conflict, the file name is \r
+ # In case of name conflict, the file name is\r
# FILE_GUIDBaseName (0D1B936F-68F3-4589-AFCC-FB8B7AEBC836module.inf)\r
#\r
TempFullPath = os.path.join(DbDir,\r
#\r
# To build same module more than once, the module path with FILE_GUID overridden has\r
# the file name FILE_GUIDmodule.inf, but the relative path (self.MetaFile.File) is the real path\r
- # in DSC which is used as relative path by C files and other files in INF. \r
+ # in DSC which is used as relative path by C files and other files in INF.\r
# A trick was used: all module paths are PathClass instances, after the initialization\r
# of PathClass, the PathClass.Path is overridden by the temporary INF path.\r
#\r
# If file exists, compare contents\r
#\r
if os.path.exists(TempFullPath):\r
- with open(str(Path), 'rb') as f1: Src = f1.read()\r
- with open(TempFullPath, 'rb') as f2: Dst = f2.read()\r
- if Src == Dst:\r
- return RtPath\r
- GlobalData.gTempInfs.append(TempFullPath)\r
+ with open(str(Path), 'rb') as f1, open(TempFullPath, 'rb') as f2:\r
+ if f1.read() == f2.read():\r
+ return RtPath\r
+ _TempInfs.append(TempFullPath)\r
shutil.copy2(str(Path), TempFullPath)\r
return RtPath\r
\r
-## Remove temporary created INFs whose paths were saved in gTempInfs\r
+## Remove temporary created INFs whose paths were saved in _TempInfs\r
#\r
def ClearDuplicatedInf():\r
- for File in GlobalData.gTempInfs:\r
+ while _TempInfs:\r
+ File = _TempInfs.pop()\r
if os.path.exists(File):\r
os.remove(File)\r
\r
Fd = open(File, "wb")\r
Fd.write(Content)\r
Fd.close()\r
- except IOError, X:\r
+ except IOError as X:\r
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
\r
return True\r
try:\r
Fd = open(File, 'rb')\r
Data = cPickle.load(Fd)\r
- except Exception, e:\r
+ except Exception as e:\r
EdkLogger.verbose("Failed to load [%s]\n\t%s" % (File, str(e)))\r
Data = None\r
finally:\r
GuidKeys = P.Guids.keys()\r
if Inffile and P._PrivateGuids:\r
if not Inffile.startswith(P.MetaFile.Dir):\r
- GuidKeys = (dict.fromkeys(x for x in P.Guids if x not in P._PrivateGuids)).keys()\r
+ GuidKeys = [x for x in P.Guids if x not in P._PrivateGuids]\r
if CName in GuidKeys:\r
return P.Guids[CName]\r
return None\r
ProtocolKeys = P.Protocols.keys()\r
if Inffile and P._PrivateProtocols:\r
if not Inffile.startswith(P.MetaFile.Dir):\r
- ProtocolKeys = (dict.fromkeys(x for x in P.Protocols if x not in P._PrivateProtocols)).keys()\r
+ ProtocolKeys = [x for x in P.Protocols if x not in P._PrivateProtocols]\r
if CName in ProtocolKeys:\r
return P.Protocols[CName]\r
return None\r
PpiKeys = P.Ppis.keys()\r
if Inffile and P._PrivatePpis:\r
if not Inffile.startswith(P.MetaFile.Dir):\r
- PpiKeys = (dict.fromkeys(x for x in P.Ppis if x not in P._PrivatePpis)).keys()\r
+ PpiKeys = [x for x in P.Ppis if x not in P._PrivatePpis]\r
if CName in PpiKeys:\r
return P.Ppis[CName]\r
return None\r
def Append(self, AppendString, Dictionary=None):\r
if Dictionary:\r
SectionList = self._Parse(AppendString)\r
- self.String += "".join([S.Instantiate(Dictionary) for S in SectionList])\r
+ self.String += "".join(S.Instantiate(Dictionary) for S in SectionList)\r
else:\r
self.String += AppendString\r
\r
# @retval str The string replaced with placeholder values\r
#\r
def Replace(self, Dictionary=None):\r
- return "".join([S.Instantiate(Dictionary) for S in self._TemplateSectionList])\r
+ return "".join(S.Instantiate(Dictionary) for S in self._TemplateSectionList)\r
\r
## Progress indicator class\r
#\r
\r
def IsFieldValueAnArray (Value):\r
Value = Value.strip()\r
- if Value.startswith('GUID') and Value.endswith(')'):\r
+ if Value.startswith(TAB_GUID) and Value.endswith(')'):\r
return True\r
if Value.startswith('L"') and Value.endswith('"') and len(list(Value[2:-1])) > 1:\r
return True\r
try:\r
p = subprocess.Popen(Cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
out, err = p.communicate()\r
- except Exception, X:\r
+ except Exception as X:\r
raise BadExpression("DevicePath: %s" % (str(X)) )\r
finally:\r
subprocess._cleanup()\r
return '{' + out + '}', Size\r
\r
def ParseFieldValue (Value):\r
- if type(Value) == type(0):\r
+ if isinstance(Value, type(0)):\r
return Value, (Value.bit_length() + 7) / 8\r
- if type(Value) <> type(''):\r
+ if not isinstance(Value, type('')):\r
raise BadExpression('Type %s is %s' %(Value, type(Value)))\r
Value = Value.strip()\r
- if Value.startswith('UINT8') and Value.endswith(')'):\r
+ if Value.startswith(TAB_UINT8) and Value.endswith(')'):\r
Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
if Size > 1:\r
raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
return Value, 1\r
- if Value.startswith('UINT16') and Value.endswith(')'):\r
+ if Value.startswith(TAB_UINT16) and Value.endswith(')'):\r
Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
if Size > 2:\r
raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
return Value, 2\r
- if Value.startswith('UINT32') and Value.endswith(')'):\r
+ if Value.startswith(TAB_UINT32) and Value.endswith(')'):\r
Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
if Size > 4:\r
raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
return Value, 4\r
- if Value.startswith('UINT64') and Value.endswith(')'):\r
+ if Value.startswith(TAB_UINT64) and Value.endswith(')'):\r
Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
if Size > 8:\r
raise BadExpression('Value (%s) Size larger than %d' % (Value, Size))\r
return Value, 8\r
- if Value.startswith('GUID') and Value.endswith(')'):\r
+ if Value.startswith(TAB_GUID) and Value.endswith(')'):\r
Value = Value.split('(', 1)[1][:-1].strip()\r
if Value[0] == '{' and Value[-1] == '}':\r
TmpValue = GuidStructureStringToGuidString(Value)\r
Value = Value[1:-1]\r
try:\r
Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"\r
- except ValueError, Message:\r
- raise BadExpression('%s' % Message)\r
+ except ValueError as Message:\r
+ raise BadExpression(Message)\r
Value, Size = ParseFieldValue(Value)\r
return Value, 16\r
if Value.startswith('L"') and Value.endswith('"'):\r
# Value, Size = ParseFieldValue(Value)\r
if Size:\r
try:\r
- int(Size,16) if Size.upper().startswith("0X") else int(Size)\r
+ int(Size, 16) if Size.upper().startswith("0X") else int(Size)\r
except:\r
IsValid = False\r
Size = -1\r
\r
if Size:\r
try:\r
- int(Size,16) if Size.upper().startswith("0X") else int(Size)\r
+ int(Size, 16) if Size.upper().startswith("0X") else int(Size)\r
except:\r
IsValid = False\r
Size = -1\r
elif PcdType in (MODEL_PCD_DYNAMIC_VPD, MODEL_PCD_DYNAMIC_EX_VPD):\r
VpdOffset = FieldList[0]\r
Value = Size = ''\r
- if not DataType == 'VOID*':\r
+ if not DataType == TAB_VOID:\r
if len(FieldList) > 1:\r
Value = FieldList[1]\r
else:\r
IsValid = (len(FieldList) <= 3)\r
if Size:\r
try:\r
- int(Size,16) if Size.upper().startswith("0X") else int(Size)\r
+ int(Size, 16) if Size.upper().startswith("0X") else int(Size)\r
except:\r
IsValid = False\r
Size = -1\r
# Used to avoid split issue while the value string contain "|" character\r
#\r
# @param[in] Setting: A String contain value/datum type/token number information;\r
-# \r
-# @retval ValueList: A List contain value, datum type and toke number. \r
+#\r
+# @retval ValueList: A List contain value, datum type and toke number.\r
#\r
def AnalyzePcdData(Setting):\r
ValueList = ['', '', '']\r
\r
ValueRe = re.compile(r'^\s*L?\".*\|.*\"')\r
PtrValue = ValueRe.findall(Setting)\r
- \r
+\r
ValueUpdateFlag = False\r
- \r
+\r
if len(PtrValue) >= 1:\r
Setting = re.sub(ValueRe, '', Setting)\r
ValueUpdateFlag = True\r
\r
TokenList = Setting.split(TAB_VALUE_SPLIT)\r
ValueList[0:len(TokenList)] = TokenList\r
- \r
+\r
if ValueUpdateFlag:\r
ValueList[0] = PtrValue[0]\r
- \r
- return ValueList \r
- \r
+\r
+ return ValueList\r
+\r
## check format of PCD value against its the datum type\r
#\r
# For PCD value setting\r
#\r
def CheckPcdDatum(Type, Value):\r
- if Type == "VOID*":\r
+ if Type == TAB_VOID:\r
ValueRe = re.compile(r'\s*L?\".*\"\s*$')\r
if not (((Value.startswith('L"') or Value.startswith('"')) and Value.endswith('"'))\r
or (Value.startswith('{') and Value.endswith('}')) or (Value.startswith("L'") or Value.startswith("'") and Value.endswith("'"))\r
Printset.add(TAB_PRINTCHAR_BS)\r
Printset.add(TAB_PRINTCHAR_NUL)\r
if not set(Value).issubset(Printset):\r
- PrintList = list(Printset)\r
- PrintList.sort()\r
+ PrintList = sorted(Printset)\r
return False, "Invalid PCD string value of type [%s]; must be printable chars %s." % (Type, PrintList)\r
elif Type == 'BOOLEAN':\r
if Value not in ['TRUE', 'True', 'true', '0x1', '0x01', '1', 'FALSE', 'False', 'false', '0x0', '0x00', '0']:\r
\r
Value = eval(Value) # translate escape character\r
NewValue = '{'\r
- for Index in range(0,len(Value)):\r
+ for Index in range(0, len(Value)):\r
if Unicode:\r
NewValue = NewValue + str(ord(Value[Index]) % 0x10000) + ','\r
else:\r
# @retval True The two PathClass are the same\r
#\r
def __eq__(self, Other):\r
- if type(Other) == type(self):\r
+ if isinstance(Other, type(self)):\r
return self.Path == Other.Path\r
else:\r
return self.Path == str(Other)\r
# @retval -1 The first PathClass is less than the second PathClass\r
# @retval 1 The first PathClass is Bigger than the second PathClass\r
def __cmp__(self, Other):\r
- if type(Other) == type(self):\r
+ if isinstance(Other, type(self)):\r
OtherKey = Other.Path\r
else:\r
OtherKey = str(Other)\r
- \r
+\r
SelfKey = self.Path\r
if SelfKey == OtherKey:\r
return 0\r
def _ByteListToStr(self, ByteList):\r
String = ''\r
for index in range(len(ByteList)):\r
- if ByteList[index] == 0: \r
+ if ByteList[index] == 0:\r
break\r
String += chr(ByteList[index])\r
return String\r
return Value\r
\r
class DefaultStore():\r
- def __init__(self,DefaultStores ):\r
+ def __init__(self, DefaultStores ):\r
\r
self.DefaultStores = DefaultStores\r
- def DefaultStoreID(self,DefaultStoreName):\r
- for key,value in self.DefaultStores.items():\r
+ def DefaultStoreID(self, DefaultStoreName):\r
+ for key, value in self.DefaultStores.items():\r
if value == DefaultStoreName:\r
return key\r
return None\r
def GetDefaultDefault(self):\r
if not self.DefaultStores or "0" in self.DefaultStores:\r
- return "0",TAB_DEFAULT_STORES_DEFAULT\r
+ return "0", TAB_DEFAULT_STORES_DEFAULT\r
else:\r
- minvalue = min([int(value_str) for value_str in self.DefaultStores.keys()])\r
+ minvalue = min(int(value_str) for value_str in self.DefaultStores)\r
return (str(minvalue), self.DefaultStores[str(minvalue)])\r
- def GetMin(self,DefaultSIdList):\r
+ def GetMin(self, DefaultSIdList):\r
if not DefaultSIdList:\r
- return "STANDARD"\r
+ return TAB_DEFAULT_STORES_DEFAULT\r
storeidset = {storeid for storeid, storename in self.DefaultStores.values() if storename in DefaultSIdList}\r
if not storeidset:\r
return ""\r
minid = min(storeidset )\r
- for sid,name in self.DefaultStores.values():\r
+ for sid, name in self.DefaultStores.values():\r
if sid == minid:\r
return name\r
class SkuClass():\r
- \r
+\r
DEFAULT = 0\r
SINGLE = 1\r
MULTIPLE =2\r
- \r
+\r
def __init__(self,SkuIdentifier='', SkuIds=None):\r
if SkuIds is None:\r
SkuIds = {}\r
\r
for SkuName in SkuIds:\r
SkuId = SkuIds[SkuName][0]\r
- skuid_num = int(SkuId,16) if SkuId.upper().startswith("0X") else int(SkuId)\r
+ skuid_num = int(SkuId, 16) if SkuId.upper().startswith("0X") else int(SkuId)\r
if skuid_num > 0xFFFFFFFFFFFFFFFF:\r
EdkLogger.error("build", PARAMETER_INVALID,\r
ExtraData = "SKU-ID [%s] value %s exceeds the max value of UINT64"\r
% (SkuName, SkuId))\r
- \r
+\r
self.AvailableSkuIds = sdict()\r
self.SkuIdSet = []\r
self.SkuIdNumberSet = []\r
self.SkuIdSet = SkuIds.keys()\r
self.SkuIdNumberSet = [num[0].strip() + 'U' for num in SkuIds.values()]\r
else:\r
- r = SkuIdentifier.split('|') \r
+ r = SkuIdentifier.split('|')\r
self.SkuIdSet=[(r[k].strip()).upper() for k in range(len(r))]\r
k = None\r
- try: \r
+ try:\r
self.SkuIdNumberSet = [SkuIds[k][0].strip() + 'U' for k in self.SkuIdSet]\r
except Exception:\r
EdkLogger.error("build", PARAMETER_INVALID,\r
self.__SkuInherit = {}\r
for item in self.SkuData.values():\r
self.__SkuInherit[item[1]]=item[2] if item[2] else "DEFAULT"\r
- return self.__SkuInherit.get(skuname,"DEFAULT")\r
+ return self.__SkuInherit.get(skuname, "DEFAULT")\r
\r
- def GetSkuChain(self,sku):\r
+ def GetSkuChain(self, sku):\r
if sku == "DEFAULT":\r
return ["DEFAULT"]\r
skulist = [sku]\r
nextsku = sku\r
- while 1:\r
+ while True:\r
nextsku = self.GetNextSkuId(nextsku)\r
skulist.append(nextsku)\r
if nextsku == "DEFAULT":\r
skuorderset = []\r
for skuname in self.SkuIdSet:\r
skuorderset.append(self.GetSkuChain(skuname))\r
- \r
+\r
skuorder = []\r
- for index in range(max([len(item) for item in skuorderset])):\r
+ for index in range(max(len(item) for item in skuorderset)):\r
for subset in skuorderset:\r
if index > len(subset)-1:\r
continue\r
\r
return skuorder\r
\r
- def __SkuUsageType(self): \r
- \r
+ def __SkuUsageType(self):\r
+\r
if self.__SkuIdentifier.upper() == "ALL":\r
return SkuClass.MULTIPLE\r
\r
return ArrayStr\r
def __GetAvailableSkuIds(self):\r
return self.AvailableSkuIds\r
- \r
+\r
def __GetSystemSkuID(self):\r
if self.__SkuUsageType() == SkuClass.SINGLE:\r
if len(self.SkuIdSet) == 1:\r
# Pack a registry format GUID\r
#\r
def PackRegistryFormatGuid(Guid):\r
- Guid = Guid.split('-')\r
- return pack('=LHHBBBBBBBB',\r
- int(Guid[0], 16),\r
- int(Guid[1], 16),\r
- int(Guid[2], 16),\r
- int(Guid[3][-4:-2], 16),\r
- int(Guid[3][-2:], 16),\r
- int(Guid[4][-12:-10], 16),\r
- int(Guid[4][-10:-8], 16),\r
- int(Guid[4][-8:-6], 16),\r
- int(Guid[4][-6:-4], 16),\r
- int(Guid[4][-4:-2], 16),\r
- int(Guid[4][-2:], 16)\r
- )\r
+ return PackGUID(Guid.split('-'))\r
\r
## Get the integer value from string like "14U" or integer like 2\r
#\r
else:\r
return int(String)\r
\r
+#\r
+# Pack a GUID (registry format) list into a buffer and return it\r
+#\r
+def PackGUID(Guid):\r
+ return pack(PACK_PATTERN_GUID,\r
+ int(Guid[0], 16),\r
+ int(Guid[1], 16),\r
+ int(Guid[2], 16),\r
+ int(Guid[3][-4:-2], 16),\r
+ int(Guid[3][-2:], 16),\r
+ int(Guid[4][-12:-10], 16),\r
+ int(Guid[4][-10:-8], 16),\r
+ int(Guid[4][-8:-6], 16),\r
+ int(Guid[4][-6:-4], 16),\r
+ int(Guid[4][-4:-2], 16),\r
+ int(Guid[4][-2:], 16)\r
+ )\r
+\r
+#\r
+# Pack a GUID (byte) list into a buffer and return it\r
+#\r
+def PackByteFormatGUID(Guid):\r
+ return pack(PACK_PATTERN_GUID,\r
+ Guid[0],\r
+ Guid[1],\r
+ Guid[2],\r
+ Guid[3],\r
+ Guid[4],\r
+ Guid[5],\r
+ Guid[6],\r
+ Guid[7],\r
+ Guid[8],\r
+ Guid[9],\r
+ Guid[10],\r
+ )\r
+\r
##\r
#\r
# This acts like the main() function for the script, unless it is 'import'ed into another\r