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 <fraggle@gmail.com>
This commit is contained in:
Simon Howard 2010-11-09 23:11:11 +00:00
parent 8bad55ffef
commit d6907ea974
5 changed files with 39 additions and 36 deletions

View file

@ -24,19 +24,22 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import sys import sys
import struct
# Return palette read from named file # Return palette read from named file
def read_palette(filename): def read_palette(filename):
f = file(filename) f = open(filename, "rb")
colors = [] colors = []
for i in range(256): 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 return colors
@ -82,7 +85,8 @@ def generate_darkened_colormap(colors, factor):
def output_colormap(colormap): def output_colormap(colormap):
for c in 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): def inverse_color(color):
average = (color[0] + color[1] + color[2]) / 3 average = (color[0] + color[1] + color[2]) / 3
@ -95,12 +99,12 @@ def print_palette(colors):
for x in range(16): for x in range(16):
color = colors[y * 16 + x] color = colors[y * 16 + x]
print "#%02x%02x%02x" % color, print("#%02x%02x%02x" % color)
print print()
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Usage: %s <base filename> > output-file.lmp" print("Usage: %s <base filename> > output-file.lmp" % sys.argv[0])
sys.exit(1) sys.exit(1)
colors = read_palette(sys.argv[1]) colors = read_palette(sys.argv[1])

View file

@ -24,7 +24,9 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import sys import sys
import struct
# IHS (Intensity Hue Saturation) to RGB conversion, utility function # IHS (Intensity Hue Saturation) to RGB conversion, utility function
# #
@ -81,14 +83,15 @@ def make_palette_new():
# Return palette read from named file # Return palette read from named file
def read_palette(filename): def read_palette(filename):
f = file(filename) f = open(filename, "rb")
colors = [] colors = []
for i in range(256): 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() f.close()
@ -155,24 +158,21 @@ def clamp_pixval(v):
elif v > 255: elif v > 255:
return 255 return 255
else: else:
return v return int(v)
def encode_palette(pal): def output_palette(pal):
def color_byte(element): for color in palette:
return chr(int(clamp_pixval(element))) color = tuple(map(clamp_pixval, color))
def encode_color(color): encoded = struct.pack("BBB", *color)
return "".join(map(color_byte, color)) os.write(sys.stdout.fileno(), encoded)
encoded = map(encode_color, pal)
return "".join(encoded)
# Main program - make a base palette, then do the biased versions # Main program - make a base palette, then do the biased versions
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Usage: %s <base filename> > playpal.lmp" % sys.argv[0] print("Usage: %s <base filename> > playpal.lmp" % sys.argv[0])
sys.exit(1)
base_pal = read_palette(sys.argv[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)) palettes.append(bias_palette_towards(base_pal, (0, 255, 0), 0.2))
result = "".join(map(encode_palette, palettes)) for palette in palettes:
output_palette(palette)
sys.stdout.write(result)

View file

@ -48,26 +48,26 @@ def parse_texture_file():
patches[name] = True patches[name] = True
print "; autogenerated patch list\n" print("; autogenerated patch list\n")
for name in sorted(patches.keys()): for name in sorted(patches.keys()):
print name print(name)
# Generate a full list of textures from the files in the # Generate a full list of textures from the files in the
# patches/ directory # patches/ directory
def list_all_textures(): def list_all_textures():
print "; autogenerated patch list\n" print("; autogenerated patch list\n")
for filename in sorted(glob.glob("patches/*.gif")): for filename in sorted(glob.glob("patches/*.gif")):
base = os.path.basename(filename) base = os.path.basename(filename)
patch_name = base[0:-4] patch_name = base[0:-4]
print patch_name print(patch_name)
if len(sys.argv) == 1: if len(sys.argv) == 1:
parse_texture_file() parse_texture_file()
elif sys.argv[1] == "-a": elif sys.argv[1] == "-a":
list_all_textures() list_all_textures()
else: else:
print "Usage: extract-pnames.pl [-a]" print("Usage: extract-pnames.py [-a]")

View file

@ -84,7 +84,7 @@ def parse_stream(stream):
raise Exception("Mismatched #if in '%s'" % stream.name) raise Exception("Mismatched #if in '%s'" % stream.name)
def parse_file(filename): def parse_file(filename):
f = file(filename) f = open(filename)
try: try:
parse_stream(f) parse_stream(f)
@ -209,7 +209,7 @@ def read_block(stream, ignore):
func(arg) func(arg)
else: else:
if not ignore: if not ignore:
print line print(line)
parse_cmdline() parse_cmdline()
parse_stream(sys.stdin) parse_stream(sys.stdin)

View file

@ -73,9 +73,9 @@ def find_file(section, name):
# Warning header displayed at the top of an output file. # Warning header displayed at the top of an output file.
def print_warning_header(): def print_warning_header():
print "; This file is automatically generated." print("; This file is automatically generated.")
print "; Do not edit it directly!" print("; Do not edit it directly!")
print print("")
# Remove comments beginning with "#" or ";" # Remove comments beginning with "#" or ";"
@ -171,7 +171,7 @@ def parse_stream(stream):
if match: if match:
line = parse_assignment(section, line, match) line = parse_assignment(section, line, match)
print line print(line)
# Parse command line options: # Parse command line options: