]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools ConvertMasmToNasm: Support Python 3
authorJordan Justen <jordan.l.justen@intel.com>
Mon, 7 Mar 2016 07:38:59 +0000 (23:38 -0800)
committerJordan Justen <jordan.l.justen@intel.com>
Thu, 10 Mar 2016 18:01:40 +0000 (10:01 -0800)
The script is updated to support both python 2.7 and python 3.

v2:
 * Use io.open() rather than open() (Jaben)

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Erik Bjorge <erik.c.bjorge@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Erik Bjorge <erik.c.bjorge@intel.com>
BaseTools/Scripts/ConvertMasmToNasm.py

index 82889721069da859c929e1e1cbb4c2ebf2cbe507..6a0d27dadb47eab4ec43923e0826928f6ca66d4d 100755 (executable)
 #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 #\r
 \r
+from __future__ import print_function\r
+\r
 #\r
 # Import Modules\r
 #\r
 import argparse\r
+import io\r
 import os.path\r
 import re\r
-import StringIO\r
 import subprocess\r
 import sys\r
 \r
@@ -152,12 +154,12 @@ class CommonUtils:
         (stdout, stderr) = p.communicate(pipeIn)\r
         if checkExitCode:\r
             if p.returncode != 0:\r
-                print 'command:', ' '.join(cmd)\r
-                print 'stdout:', stdout\r
-                print 'stderr:', stderr\r
-                print 'return:', p.returncode\r
+                print('command:', ' '.join(cmd))\r
+                print('stdout:', stdout)\r
+                print('stderr:', stderr)\r
+                print('return:', p.returncode)\r
             assert p.returncode == 0\r
-        return stdout\r
+        return stdout.decode('utf-8', 'ignore')\r
 \r
     def FileUpdated(self, path):\r
         if not self.git or not self.gitdir:\r
@@ -181,7 +183,7 @@ class CommonUtils:
             return\r
 \r
         if not self.args.quiet:\r
-            print 'Committing: Conversion of', dst\r
+            print('Committing: Conversion of', dst)\r
 \r
         prefix = ' '.join(filter(lambda a: a, [pkg, module]))\r
         message = ''\r
@@ -195,6 +197,7 @@ class CommonUtils:
         message += 'Contributed-under: TianoCore Contribution Agreement 1.0\n'\r
         assert(self.gitemail is not None)\r
         message += 'Signed-off-by: %s\n' % self.gitemail\r
+        message = message.encode('utf-8', 'ignore')\r
 \r
         cmd = ('git', 'commit', '-F', '-')\r
         self.RunAndCaptureOutput(cmd, pipeIn=message)\r
@@ -226,23 +229,22 @@ class ConvertAsmFile(CommonUtils):
 \r
         self.inputFileBase = os.path.basename(self.inputFilename)\r
         self.outputFileBase = os.path.basename(self.outputFilename)\r
-        if self.outputFilename == '-' and not self.diff:\r
-            self.output = sys.stdout\r
-        else:\r
-            self.output = StringIO.StringIO()\r
+        self.output = io.BytesIO()\r
         if not self.args.quiet:\r
             dirpath, src = os.path.split(self.inputFilename)\r
             dirpath = self.RootRelative(dirpath)\r
             dst = os.path.basename(self.outputFilename)\r
-            print 'Converting:', dirpath, src, '->', dst\r
-        lines = open(self.inputFilename).readlines()\r
+            print('Converting:', dirpath, src, '->', dst)\r
+        lines = io.open(self.inputFilename).readlines()\r
         self.Convert(lines)\r
-        if self.outputFilename == '-':\r
-            if self.diff:\r
-                sys.stdout.write(self.output.getvalue())\r
-                self.output.close()\r
+        if self.outputFilename == '-' and not self.diff:\r
+            output_data = self.output.getvalue()\r
+            if sys.version_info >= (3, 0):\r
+                output_data = output_data.decode('utf-8', 'ignore')\r
+            sys.stdout.write(output_data)\r
+            self.output.close()\r
         else:\r
-            f = open(self.outputFilename, 'wb')\r
+            f = io.open(self.outputFilename, 'wb')\r
             f.write(self.output.getvalue())\r
             f.close()\r
             self.output.close()\r
@@ -521,18 +523,18 @@ class ConvertAsmFile(CommonUtils):
         return '.%d' % count\r
 \r
     def EmitString(self, string):\r
-        self.output.write(string)\r
+        self.output.write(string.encode('utf-8', 'ignore'))\r
 \r
     def EmitLineWithDiff(self, old, new):\r
         newLine = (self.indent + new).rstrip()\r
         if self.diff:\r
             if old is None:\r
-                print '+%s' % newLine\r
+                print('+%s' % newLine)\r
             elif newLine != old:\r
-                print '-%s' % old\r
-                print '+%s' % newLine\r
+                print('-%s' % old)\r
+                print('+%s' % newLine)\r
             else:\r
-                print '', newLine\r
+                print('', newLine)\r
         if newLine != '':\r
             self.newAsmEmptyLineCount = 0\r
         self.EmitString(newLine + '\r\n')\r
@@ -565,7 +567,7 @@ class ConvertAsmFile(CommonUtils):
         if emitNewLine:\r
             self.EmitLine(newLine.rstrip())\r
         elif self.diff:\r
-            print '-%s' % self.originalLine\r
+            print('-%s' % self.originalLine)\r
 \r
     leaRe = re.compile(r'''\r
                            (lea \s+) ([\w@][\w@0-9]*) \s* , \s* (\S (?:.*\S)?)\r
@@ -759,7 +761,7 @@ class ConvertInfFile(CommonUtils):
     def ScanInfAsmFiles(self):\r
         src = self.inf\r
         assert os.path.isfile(src)\r
-        f = open(src)\r
+        f = io.open(src, 'rt')\r
         self.lines = f.readlines()\r
         f.close()\r
 \r
@@ -801,17 +803,16 @@ class ConvertInfFile(CommonUtils):
         unsupportedArchCount = 0\r
         for dst in self:\r
             didSomething = False\r
-            fileChanged = self.UpdateInfAsmFile(dst)\r
             try:\r
                 self.UpdateInfAsmFile(dst)\r
                 didSomething = True\r
             except UnsupportedConversion:\r
                 if not self.args.quiet:\r
-                    print 'MASM=>NASM conversion unsupported for', dst\r
+                    print('MASM=>NASM conversion unsupported for', dst)\r
                 notConverted.append(dst)\r
             except NoSourceFile:\r
                 if not self.args.quiet:\r
-                    print 'Source file missing for', reldst\r
+                    print('Source file missing for', reldst)\r
                 notConverted.append(dst)\r
             except UnsupportedArch:\r
                 unsupportedArchCount += 1\r
@@ -821,9 +822,9 @@ class ConvertInfFile(CommonUtils):
         if len(notConverted) > 0 and not self.args.quiet:\r
             for dst in notConverted:\r
                 reldst = self.RootRelative(dst)\r
-                print 'Unabled to convert', reldst\r
+                print('Unabled to convert', reldst)\r
         if unsupportedArchCount > 0 and not self.args.quiet:\r
-            print 'Skipped', unsupportedArchCount, 'files based on architecture'\r
+            print('Skipped', unsupportedArchCount, 'files based on architecture')\r
 \r
     def UpdateInfAsmFile(self, dst, IgnoreMissingAsm=False):\r
         infPath = os.path.split(os.path.realpath(self.inf))[0]\r
@@ -842,8 +843,9 @@ class ConvertInfFile(CommonUtils):
 \r
         lastLine = ''\r
         fileChanged = False\r
-        for i in range(len(self.lines)):\r
-            line = self.lines[i].rstrip()\r
+        i = 0\r
+        for line in self.lines:\r
+            line = line.rstrip()\r
             updatedLine = line\r
             for src in self.dstToSrc[dst]:\r
                 assert self.srcToDst[src] == dst\r
@@ -855,22 +857,24 @@ class ConvertInfFile(CommonUtils):
                 if lastLine.strip() == updatedLine.strip():\r
                     self.lines[i] = None\r
                 else:\r
-                    self.lines[i] = updatedLine + '\r\n'\r
+                    self.lines[i] = updatedLine + '\n'\r
 \r
             if self.diff:\r
                 if lineChanged:\r
-                    print '-%s' % line\r
+                    print('-%s' % line)\r
                     if self.lines[i] is not None:\r
-                        print '+%s' % updatedLine\r
+                        print('+%s' % updatedLine)\r
                 else:\r
-                    print '', line\r
+                    print('', line)\r
 \r
             fileChanged |= lineChanged\r
             if self.lines[i] is not None:\r
                 lastLine = self.lines[i]\r
 \r
+            i += 1\r
+\r
         if fileChanged:\r
-            self.lines = filter(lambda l: l is not None, self.lines)\r
+            self.lines = list(filter(lambda l: l is not None, self.lines))\r
 \r
         for src in self.dstToSrc[dst]:\r
             if not src.endswith('.asm'):\r
@@ -879,7 +883,7 @@ class ConvertInfFile(CommonUtils):
                     self.RemoveFile(fullSrc)\r
 \r
         if fileChanged:\r
-            f = open(self.inf, 'wb')\r
+            f = io.open(self.inf, 'w', newline='\r\n')\r
             f.writelines(self.lines)\r
             f.close()\r
             self.FileUpdated(self.inf)\r
@@ -917,11 +921,11 @@ class ConvertInfFiles(CommonUtils):
                     didSomething = True\r
             except UnsupportedConversion:\r
                 if not self.args.quiet:\r
-                    print 'MASM=>NASM conversion unsupported for', reldst\r
+                    print('MASM=>NASM conversion unsupported for', reldst)\r
                 notConverted.append(dst)\r
             except NoSourceFile:\r
                 if not self.args.quiet:\r
-                    print 'Source file missing for', reldst\r
+                    print('Source file missing for', reldst)\r
                 notConverted.append(dst)\r
             except UnsupportedArch:\r
                 unsupportedArchCount += 1\r
@@ -931,9 +935,9 @@ class ConvertInfFiles(CommonUtils):
         if len(notConverted) > 0 and not self.args.quiet:\r
             for dst in notConverted:\r
                 reldst = self.RootRelative(dst)\r
-                print 'Unabled to convert', reldst\r
+                print('Unabled to convert', reldst)\r
         if unsupportedArchCount > 0 and not self.args.quiet:\r
-            print 'Skipped', unsupportedArchCount, 'files based on architecture'\r
+            print('Skipped', unsupportedArchCount, 'files based on architecture')\r
 \r
 \r
 class ConvertDirectories(CommonUtils):\r
@@ -968,7 +972,7 @@ class ConvertAsmApp(CommonUtils):
         src = self.args.source\r
         dst = self.args.dest\r
         if self.infmode:\r
-            ConvertInfFiles(src, self)\r
+            ConvertInfFiles((src,), self)\r
         elif self.dirmode:\r
             ConvertDirectories((src,), self)\r
         elif not self.dirmode:\r