public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks
@ 2018-06-09  6:15 Kinney, Michael D
  2018-06-09  6:15 ` [Patch 1/5] BaseTools/BinToPcd: Fix typo in error messages Kinney, Michael D
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Kinney, Michael D @ 2018-06-09  6:15 UTC (permalink / raw)
  To: edk2-devel; +Cc: Yanyan Sun, Yonghong Zhu, Liming Gao, Michael D Kinney

https://bugzilla.tianocore.org/show_bug.cgi?id=962
https://bugzilla.tianocore.org/show_bug.cgi?id=963
https://bugzilla.tianocore.org/show_bug.cgi?id=974
https://bugzilla.tianocore.org/show_bug.cgi?id=965

* Change "PcdToBin" to "BinToPcd"
* Update error message for --type HII.  If either --variable-guid
  or --variable-name is missing, then print an error message that
  states that both --variable-guid and --variable-name are required.
* --offset must be 8-byte aligned.
* Compatible with both Python 2.x and Python 3.x.
* Return error code when error message printed.
* Follow PEP-8 indent of 4 spaces.  https://www.python.org/dev/peps/pep-0008/

Cc: Yanyan Sun <yanyan.sun@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1

Kinney, Michael D (5):
  BaseTools/BinToPcd: Fix typo in error messages
  BaseTools/BinToPcd: Clarify error message for --type HII
  BaseTools/BinToPcd: --offset must be 8-byte aligned
  BaseTools/BinToPcd: Update for Python 3 compatibility
  BaseTools/BinToPcd: Follow PEP-8 indent of 4 spaces

 BaseTools/Scripts/BinToPcd.py | 350 +++++++++++++++++++++---------------------
 1 file changed, 179 insertions(+), 171 deletions(-)

-- 
2.14.2.windows.3



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Patch 1/5] BaseTools/BinToPcd: Fix typo in error messages
  2018-06-09  6:15 [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Kinney, Michael D
@ 2018-06-09  6:15 ` Kinney, Michael D
  2018-06-09  6:15 ` [Patch 2/5] BaseTools/BinToPcd: Clarify error message for --type HII Kinney, Michael D
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Kinney, Michael D @ 2018-06-09  6:15 UTC (permalink / raw)
  To: edk2-devel; +Cc: Yanyan Sun, Yonghong Zhu, Liming Gao, Michael D Kinney

https://bugzilla.tianocore.org/show_bug.cgi?id=962

Change "PcdToBin" to "BinToPcd"

Cc: Yanyan Sun <yanyan.sun@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
---
 BaseTools/Scripts/BinToPcd.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
index f2485a27fa..014aad9800 100644
--- a/BaseTools/Scripts/BinToPcd.py
+++ b/BaseTools/Scripts/BinToPcd.py
@@ -135,7 +135,7 @@ if __name__ == '__main__':
     #
     Pcd = PcdValue
     if args.Verbose:
-      print 'PcdToBin: Convert binary file to PCD Value'
+      print 'BinToPcd: Convert binary file to PCD Value'
   elif args.PcdType is None:
     #
     # If --type is neither VPD nor HII, then use PCD statement syntax that is
@@ -155,7 +155,7 @@ if __name__ == '__main__':
       Pcd = '  %s|%s|VOID*|%d' % (args.PcdName, PcdValue, args.MaxSize)
 
     if args.Verbose:
-      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections:'
+      print 'BinToPcd: Convert binary file to PCD statement compatible with PCD sections:'
       print '    [PcdsFixedAtBuild]'
       print '    [PcdsPatchableInModule]'
       print '    [PcdsDynamicDefault]'
@@ -182,7 +182,7 @@ if __name__ == '__main__':
       #
       Pcd = '  %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, PcdValue)
     if args.Verbose:
-      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'
+      print 'BinToPcd: Convert binary file to PCD statement compatible with PCD sections'
       print '    [PcdsDynamicVpd]'
       print '    [PcdsDynamicExVpd]'
   elif args.PcdType == 'HII':
@@ -199,7 +199,7 @@ if __name__ == '__main__':
       args.Offset = 0
     Pcd = '  %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args.VariableGuid, args.Offset, PcdValue)
     if args.Verbose:
-      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'
+      print 'BinToPcd: Convert binary file to PCD statement compatible with PCD sections'
       print '    [PcdsDynamicHii]'
       print '    [PcdsDynamicExHii]'
 
-- 
2.14.2.windows.3



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Patch 2/5] BaseTools/BinToPcd: Clarify error message for --type HII
  2018-06-09  6:15 [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Kinney, Michael D
  2018-06-09  6:15 ` [Patch 1/5] BaseTools/BinToPcd: Fix typo in error messages Kinney, Michael D
@ 2018-06-09  6:15 ` Kinney, Michael D
  2018-06-09  6:15 ` [Patch 3/5] BaseTools/BinToPcd: --offset must be 8-byte aligned Kinney, Michael D
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Kinney, Michael D @ 2018-06-09  6:15 UTC (permalink / raw)
  To: edk2-devel; +Cc: Yanyan Sun, Yonghong Zhu, Liming Gao, Michael D Kinney

