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
# 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 <base filename> > output-file.lmp"
print("Usage: %s <base filename> > output-file.lmp" % sys.argv[0])
sys.exit(1)
colors = read_palette(sys.argv[1])

View file

@ -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)))
for color in palette:
color = tuple(map(clamp_pixval, color))
def encode_color(color):
return "".join(map(color_byte, color))
encoded = map(encode_color, pal)
return "".join(encoded)
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 <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])
@ -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)

View file

@ -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]")

View file

@ -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)

View file

@ -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: