# 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
(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
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
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
\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
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
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
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
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
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
\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
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
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
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
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
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