https://bugzilla.tianocore.org/show_bug.cgi?id=963

Update error message for --type HII.  If either --variable-guid
or --variable-name is missing, then print an error message that
states that both --variable-guid and --variable-name are required.

Cc: Yanyan Sun <yanyan.sun@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
---
 BaseTools/Scripts/BinToPcd.py | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
index 014aad9800..7e3380190e 100644
--- a/BaseTools/Scripts/BinToPcd.py
+++ b/BaseTools/Scripts/BinToPcd.py
@@ -186,11 +186,8 @@ if __name__ == '__main__':
       print '    [PcdsDynamicVpd]'
       print '    [PcdsDynamicExVpd]'
   elif args.PcdType == 'HII':
-    if args.VariableGuid is None:
-      print 'BinToPcd: error: argument --variable-guid is required for --type HII.'
-      sys.exit()
-    if args.VariableName is None:
-      print 'BinToPcd: error: argument --variable-name is required for --type HII.'
+    if args.VariableGuid is None or args.VariableName is None:
+      print 'BinToPcd: error: arguments --variable-guid and --variable-name are required for --type HII.'
       sys.exit()
     if args.Offset is None:
       #
-- 
2.14.2.windows.3



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Patch 3/5] BaseTools/BinToPcd: --offset must be 8-byte aligned
  2018-06-09  6:15 [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Kinney, Michael D
  2018-06-09  6:15 ` [Patch 1/5] BaseTools/BinToPcd: Fix typo in error messages Kinney, Michael D
  2018-06-09  6:15 ` [Patch 2/5] BaseTools/BinToPcd: Clarify error message for --type HII Kinney, Michael D
@ 2018-06-09  6:15 ` Kinney, Michael D
  2018-06-09  6:15 ` [Patch 4/5] BaseTools/BinToPcd: Update for Python 3 compatibility Kinney, Michael D
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Kinney, Michael D @ 2018-06-09  6:15 UTC (permalink / raw)
  To: edk2-devel; +Cc: Yanyan Sun, Yonghong Zhu, Liming Gao, Michael D Kinney

https://bugzilla.tianocore.org/show_bug.cgi?id=974
https://bugzilla.tianocore.org/show_bug.cgi?id=965

Update help to state that --offset must be 8-byte aligned.
Verify that --offset is 8-byte aligned and print an error
message if it is not 8-byte aligned.

Cc: Yanyan Sun <yanyan.sun@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
---
 BaseTools/Scripts/BinToPcd.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
index 7e3380190e..54cb844d68 100644
--- a/BaseTools/Scripts/BinToPcd.py
+++ b/BaseTools/Scripts/BinToPcd.py
@@ -89,7 +89,7 @@ if __name__ == '__main__':
   parser.add_argument("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,
                       help = "Maximum size of the PCD.  Ignored with --type HII.")
   parser.add_argument("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,
-                      help = "VPD offset if --type is VPD.  UEFI Variable offset if --type is HII.")
+                      help = "VPD offset if --type is VPD.  UEFI Variable offset if --type is HII.  Must be 8-byte aligned.")
   parser.add_argument("-n", "--variable-name", dest = 'VariableName',
                       help = "UEFI variable name.  Only used with --type HII.")
   parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',
@@ -178,6 +178,12 @@ if __name__ == '__main__':
       Pcd = '  %s|*|%d|%s' % (args.PcdName, args.MaxSize, PcdValue)
     else:
       #
+      # --offset value must be 8-byte aligned
+      #
+      if (args.Offset % 8) != 0:
+        print 'BinToPcd: error: argument --offset must be 8-byte aligned.'
+        sys.exit()
+      #
       # Use the --offset value provided.
       #
       Pcd = '  %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, PcdValue)
@@ -194,6 +200,12 @@ if __name__ == '__main__':
       # Use UEFI Variable offset of 0 if --offset is not provided
       #
       args.Offset = 0
+    #
+    # --offset value must be 8-byte aligned
+    #
+    if (args.Offset % 8) != 0:
+      print 'BinToPcd: error: argument --offset must be 8-byte aligned.'
+      sys.exit()
     Pcd = '  %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args.VariableGuid, args.Offset, PcdValue)
     if args.Verbose:
       print 'BinToPcd: Convert binary file to PCD statement compatible with PCD sections'
-- 
2.14.2.windows.3



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Patch 4/5] BaseTools/BinToPcd: Update for Python 3 compatibility
  2018-06-09  6:15 [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Kinney, Michael D
                   ` (2 preceding siblings ...)
  2018-06-09  6:15 ` [Patch 3/5] BaseTools/BinToPcd: --offset must be 8-byte aligned Kinney, Michael D
@ 2018-06-09  6:15 ` Kinney, Michael D
  2018-06-09  6:15 ` [Patch 5/5] BaseTools/BinToPcd: Follow PEP-8 indent of 4 spaces Kinney, Michael D
  2018-06-12  0:40 ` [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Zhu, Yonghong
  5 siblings, 0 replies; 7+ messages in thread
From: Kinney, Michael D @ 2018-06-09  6:15 UTC (permalink / raw)
  To: edk2-devel; +Cc: Yanyan Sun, Yonghong Zhu, Liming Gao, Michael D Kinney

Update to be compatible with both Python 2.x and Python 3.x.
Also return error code 1 when an error is detected to support
use of this tool in scripts.

Cc: Yanyan Sun <yanyan.sun@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
---
 BaseTools/Scripts/BinToPcd.py | 81 +++++++++++++++++++++----------------------
 1 file changed, 40 insertions(+), 41 deletions(-)

diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
index 54cb844d68..52c231615f 100644
--- a/BaseTools/Scripts/BinToPcd.py
+++ b/BaseTools/Scripts/BinToPcd.py
@@ -24,7 +24,6 @@ import xdrlib
 # Globals for help information
 #
 __prog__        = 'BinToPcd'
-__version__     = '%s Version %s' % (__prog__, '0.91 ')
 __copyright__   = 'Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.'
 __description__ = 'Convert one or more binary files to a VOID* PCD value or DSC file VOID* PCD statement.\n'
 
@@ -33,22 +32,22 @@ if __name__ == '__main__':
     try:
       Value = int (Argument, 0)
     except:
-      Message = '%s is not a valid integer value.' % (Argument)
+      Message = '{Argument} is not a valid integer value.'.format (Argument = Argument)
       raise argparse.ArgumentTypeError(Message)
     if Value < 0:
-      Message = '%s is a negative value.' % (Argument)
+      Message = '{Argument} is a negative value.'.format (Argument = Argument)
       raise argparse.ArgumentTypeError(Message)
     return Value
 
   def ValidatePcdName (Argument):
-    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:
-      Message = '%s is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>' % (Argument)
+    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:
+      Message = '{Argument} is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>'.format (Argument = Argument)
       raise argparse.ArgumentTypeError(Message)
     return Argument
 
   def ValidateGuidName (Argument):
-    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:
-      Message = '%s is not a valid GUID C name' % (Argument)
+    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:
+      Message = '{Argument} is not a valid GUID C name'.format (Argument = Argument)
       raise argparse.ArgumentTypeError(Message)
     return Argument
 
@@ -61,21 +60,21 @@ if __name__ == '__main__':
       XdrEncoder = xdrlib.Packer()
       for Item in Buffer:
         XdrEncoder.pack_bytes(Item)
-      Buffer = XdrEncoder.get_buffer()
+      Buffer = bytearray(XdrEncoder.get_buffer())
     else:
       #
       # If Xdr flag is not set, then concatenate all the data
       #
-      Buffer = ''.join(Buffer)
+      Buffer = b''.join(Buffer)
     #
     # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PCD length in bytes
     #
-    return '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in Buffer])), len (Buffer)
+    return '{' + (', '.join(['0x{Byte:02X}'.format(Byte = Item) for Item in Buffer])) + '}', len (Buffer)
 
   #
   # Create command line argument parser object
   #
-  parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
+  parser = argparse.ArgumentParser(prog = __prog__,
                                    description = __description__ + __copyright__,
                                    conflict_handler = 'resolve')
   parser.add_argument("-i", "--input", dest = 'InputFile', type = argparse.FileType('rb'), action='append', required = True,
@@ -117,8 +116,8 @@ if __name__ == '__main__':
       Buffer.append(File.read())
       File.close()
     except:
-      print 'BinToPcd: error: can not read binary input file', File
-      sys.exit()
+      print ('BinToPcd: error: can not read binary input file {File}'.format (File = File))
+      sys.exit(1)
 
   #
   # Convert PCD to an encoded string of hex values and determine the size of
@@ -135,7 +134,7 @@ if __name__ == '__main__':
     #
     Pcd = PcdValue
     if args.Verbose:
-      print 'BinToPcd: Convert binary file to PCD Value'
+      print ('BinToPcd: Convert binary file to PCD Value')
   elif args.PcdType is None:
     #
     # If --type is neither VPD nor HII, then use PCD statement syntax that is
@@ -147,19 +146,19 @@ if __name__ == '__main__':
       # If --max-size is not provided, then do not generate the syntax that
       # includes the maximum size.
       #
-      Pcd = '  %s|%s' % (args.PcdName, PcdValue)
+      Pcd = '  {Name}|{Value}'.format (Name = args.PcdName, Value = PcdValue)
     elif args.MaxSize < PcdSize:
-      print 'BinToPcd: error: argument --max-size is smaller than input file.'
-      sys.exit()
+      print ('BinToPcd: error: argument --max-size is smaller than input file.')
+      sys.exit(1)
     else:
-      Pcd = '  %s|%s|VOID*|%d' % (args.PcdName, PcdValue, args.MaxSize)
+      Pcd = '  {Name}|{Value}|VOID*|{Size}'.format (Name = args.PcdName, Value = PcdValue, Size = args.MaxSize)
 
     if args.Verbose:
-      print 'BinToPcd: Convert binary file to PCD statement compatible with PCD sections:'
-      print '    [PcdsFixedAtBuild]'
-      print '    [PcdsPatchableInModule]'
-      print '    [PcdsDynamicDefault]'
-      print '    [PcdsDynamicExDefault]'
+      print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections:')
+      print ('    [PcdsFixedAtBuild]')
+      print ('    [PcdsPatchableInModule]')
+      print ('    [PcdsDynamicDefault]')
+      print ('    [PcdsDynamicExDefault]')
   elif args.PcdType == 'VPD':
     if args.MaxSize is None:
       #
@@ -168,33 +167,33 @@ if __name__ == '__main__':
       #
       args.MaxSize = PcdSize
     if args.MaxSize < PcdSize:
-      print 'BinToPcd: error: argument --max-size is smaller than input file.'
-      sys.exit()
+      print ('BinToPcd: error: argument --max-size is smaller than input file.')
+      sys.exit(1)
     if args.Offset is None:
       #
       # if --offset is not provided, then set offset field to '*' so build
       # tools will compute offset of PCD in VPD region.
       #
-      Pcd = '  %s|*|%d|%s' % (args.PcdName, args.MaxSize, PcdValue)
+      Pcd = '  {Name}|*|{Size}|{Value}'.format (Name = args.PcdName, Size = args.MaxSize, Value = PcdValue)
     else:
       #
       # --offset value must be 8-byte aligned
       #
       if (args.Offset % 8) != 0:
-        print 'BinToPcd: error: argument --offset must be 8-byte aligned.'
-        sys.exit()
+        print ('BinToPcd: error: argument --offset must be 8-byte aligned.')
+        sys.exit(1)
       #
       # Use the --offset value provided.
       #
-      Pcd = '  %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, PcdValue)
+      Pcd = '  {Name}|{Offset}|{Size}|{Value}'.format (Name = args.PcdName, Offset = args.Offset, Size = args.MaxSize, Value = PcdValue)
     if args.Verbose:
-      print 'BinToPcd: Convert binary file to PCD statement compatible with PCD sections'
-      print '    [PcdsDynamicVpd]'
-      print '    [PcdsDynamicExVpd]'
+      print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')
+      print ('    [PcdsDynamicVpd]')
+      print ('    [PcdsDynamicExVpd]')
   elif args.PcdType == 'HII':
     if args.VariableGuid is None or args.VariableName is None:
-      print 'BinToPcd: error: arguments --variable-guid and --variable-name are required for --type HII.'
-      sys.exit()
+      print ('BinToPcd: error: arguments --variable-guid and --variable-name are required for --type HII.')
+      sys.exit(1)
     if args.Offset is None:
       #
       # Use UEFI Variable offset of 0 if --offset is not provided
@@ -204,13 +203,13 @@ if __name__ == '__main__':
     # --offset value must be 8-byte aligned
     #
     if (args.Offset % 8) != 0:
-      print 'BinToPcd: error: argument --offset must be 8-byte aligned.'
-      sys.exit()
-    Pcd = '  %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args.VariableGuid, args.Offset, PcdValue)
+      print ('BinToPcd: error: argument --offset must be 8-byte aligned.')
+      sys.exit(1)
+    Pcd = '  {Name}|L"{VarName}"|{VarGuid}|{Offset}|{Value}'.format (Name = args.PcdName, VarName = args.VariableName, VarGuid = args.VariableGuid, Offset = args.Offset, Value = PcdValue)
     if args.Verbose:
-      print 'BinToPcd: Convert binary file to PCD statement compatible with PCD sections'
-      print '    [PcdsDynamicHii]'
-      print '    [PcdsDynamicExHii]'
+      print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')
+      print ('    [PcdsDynamicHii]')
+      print ('    [PcdsDynamicExHii]')
 
   #
   # Write PCD value or PCD statement to the output file
@@ -223,4 +222,4 @@ if __name__ == '__main__':
     # If output file is not specified or it can not be written, then write the
     # PCD value or PCD statement to the console
     #
-    print Pcd
+    print (Pcd)
-- 
2.14.2.windows.3



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Patch 5/5] BaseTools/BinToPcd: Follow PEP-8 indent of 4 spaces
  2018-06-09  6:15 [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Kinney, Michael D
                   ` (3 preceding siblings ...)
  2018-06-09  6:15 ` [Patch 4/5] BaseTools/BinToPcd: Update for Python 3 compatibility Kinney, Michael D
@ 2018-06-09  6:15 ` Kinney, Michael D
  2018-06-12  0:40 ` [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Zhu, Yonghong
  5 siblings, 0 replies; 7+ messages in thread
From: Kinney, Michael D @ 2018-06-09  6:15 UTC (permalink / raw)
  To: edk2-devel; +Cc: Yanyan Sun, Yonghong Zhu, Liming Gao, Michael D Kinney

https://www.python.org/dev/peps/pep-0008/

Cc: Yanyan Sun <yanyan.sun@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
---
 BaseTools/Scripts/BinToPcd.py | 356 +++++++++++++++++++++---------------------
 1 file changed, 178 insertions(+), 178 deletions(-)

diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
index 52c231615f..b907d3e5e0 100644
--- a/BaseTools/Scripts/BinToPcd.py
+++ b/BaseTools/Scripts/BinToPcd.py
@@ -28,198 +28,198 @@ __copyright__   = 'Copyright (c) 2016 - 2018, Intel Corporation. All rights rese
 __description__ = 'Convert one or more binary files to a VOID* PCD value or DSC file VOID* PCD statement.\n'
 
 if __name__ == '__main__':
-  def ValidateUnsignedInteger (Argument):
-    try:
-      Value = int (Argument, 0)
-    except:
-      Message = '{Argument} is not a valid integer value.'.format (Argument = Argument)
-      raise argparse.ArgumentTypeError(Message)
-    if Value < 0:
-      Message = '{Argument} is a negative value.'.format (Argument = Argument)
-      raise argparse.ArgumentTypeError(Message)
-    return Value
+    def ValidateUnsignedInteger (Argument):
+        try:
+            Value = int (Argument, 0)
+        except:
+            Message = '{Argument} is not a valid integer value.'.format (Argument = Argument)
+            raise argparse.ArgumentTypeError (Message)
+        if Value < 0:
+            Message = '{Argument} is a negative value.'.format (Argument = Argument)
+            raise argparse.ArgumentTypeError (Message)
+        return Value
 
-  def ValidatePcdName (Argument):
-    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:
-      Message = '{Argument} is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>'.format (Argument = Argument)
-      raise argparse.ArgumentTypeError(Message)
-    return Argument
+    def ValidatePcdName (Argument):
+        if re.split ('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:
+            Message = '{Argument} is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>'.format (Argument = Argument)
+            raise argparse.ArgumentTypeError (Message)
+        return Argument
 
-  def ValidateGuidName (Argument):
-    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:
-      Message = '{Argument} is not a valid GUID C name'.format (Argument = Argument)
-      raise argparse.ArgumentTypeError(Message)
-    return Argument
+    def ValidateGuidName (Argument):
+        if re.split ('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:
+            Message = '{Argument} is not a valid GUID C name'.format (Argument = Argument)
+            raise argparse.ArgumentTypeError (Message)
+        return Argument
+
+    def ByteArray (Buffer, Xdr = False):
+        if Xdr:
+            #
+            # If Xdr flag is set then encode data using the Variable-Length Opaque
+            # Data format of RFC 4506 External Data Representation Standard (XDR).
+            #
+            XdrEncoder = xdrlib.Packer ()
+            for Item in Buffer:
+                XdrEncoder.pack_bytes (Item)
+            Buffer = bytearray (XdrEncoder.get_buffer ())
+        else:
+            #
+            # If Xdr flag is not set, then concatenate all the data
+            #
+            Buffer = b''.join (Buffer)
+        #
+        # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PCD length in bytes
+        #
+        return '{' + (', '.join (['0x{Byte:02X}'.format (Byte = Item) for Item in Buffer])) + '}', len (Buffer)
 
-  def ByteArray (Buffer, Xdr = False):
-    if Xdr:
-      #
-      # If Xdr flag is set then encode data using the Variable-Length Opaque
-      # Data format of RFC 4506 External Data Representation Standard (XDR).
-      #
-      XdrEncoder = xdrlib.Packer()
-      for Item in Buffer:
-        XdrEncoder.pack_bytes(Item)
-      Buffer = bytearray(XdrEncoder.get_buffer())
-    else:
-      #
-      # If Xdr flag is not set, then concatenate all the data
-      #
-      Buffer = b''.join(Buffer)
     #
-    # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PCD length in bytes
+    # Create command line argument parser object
     #
-    return '{' + (', '.join(['0x{Byte:02X}'.format(Byte = Item) for Item in Buffer])) + '}', len (Buffer)
-
-  #
-  # Create command line argument parser object
-  #
-  parser = argparse.ArgumentParser(prog = __prog__,
-                                   description = __description__ + __copyright__,
-                                   conflict_handler = 'resolve')
-  parser.add_argument("-i", "--input", dest = 'InputFile', type = argparse.FileType('rb'), action='append', required = True,
-                      help = "Input binary filename.  Multiple input files are combined into a single PCD.")
-  parser.add_argument("-o", "--output", dest = 'OutputFile', type = argparse.FileType('wb'),
-                      help = "Output filename for PCD value or PCD statement")
-  parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
-                      help = "Name of the PCD in the form <PcdTokenSpaceGuidCName>.<PcdCName>")
-  parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices = ['VPD','HII'],
-                      help = "PCD statement type (HII or VPD).  Default is standard.")
-  parser.add_argument("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,
-                      help = "Maximum size of the PCD.  Ignored with --type HII.")
-  parser.add_argument("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,
-                      help = "VPD offset if --type is VPD.  UEFI Variable offset if --type is HII.  Must be 8-byte aligned.")
-  parser.add_argument("-n", "--variable-name", dest = 'VariableName',
-                      help = "UEFI variable name.  Only used with --type HII.")
-  parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',
-                      help = "UEFI variable GUID C name.  Only used with --type HII.")
-  parser.add_argument("-x", "--xdr", dest = 'Xdr', action = "store_true",
-                      help = "Encode PCD using the Variable-Length Opaque Data format of RFC 4506 External Data Representation Standard (XDR)")
-  parser.add_argument("-v", "--verbose", dest = 'Verbose', action = "store_true",
-                      help = "Increase output messages")
-  parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
-                      help = "Reduce output messages")
-  parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range(0,10), default = 0,
-                      help = "Set debug level")
-
-  #
-  # Parse command line arguments
-  #
-  args = parser.parse_args()
-
-  #
-  # Read all binary input files
-  #
-  Buffer = []
-  for File in args.InputFile:
-    try:
-      Buffer.append(File.read())
-      File.close()
-    except:
-      print ('BinToPcd: error: can not read binary input file {File}'.format (File = File))
-      sys.exit(1)
+    parser = argparse.ArgumentParser (prog = __prog__,
+                                      description = __description__ + __copyright__,
+                                      conflict_handler = 'resolve')
+    parser.add_argument ("-i", "--input", dest = 'InputFile', type = argparse.FileType ('rb'), action='append', required = True,
+                         help = "Input binary filename.  Multiple input files are combined into a single PCD.")
+    parser.add_argument ("-o", "--output", dest = 'OutputFile', type = argparse.FileType ('wb'),
+                         help = "Output filename for PCD value or PCD statement")
+    parser.add_argument ("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
+                         help = "Name of the PCD in the form <PcdTokenSpaceGuidCName>.<PcdCName>")
+    parser.add_argument ("-t", "--type", dest = 'PcdType', default = None, choices = ['VPD','HII'],
+                         help = "PCD statement type (HII or VPD).  Default is standard.")
+    parser.add_argument ("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,
+                         help = "Maximum size of the PCD.  Ignored with --type HII.")
+    parser.add_argument ("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,
+                         help = "VPD offset if --type is VPD.  UEFI Variable offset if --type is HII.  Must be 8-byte aligned.")
+    parser.add_argument ("-n", "--variable-name", dest = 'VariableName',
+                         help = "UEFI variable name.  Only used with --type HII.")
+    parser.add_argument ("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',
+                         help = "UEFI variable GUID C name.  Only used with --type HII.")
+    parser.add_argument ("-x", "--xdr", dest = 'Xdr', action = "store_true",
+                         help = "Encode PCD using the Variable-Length Opaque Data format of RFC 4506 External Data Representation Standard (XDR)")
+    parser.add_argument ("-v", "--verbose", dest = 'Verbose', action = "store_true",
+                         help = "Increase output messages")
+    parser.add_argument ("-q", "--quiet", dest = 'Quiet', action = "store_true",
+                         help = "Reduce output messages")
+    parser.add_argument ("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range (0, 10), default = 0,
+                         help = "Set debug level")
 
-  #
-  # Convert PCD to an encoded string of hex values and determine the size of
-  # the encoded PCD in bytes.
-  #
-  PcdValue, PcdSize = ByteArray (Buffer, args.Xdr)
+    #
+    # Parse command line arguments
+    #
+    args = parser.parse_args ()
 
-  #
-  # Convert binary buffer to a DSC file PCD statement
-  #
-  if args.PcdName is None:
     #
-    # If PcdName is None, then only a PCD value is being requested.
+    # Read all binary input files
     #
-    Pcd = PcdValue
-    if args.Verbose:
-      print ('BinToPcd: Convert binary file to PCD Value')
-  elif args.PcdType is None:
+    Buffer = []
+    for File in args.InputFile:
+        try:
+            Buffer.append (File.read ())
+            File.close ()
+        except:
+            print ('BinToPcd: error: can not read binary input file {File}'.format (File = File))
+            sys.exit (1)
+
     #
-    # If --type is neither VPD nor HII, then use PCD statement syntax that is
-    # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
-    # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
+    # Convert PCD to an encoded string of hex values and determine the size of
+    # the encoded PCD in bytes.
     #
-    if args.MaxSize is None:
-      #
-      # If --max-size is not provided, then do not generate the syntax that
-      # includes the maximum size.
-      #
-      Pcd = '  {Name}|{Value}'.format (Name = args.PcdName, Value = PcdValue)
-    elif args.MaxSize < PcdSize:
-      print ('BinToPcd: error: argument --max-size is smaller than input file.')
-      sys.exit(1)
-    else:
-      Pcd = '  {Name}|{Value}|VOID*|{Size}'.format (Name = args.PcdName, Value = PcdValue, Size = args.MaxSize)
+    PcdValue, PcdSize = ByteArray (Buffer, args.Xdr)
 
-    if args.Verbose:
-      print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections:')
-      print ('    [PcdsFixedAtBuild]')
-      print ('    [PcdsPatchableInModule]')
-      print ('    [PcdsDynamicDefault]')
-      print ('    [PcdsDynamicExDefault]')
-  elif args.PcdType == 'VPD':
-    if args.MaxSize is None:
-      #
-      # If --max-size is not provided, then set maximum size to the size of the
-      # binary input file
-      #
-      args.MaxSize = PcdSize
-    if args.MaxSize < PcdSize:
-      print ('BinToPcd: error: argument --max-size is smaller than input file.')
-      sys.exit(1)
-    if args.Offset is None:
-      #
-      # if --offset is not provided, then set offset field to '*' so build
-      # tools will compute offset of PCD in VPD region.
-      #
-      Pcd = '  {Name}|*|{Size}|{Value}'.format (Name = args.PcdName, Size = args.MaxSize, Value = PcdValue)
-    else:
-      #
-      # --offset value must be 8-byte aligned
-      #
-      if (args.Offset % 8) != 0:
-        print ('BinToPcd: error: argument --offset must be 8-byte aligned.')
-        sys.exit(1)
-      #
-      # Use the --offset value provided.
-      #
-      Pcd = '  {Name}|{Offset}|{Size}|{Value}'.format (Name = args.PcdName, Offset = args.Offset, Size = args.MaxSize, Value = PcdValue)
-    if args.Verbose:
-      print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')
-      print ('    [PcdsDynamicVpd]')
-      print ('    [PcdsDynamicExVpd]')
-  elif args.PcdType == 'HII':
-    if args.VariableGuid is None or args.VariableName is None:
-      print ('BinToPcd: error: arguments --variable-guid and --variable-name are required for --type HII.')
-      sys.exit(1)
-    if args.Offset is None:
-      #
-      # Use UEFI Variable offset of 0 if --offset is not provided
-      #
-      args.Offset = 0
     #
-    # --offset value must be 8-byte aligned
+    # Convert binary buffer to a DSC file PCD statement
     #
-    if (args.Offset % 8) != 0:
-      print ('BinToPcd: error: argument --offset must be 8-byte aligned.')
-      sys.exit(1)
-    Pcd = '  {Name}|L"{VarName}"|{VarGuid}|{Offset}|{Value}'.format (Name = args.PcdName, VarName = args.VariableName, VarGuid = args.VariableGuid, Offset = args.Offset, Value = PcdValue)
-    if args.Verbose:
-      print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')
-      print ('    [PcdsDynamicHii]')
-      print ('    [PcdsDynamicExHii]')
+    if args.PcdName is None:
+        #
+        # If PcdName is None, then only a PCD value is being requested.
+        #
+        Pcd = PcdValue
+        if args.Verbose:
+            print ('BinToPcd: Convert binary file to PCD Value')
+    elif args.PcdType is None:
+        #
+        # If --type is neither VPD nor HII, then use PCD statement syntax that is
+        # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
+        # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
+        #
+        if args.MaxSize is None:
+            #
+            # If --max-size is not provided, then do not generate the syntax that
+            # includes the maximum size.
+            #
+            Pcd = '  {Name}|{Value}'.format (Name = args.PcdName, Value = PcdValue)
+        elif args.MaxSize < PcdSize:
+            print ('BinToPcd: error: argument --max-size is smaller than input file.')
+            sys.exit (1)
+        else:
+            Pcd = '  {Name}|{Value}|VOID*|{Size}'.format (Name = args.PcdName, Value = PcdValue, Size = args.MaxSize)
+
+        if args.Verbose:
+            print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections:')
+            print ('    [PcdsFixedAtBuild]')
+            print ('    [PcdsPatchableInModule]')
+            print ('    [PcdsDynamicDefault]')
+            print ('    [PcdsDynamicExDefault]')
+    elif args.PcdType == 'VPD':
+        if args.MaxSize is None:
+            #
+            # If --max-size is not provided, then set maximum size to the size of the
+            # binary input file
+            #
+            args.MaxSize = PcdSize
+        if args.MaxSize < PcdSize:
+            print ('BinToPcd: error: argument --max-size is smaller than input file.')
+            sys.exit (1)
+        if args.Offset is None:
+            #
+            # if --offset is not provided, then set offset field to '*' so build
+            # tools will compute offset of PCD in VPD region.
+            #
+            Pcd = '  {Name}|*|{Size}|{Value}'.format (Name = args.PcdName, Size = args.MaxSize, Value = PcdValue)
+        else:
+            #
+            # --offset value must be 8-byte aligned
+            #
+            if (args.Offset % 8) != 0:
+                print ('BinToPcd: error: argument --offset must be 8-byte aligned.')
+                sys.exit (1)
+            #
+            # Use the --offset value provided.
+            #
+            Pcd = '  {Name}|{Offset}|{Size}|{Value}'.format (Name = args.PcdName, Offset = args.Offset, Size = args.MaxSize, Value = PcdValue)
+        if args.Verbose:
+            print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')
+            print ('    [PcdsDynamicVpd]')
+            print ('    [PcdsDynamicExVpd]')
+    elif args.PcdType == 'HII':
+        if args.VariableGuid is None or args.VariableName is None:
+            print ('BinToPcd: error: arguments --variable-guid and --variable-name are required for --type HII.')
+            sys.exit (1)
+        if args.Offset is None:
+            #
+            # Use UEFI Variable offset of 0 if --offset is not provided
+            #
+            args.Offset = 0
+        #
+        # --offset value must be 8-byte aligned
+        #
+        if (args.Offset % 8) != 0:
+            print ('BinToPcd: error: argument --offset must be 8-byte aligned.')
+            sys.exit (1)
+        Pcd = '  {Name}|L"{VarName}"|{VarGuid}|{Offset}|{Value}'.format (Name = args.PcdName, VarName = args.VariableName, VarGuid = args.VariableGuid, Offset = args.Offset, Value = PcdValue)
+        if args.Verbose:
+            print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')
+            print ('    [PcdsDynamicHii]')
+            print ('    [PcdsDynamicExHii]')
 
-  #
-  # Write PCD value or PCD statement to the output file
-  #
-  try:
-    args.OutputFile.write (Pcd)
-    args.OutputFile.close ()
-  except:
     #
-    # If output file is not specified or it can not be written, then write the
-    # PCD value or PCD statement to the console
+    # Write PCD value or PCD statement to the output file
     #
-    print (Pcd)
+    try:
+        args.OutputFile.write (Pcd)
+        args.OutputFile.close ()
+    except:
+        #
+        # If output file is not specified or it can not be written, then write the
+        # PCD value or PCD statement to the console
+        #
+        print (Pcd)
-- 
2.14.2.windows.3



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks
  2018-06-09  6:15 [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Kinney, Michael D
                   ` (4 preceding siblings ...)
  2018-06-09  6:15 ` [Patch 5/5] BaseTools/BinToPcd: Follow PEP-8 indent of 4 spaces Kinney, Michael D
@ 2018-06-12  0:40 ` Zhu, Yonghong
  5 siblings, 0 replies; 7+ messages in thread
From: Zhu, Yonghong @ 2018-06-12  0:40 UTC (permalink / raw)
  To: Kinney, Michael D, edk2-devel@lists.01.org
  Cc: Sun, Yanyan, Gao, Liming, Zhu, Yonghong

Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com> 

Best Regards,
Zhu Yonghong

-----Original Message-----
From: Kinney, Michael D 
Sent: Saturday, June 09, 2018 2:15 PM
To: edk2-devel@lists.01.org
Cc: Sun, Yanyan <yanyan.sun@intel.com>; Zhu, Yonghong <yonghong.zhu@intel.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks

https://bugzilla.tianocore.org/show_bug.cgi?id=962
https://bugzilla.tianocore.org/show_bug.cgi?id=963
https://bugzilla.tianocore.org/show_bug.cgi?id=974
https://bugzilla.tianocore.org/show_bug.cgi?id=965

* Change "PcdToBin" to "BinToPcd"
* Update error message for --type HII.  If either --variable-guid
  or --variable-name is missing, then print an error message that
  states that both --variable-guid and --variable-name are required.
* --offset must be 8-byte aligned.
* Compatible with both Python 2.x and Python 3.x.
* Return error code when error message printed.
* Follow PEP-8 indent of 4 spaces.  https://www.python.org/dev/peps/pep-0008/

Cc: Yanyan Sun <yanyan.sun@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1

Kinney, Michael D (5):
  BaseTools/BinToPcd: Fix typo in error messages
  BaseTools/BinToPcd: Clarify error message for --type HII
  BaseTools/BinToPcd: --offset must be 8-byte aligned
  BaseTools/BinToPcd: Update for Python 3 compatibility
  BaseTools/BinToPcd: Follow PEP-8 indent of 4 spaces

 BaseTools/Scripts/BinToPcd.py | 350 +++++++++++++++++++++---------------------
 1 file changed, 179 insertions(+), 171 deletions(-)

-- 
2.14.2.windows.3



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-06-12  0:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-06-09  6:15 [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Kinney, Michael D
2018-06-09  6:15 ` [Patch 1/5] BaseTools/BinToPcd: Fix typo in error messages Kinney, Michael D
2018-06-09  6:15 ` [Patch 2/5] BaseTools/BinToPcd: Clarify error message for --type HII Kinney, Michael D
2018-06-09  6:15 ` [Patch 3/5] BaseTools/BinToPcd: --offset must be 8-byte aligned Kinney, Michael D
2018-06-09  6:15 ` [Patch 4/5] BaseTools/BinToPcd: Update for Python 3 compatibility Kinney, Michael D
2018-06-09  6:15 ` [Patch 5/5] BaseTools/BinToPcd: Follow PEP-8 indent of 4 spaces Kinney, Michael D
2018-06-12  0:40 ` [Patch 0/5] BaseTools/BinToPcd: Multiple clean up tasks Zhu, Yonghong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox