shithub: choc

Download patch

ref: 167d4df40b55f2e8b559a797bb93b09554a871d0
parent: 253731b66d4a70d5040d11e4b1acf8aa58230a46
author: Simon Howard <[email protected]>
date: Tue Nov 9 11:10:52 EST 2010

Update Python scripts to work in Python 3.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 2162

--- a/data/convert-icon
+++ b/data/convert-icon
@@ -1,7 +1,5 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 #
-# $Id: convert-icon 704 2006-10-18 00:51:11Z fraggle $
-#
 # Copyright(C) 2005 Simon Howard
 #
 # This program is free software; you can redistribute it and/or
@@ -29,7 +27,7 @@
 try:
         import Image
 except ImportError:
-        print "WARNING: Could not update %s.  Please install the Python Imaging library." % sys.argv[2]
+        print("WARNING: Could not update %s.  Please install the Python Imaging library." % sys.argv[2])
         sys.exit(0)
 
 
@@ -70,5 +68,4 @@
     outfile.write("};\n")
 
 convert_image(sys.argv[1], sys.argv[2])
-
 
--- a/man/docgen
+++ b/man/docgen
@@ -150,11 +150,8 @@
 show_vanilla_options = True
 
 class Parameter:
-    def __cmp__(self, other):
-        if self.name < other.name:
-            return -1
-        else:
-            return 1
+    def __lt__(self, other):
+        return self.name < other.name
 
     def __init__(self):
         self.text = ""
@@ -389,7 +386,7 @@
     try:
         for line in f:
             line = line.replace("@content", content)
-            print line.rstrip()
+            print(line.rstrip())
 
     finally:
         f.close()
@@ -407,7 +404,7 @@
     read_wikipages()
 
     for t in targets:
-        print t.wiki_output()
+        print(t.wiki_output())
 
 def plaintext_output(targets, template_file):
 
@@ -419,13 +416,13 @@
     print_template(template_file, content)
 
 def usage():
-    print "Usage: %s [-V] [-c filename ]( -m | -w | -p ) <directory>" \
-            % sys.argv[0]
-    print "   -c :  Provide documentation for the specified configuration file"
-    print "   -m :  Manpage output"
-    print "   -w :  Wikitext output"
-    print "   -p :  Plaintext output"
-    print "   -V :  Don't show Vanilla Doom options"
+    print("Usage: %s [-V] [-c filename ]( -m | -w | -p ) <directory>" \
+            % sys.argv[0])
+    print("   -c :  Provide documentation for the specified configuration file")
+    print("   -m :  Manpage output")
+    print("   -w :  Wikitext output")
+    print("   -p :  Plaintext output")
+    print("   -V :  Don't show Vanilla Doom options")
     sys.exit(0)
 
 # Parse command line
--- a/pkg/wince/wince-cabgen
+++ b/pkg/wince/wince-cabgen
@@ -3,10 +3,11 @@
 import os
 import re
 import shutil
+import struct
 import sys
 import tempfile
 
-CAB_HEADER = "MSCE"
+CAB_HEADER = "MSCE".encode("ascii")
 
 ARCHITECTURES = {
     "shx-sh3":        103,
@@ -58,16 +59,10 @@
 }
 
 def write_int16(f, value):
-    b1 = value & 0xff
-    b2 = (value >> 8) & 0xff
-    f.write("%c%c" % (b1, b2))
+    f.write(struct.pack("<H", value))
 
 def write_int32(f, value):
-    b1 = value & 0xff
-    b2 = (value >> 8) & 0xff
-    b3 = (value >> 16) & 0xff
-    b4 = (value >> 24) & 0xff
-    f.write("%c%c%c%c" % (b1, b2, b3, b4))
+    f.write(struct.pack("<I", value))
 
 # Pad a string with NUL characters so that it has a length that is 
 # a multiple of 4.  At least one NUL is always added.
@@ -208,7 +203,7 @@
         for i, s in self.string_list:
             write_int16(stream, i)
             write_int16(stream, len(s))
-            stream.write(s)
+            stream.write(s.encode("ascii"))
 
 class DirectoryList:
     def __init__(self, cab_header):
@@ -252,7 +247,7 @@
             #    dir_path = dir_path[1:]
             dir_path = [ dir ]
 
-            dir_path = map(lambda x: dictionary.get(x), dir_path)
+            dir_path = list(map(lambda x: dictionary.get(x), dir_path))
 
             self.directories[key] = self.index
             self.directories_list.append((self.index, dir_path))
@@ -334,7 +329,7 @@
             write_int16(stream, file_no)
             write_int32(stream, flags)
             write_int16(stream, len(filename))
-            stream.write(filename)
+            stream.write(filename.encode("ascii"))
 
 # TODO?
 
@@ -412,7 +407,7 @@
         # Map dirs that make up the path to strings.
 
         dictionary = self.cab_header.dictionary
-        dest_path = map(lambda x: dictionary.get(x), dest_path)
+        dest_path = list(map(lambda x: dictionary.get(x), dest_path))
 
         self.links.append((self.index, target_type, target_id,
                            base_dir, dest_path))
@@ -492,6 +487,7 @@
             section.write(stream)
             pos = stream.tell()
             if pos != old_pos + len(section):
+                print(section)
                 raise Exception("Section is %i bytes long, but %i written" % \
                                 (len(section), pos - old_pos))
             old_pos = pos
@@ -574,7 +570,7 @@
         basename = self.__shorten_name(self.files[0], 0)
         filename = os.path.join(dir, basename)
 
-        stream = file(filename, "w")
+        stream = open(filename, "wb")
         self.cab_header.write(stream)
         stream.close()
 
@@ -625,17 +621,17 @@
     # Expand $(xyz) path variables to their Windows equivalents:
 
     def replace_var(match):
-	var_name = match.group(1)
+        var_name = match.group(1)
 
-	if not var_name in DIR_VARIABLES:
-	    raise Exception("Unknown variable '%s'" % var_name)
-	else:
-	    return DIR_VARIABLES[var_name]
+        if not var_name in DIR_VARIABLES:
+            raise Exception("Unknown variable '%s'" % var_name)
+        else:
+            return DIR_VARIABLES[var_name]
 
     return re.sub(r"\$\((.*?)\)", replace_var, filename)
 
 def read_config_file(filename):
-    f = file(filename)
+    f = open(filename)
 
     data = f.readlines()
     data = "".join(data)
@@ -656,10 +652,10 @@
     files_list = config["files"]
 
     for dest, source_file in files_list.items():
-        print source_file
+        print(source_file)
 
 if len(sys.argv) < 3:
-    print "Usage: %s <config file> <output file>" % sys.argv[0]
+    print("Usage: %s <config file> <output file>" % sys.argv[0])
     sys.exit(0)
 
 if sys.argv[1] == "-d":