]> xenbits.xensource.com Git - seabios.git/commitdiff
build: Be careful with unicode and byte strings for python3 compatibility.
authorJohannes Krampf <johannes.krampf@googlemail.com>
Sun, 19 Jan 2014 15:03:49 +0000 (16:03 +0100)
committerKevin O'Connor <kevin@koconnor.net>
Mon, 20 Jan 2014 17:42:44 +0000 (12:42 -0500)
Signed-off-by: Johannes Krampf <johannes.krampf@googlemail.com>
scripts/buildrom.py
scripts/checkrom.py
scripts/layoutrom.py
scripts/python23compat.py [new file with mode: 0644]
scripts/readserial.py
scripts/transdump.py
scripts/vgafixup.py

index 36de14ec08491a8e1c9b11b9407833b114d2f7ef..8ff60e25d86df55a1f210687220fc234c607f0ee 100755 (executable)
@@ -7,6 +7,8 @@
 
 import sys
 
+from python23compat import as_bytes
+
 def alignpos(pos, alignbytes):
     mask = alignbytes - 1
     return (pos + mask) & ~mask
@@ -26,7 +28,7 @@ def main():
     count = len(data)
 
     # Pad to a 512 byte boundary
-    data += "\0" * (alignpos(count, 512) - count)
+    data += as_bytes("\0") * (alignpos(count, 512) - count)
     count = len(data)
 
     # Check if a pci header is present
@@ -35,7 +37,7 @@ def main():
         data = data[:pcidata + 16] + chr(int(count/512)) + chr(0) + data[pcidata + 18:]
 
     # Fill in size field; clear checksum field
-    data = data[:2] + chr(int(count/512)) + data[3:6] + "\0" + data[7:]
+    data = data[:2] + chr(int(count/512)) + data[3:6] + as_bytes("\0") + data[7:]
 
     # Checksum rom
     newsum = (256 - checksum(data)) & 0xff
index 30c9db248750fd9b47aaa6fdcc9fc86e8c736171..83d4671563c2d47cc27abadb61ec4dfd67acc52f 100755 (executable)
@@ -8,6 +8,8 @@
 import sys
 import layoutrom
 
+from python23compat import as_bytes
+
 def subst(data, offset, new):
     return data[:offset] + new + data[offset + len(new):]
 
@@ -24,7 +26,7 @@ def main():
     objinfo, finalsize, rawfile, outfile = sys.argv[1:]
 
     # Read in symbols
-    objinfofile = open(objinfo, 'rb')
+    objinfofile = open(objinfo, 'r')
     symbols = layoutrom.parseObjDump(objinfofile, 'in')[1]
 
     # Read in raw file
@@ -90,7 +92,7 @@ def main():
 
     # Write final file
     f = open(outfile, 'wb')
-    f.write(("\0" * (finalsize - datasize)) + rawdata)
+    f.write((as_bytes("\0") * (finalsize - datasize)) + rawdata)
     f.close()
 
 if __name__ == '__main__':
index 3e57787d773afdf2a885bd1cfc73b25b30a19910..b3254061ab2276160bc42613318d259330949035 100755 (executable)
@@ -363,7 +363,7 @@ def writeLinkerScripts(li, out16, out32seg, out32flat):
        int(li.zonelow_base / 16),
        li.sec16_start - BUILD_BIOS_ADDR,
        outRelSections(li.sections16, 'code16_start', useseg=1))
-    outfile = open(out16, 'wb')
+    outfile = open(out16, 'w')
     outfile.write(COMMONHEADER + out + COMMONTRAILER)
     outfile.close()
 
@@ -375,7 +375,7 @@ def writeLinkerScripts(li, out16, out32seg, out32flat):
     }
 """ % (li.sec32seg_start - BUILD_BIOS_ADDR,
        outRelSections(li.sections32seg, 'code32seg_start', useseg=1))
-    outfile = open(out32seg, 'wb')
+    outfile = open(out32seg, 'w')
     outfile.write(COMMONHEADER + out + COMMONTRAILER)
     outfile.close()
 
@@ -445,7 +445,7 @@ PHDRS
         text PT_LOAD AT ( code32flat_start ) ;
 }
 """
-    outfile = open(out32flat, 'wb')
+    outfile = open(out32flat, 'w')
     outfile.write(out)
     outfile.close()
 
@@ -644,7 +644,7 @@ def parseObjDump(file, fileid):
 
 # Parser for constants in simple C header files.
 def scanconfig(file):
-    f = open(file, 'rb')
+    f = open(file, 'r')
     opts = {}
     for l in f.readlines():
         parts = l.split()
@@ -663,9 +663,9 @@ def main():
     in16, in32seg, in32flat, cfgfile, out16, out32seg, out32flat = sys.argv[1:]
 
     # Read in the objdump information
-    infile16 = open(in16, 'rb')
-    infile32seg = open(in32seg, 'rb')
-    infile32flat = open(in32flat, 'rb')
+    infile16 = open(in16, 'r')
+    infile32seg = open(in32seg, 'r')
+    infile32flat = open(in32flat, 'r')
 
     # infoX = (sections, symbols)
     info16 = parseObjDump(infile16, '16')
diff --git a/scripts/python23compat.py b/scripts/python23compat.py
new file mode 100644 (file)
index 0000000..572b7f1
--- /dev/null
@@ -0,0 +1,14 @@
+# Helper code for compatibility of the code with both Python 2 and Python 3
+#
+# Copyright (C) 2014 Johannes Krampf <johannes.krampf@googlemail.com>
+#
+# This file may be distributed under the terms of the GNU GPLv3 license.
+
+import sys
+
+if (sys.version_info > (3, 0)):
+    def as_bytes(str):
+        return bytes(str, "ASCII")
+else:
+    def as_bytes(str):
+        return str
index 5b40fdc0726b0b9dca4f5bb629a76dbac75ebc99..4f29648d24ddbf1880ee3c75d36fb63387d379eb 100755 (executable)
@@ -13,6 +13,8 @@ import time
 import select
 import optparse
 
+from python23compat import as_bytes
+
 # Reset time counter after this much idle time.
 RESTARTINTERVAL = 60
 # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
@@ -25,7 +27,7 @@ def calibrateserialwrite(outfile, byteadjust):
     data = data * 80
     while 1:
         st = time.time()
-        outfile.write(data)
+        outfile.write(as_bytes(data))
         outfile.flush()
         et = time.time()
         sys.stdout.write(
@@ -85,11 +87,11 @@ def readserial(infile, logfile, byteadjust):
             msg = "\n\n======= %s (adjust=%.1fus)\n" % (
                 time.asctime(time.localtime(datatime)), byteadjust * 1000000)
             sys.stdout.write(msg)
-            logfile.write(msg)
+            logfile.write(as_bytes(msg))
         lasttime = datatime
 
         # Translate unprintable chars; add timestamps
-        out = ""
+        out = as_bytes("")
         for c in d:
             if isnewline:
                 delta = datatime - starttime - (charcount * byteadjust)
@@ -113,7 +115,10 @@ def readserial(infile, logfile, byteadjust):
                 continue
             out += c
 
-        sys.stdout.write(out)
+        if (sys.version_info > (3, 0)):
+            sys.stdout.buffer.write(out)
+        else:
+            sys.stdout.write(out)
         sys.stdout.flush()
         logfile.write(out)
         logfile.flush()
index 4caaeb70b7e89ca54188525cdaf45dbbe73dd68f..665f04a000fccd9250a94357be9c16891a45c855 100755 (executable)
@@ -44,7 +44,10 @@ def main():
         filehdl = open(filename, 'r')
     mem = parseMem(filehdl)
     for i in mem:
-        sys.stdout.write(struct.pack("<I", i))
+        if (sys.version_info > (3, 0)):
+            sys.stdout.buffer.write(struct.pack("<I", i))
+        else:
+            sys.stdout.write(struct.pack("<I", i))
 
 if __name__ == '__main__':
     main()
index 2493f351bc65e6c5e9a05cdc3261beeb38924d8b..a981bbf953fb53e497c6f8ab73184b5fc0369c95 100644 (file)
@@ -20,7 +20,7 @@ import sys
 
 def main():
     infilename, outfilename = sys.argv[1:]
-    infile = open(infilename, 'rb')
+    infile = open(infilename, 'r')
     out = []
     for line in infile:
         sline = line.strip()
@@ -33,7 +33,7 @@ def main():
         else:
             out.append(line)
     infile.close()
-    outfile = open(outfilename, 'wb')
+    outfile = open(outfilename, 'w')
     outfile.write(''.join(out))
     outfile.close()