From d6907ea9749b99d7b10f0e1837f6754287d9ea0b Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 9 Nov 2010 23:11:11 +0000 Subject: [PATCH] build: Update Python scripts to work with Python 3. Python 3 makes a number of changes to the language and breaks backward compatibility with Python 2 in some places. This change updates the Python scripts used in the build system to work with Python 3, although the updated scripts still run in Python 2 as well. Most of the changes are minor; the biggest changes are to the palette / colormap generation scripts. Signed-off-by: Simon Howard --- lumps/cph/misc-lumps/colormap.py | 18 +++++++++------- lumps/cph/misc-lumps/playpal.py | 35 ++++++++++++++++---------------- scripts/extract-pnames.py | 10 ++++----- scripts/simplecpp | 4 ++-- scripts/wadinfo-builder.py | 8 ++++---- 5 files changed, 39 insertions(+), 36 deletions(-) diff --git a/lumps/cph/misc-lumps/colormap.py b/lumps/cph/misc-lumps/colormap.py index 6f4cc170..58906838 100755 --- a/lumps/cph/misc-lumps/colormap.py +++ b/lumps/cph/misc-lumps/colormap.py @@ -24,19 +24,22 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +import os import sys +import struct # Return palette read from named file def read_palette(filename): - f = file(filename) + f = open(filename, "rb") colors = [] for i in range(256): - color = f.read(3) + data = f.read(3) - colors.append((ord(color[0]), ord(color[1]), ord(color[2]))) + color = struct.unpack("BBB", data) + colors.append(color) return colors @@ -82,7 +85,8 @@ def generate_darkened_colormap(colors, factor): def output_colormap(colormap): for c in colormap: - sys.stdout.write(chr(c)) + x = struct.pack("B", c) + os.write(sys.stdout.fileno(), x) def inverse_color(color): average = (color[0] + color[1] + color[2]) / 3 @@ -95,12 +99,12 @@ def print_palette(colors): for x in range(16): color = colors[y * 16 + x] - print "#%02x%02x%02x" % color, + print("#%02x%02x%02x" % color) - print + print() if len(sys.argv) < 2: - print "Usage: %s > output-file.lmp" + print("Usage: %s > output-file.lmp" % sys.argv[0]) sys.exit(1) colors = read_palette(sys.argv[1]) diff --git a/lumps/cph/misc-lumps/playpal.py b/lumps/cph/misc-lumps/playpal.py index bdd6ab68..2be6b48a 100755 --- a/lumps/cph/misc-lumps/playpal.py +++ b/lumps/cph/misc-lumps/playpal.py @@ -24,7 +24,9 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +import os import sys +import struct # IHS (Intensity Hue Saturation) to RGB conversion, utility function # @@ -81,14 +83,15 @@ def make_palette_new(): # Return palette read from named file def read_palette(filename): - f = file(filename) + f = open(filename, "rb") colors = [] for i in range(256): - color = f.read(3) + data = f.read(3) + color = struct.unpack("BBB", data) - colors.append((ord(color[0]), ord(color[1]), ord(color[2]))) + colors.append(color) f.close() @@ -155,24 +158,21 @@ def clamp_pixval(v): elif v > 255: return 255 else: - return v + return int(v) -def encode_palette(pal): +def output_palette(pal): - def color_byte(element): - return chr(int(clamp_pixval(element))) - - def encode_color(color): - return "".join(map(color_byte, color)) - - encoded = map(encode_color, pal) - - return "".join(encoded) + for color in palette: + color = tuple(map(clamp_pixval, color)) + + encoded = struct.pack("BBB", *color) + os.write(sys.stdout.fileno(), encoded) # Main program - make a base palette, then do the biased versions if len(sys.argv) < 2: - print "Usage: %s > playpal.lmp" % sys.argv[0] + print("Usage: %s > playpal.lmp" % sys.argv[0]) + sys.exit(1) base_pal = read_palette(sys.argv[1]) @@ -207,7 +207,6 @@ for i in range(4): palettes.append(bias_palette_towards(base_pal, (0, 255, 0), 0.2)) -result = "".join(map(encode_palette, palettes)) - -sys.stdout.write(result) +for palette in palettes: + output_palette(palette) diff --git a/scripts/extract-pnames.py b/scripts/extract-pnames.py index ccfabe2b..a501faba 100755 --- a/scripts/extract-pnames.py +++ b/scripts/extract-pnames.py @@ -48,26 +48,26 @@ def parse_texture_file(): patches[name] = True - print "; autogenerated patch list\n" + print("; autogenerated patch list\n") for name in sorted(patches.keys()): - print name + print(name) # Generate a full list of textures from the files in the # patches/ directory def list_all_textures(): - print "; autogenerated patch list\n" + print("; autogenerated patch list\n") for filename in sorted(glob.glob("patches/*.gif")): base = os.path.basename(filename) patch_name = base[0:-4] - print patch_name + print(patch_name) if len(sys.argv) == 1: parse_texture_file() elif sys.argv[1] == "-a": list_all_textures() else: - print "Usage: extract-pnames.pl [-a]" + print("Usage: extract-pnames.py [-a]") diff --git a/scripts/simplecpp b/scripts/simplecpp index b083680e..3aa1fab0 100755 --- a/scripts/simplecpp +++ b/scripts/simplecpp @@ -84,7 +84,7 @@ def parse_stream(stream): raise Exception("Mismatched #if in '%s'" % stream.name) def parse_file(filename): - f = file(filename) + f = open(filename) try: parse_stream(f) @@ -209,7 +209,7 @@ def read_block(stream, ignore): func(arg) else: if not ignore: - print line + print(line) parse_cmdline() parse_stream(sys.stdin) diff --git a/scripts/wadinfo-builder.py b/scripts/wadinfo-builder.py index cb2fd896..a84a183d 100755 --- a/scripts/wadinfo-builder.py +++ b/scripts/wadinfo-builder.py @@ -73,9 +73,9 @@ def find_file(section, name): # Warning header displayed at the top of an output file. def print_warning_header(): - print "; This file is automatically generated." - print "; Do not edit it directly!" - print + print("; This file is automatically generated.") + print("; Do not edit it directly!") + print("") # Remove comments beginning with "#" or ";" @@ -171,7 +171,7 @@ def parse_stream(stream): if match: line = parse_assignment(section, line, match) - print line + print(line) # Parse command line options: