Commits

clach04 committed 36ba23b Draft

dos2unix

  • Participants
  • Parent commits f5f613f

Comments (0)

Files changed (1)

-#!/usr/bin/env python
-# -*- coding: us-ascii -*-
-# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
-#
-"""Pure Python version of Janes Longbow 2 detre tool.
-See Longbow 2: TRE extractor by Mario Brito
-"""
-
-import os
-import sys
-import struct
-
-
-# function that behaves like "mkdir -p" under Unix
-def mkdir_p(newdir):
-    """works the way a good mkdir should :)
-        - already exists, silently complete
-        - regular file in the way, raise an exception
-        - parent directory(ies) does not exist, make them as well
-    """
-    if os.path.isdir(newdir):
-        pass
-    elif os.path.isfile(newdir):
-        raise OSError("a file with the same name as the desired " \
-                      "dir, '%s', already exists." % newdir)
-    else:
-        try:
-            os.makedirs(newdir)
-        except OSError:
-            #except OSError, info:
-            info = sys.exc_info()[1]  # Handle Py 2.x and Py 3.x
-            if info.errno == 17 and os.path.isdir(newdir):
-                pass
-            else:
-                raise
-
-
-def b2ui4(buf):
-    """buffer to unsigned 4 byte int (little endian)
-    """
-    num_files = struct.unpack('<L', buf)[0]  # little endian
-    return num_files
-
-
-def read_ui4(f):
-    buf = f.read(4)
-    result = b2ui4(buf)
-    return result
-
-
-class TreFile(object):
-    def __init__(self, tre_filename):
-        f = open(tre_filename, 'rb')
-        
-        # Longbow 2 MOVIES2.TRE file from cd2
-        buf = f.read(4)
-        assert buf == 'SKNK', "doesn't appear to be a Longbow2 TRE file. Got %r" % buf
-        buf = f.read(4)
-        assert buf == 'TREE', "doesn't appear to be a Longbow2 TRE file. Got %r" % buf
-        buf = f.read(4)
-        assert buf == '010A', "wrong version, possibly not a Longbow2 TRE file. Got %r" % buf
-        
-        num_files = read_ui4(f)
-        print '%d files' % num_files
-        
-        header_size = read_ui4(f)
-        #print '%d' % header_size
-        
-        start_offset = []
-        for x in range(num_files):
-            start_offset.append(read_ui4(f))
-        
-        size1 = []
-        for x in range(num_files):
-            size1.append(read_ui4(f))
-        
-        size2 = []
-        for x in range(num_files):
-            size2.append(read_ui4(f))
-        
-        zero_buf = []
-        for x in range(num_files):
-            zero_buf.append(read_ui4(f))
-        
-        """
-        print start_offset
-        print size1
-        print size2
-        print zero_buf
-        """
-        
-        filenames = []
-        for x in range(num_files):
-            filename = []
-            while 1:
-                b = f.read(1)
-                if b == '\x00':
-                    break
-                else:
-                    filename.append(b)
-            filename = ''.join(filename)
-            filename_list = filename.split('\\')  # Windows/DOS seperator
-            if filename_list[0] == '..':
-                del(filename_list[0])
-            top_f = filename_list[0]
-            bot_f = filename_list[1:]
-            filename = os.path.join(top_f, *bot_f)
-            filenames.append(filename)
-        #print filenames
-        
-        f.close()
-        
-        self.tre_filename = tre_filename
-        self.start_offset = start_offset
-        self.size1 = size1
-        self.size2 = size2
-        self.zero_buf = zero_buf
-        self.filenames = filenames
-        self.num_files = num_files
-
-    def extract(self, file_num):
-        filename = self.filenames[file_num]
-        #filename = '/tmp/d.bin'  # DEBUG
-        print filename
-        if self.size1[file_num] != self.size2[file_num]:
-            print 'Warning: file may be compressed'
-        if self.zero_buf[file_num] != 0:
-            print 'Warning: unknown field is not zero'
-        
-        file_size = min(self.size1[file_num], self.size2[file_num])
-        print '\t%d bytes' % file_size
-        f = open(self.tre_filename, 'rb')
-        f.seek(self.start_offset[file_num])
-        
-        data = f.read(file_size)  # read the whole thing into RAM :-)
-        mkdir_p(os.path.dirname(filename))
-        f_out = open(filename, 'wb')
-        f_out.write(data)
-        
-        f_out.close()
-        f.close()
-
-    def extract_all(self):
-        for x in range(self.num_files):
-            self.extract(x)
-
-def doit(tre_filename):
-    t = TreFile(tre_filename)
-    #t.extract(0)  # first file
-    t.extract_all()
-
-
-def main(argv=None):
-    if argv is None:
-        argv = sys.argv
-    
-    tre_filename = argv[1]
-    #tre_filename = '/windows/lb2/MOVIES2.TRE'
-    doit(tre_filename)
-    
-    return 0
-
-
-if __name__ == "__main__":
-    sys.exit(main())
+#!/usr/bin/env python
+# -*- coding: us-ascii -*-
+# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
+#
+"""Pure Python version of Janes Longbow 2 detre tool.
+See Longbow 2: TRE extractor by Mario Brito
+"""
+
+import os
+import sys
+import struct
+
+
+# function that behaves like "mkdir -p" under Unix
+def mkdir_p(newdir):
+    """works the way a good mkdir should :)
+        - already exists, silently complete
+        - regular file in the way, raise an exception
+        - parent directory(ies) does not exist, make them as well
+    """
+    if os.path.isdir(newdir):
+        pass
+    elif os.path.isfile(newdir):
+        raise OSError("a file with the same name as the desired " \
+                      "dir, '%s', already exists." % newdir)
+    else:
+        try:
+            os.makedirs(newdir)
+        except OSError:
+            #except OSError, info:
+            info = sys.exc_info()[1]  # Handle Py 2.x and Py 3.x
+            if info.errno == 17 and os.path.isdir(newdir):
+                pass
+            else:
+                raise
+
+
+def b2ui4(buf):
+    """buffer to unsigned 4 byte int (little endian)
+    """
+    num_files = struct.unpack('<L', buf)[0]  # little endian
+    return num_files
+
+
+def read_ui4(f):
+    buf = f.read(4)
+    result = b2ui4(buf)
+    return result
+
+
+class TreFile(object):
+    def __init__(self, tre_filename):
+        f = open(tre_filename, 'rb')
+        
+        # Longbow 2 MOVIES2.TRE file from cd2
+        buf = f.read(4)
+        assert buf == 'SKNK', "doesn't appear to be a Longbow2 TRE file. Got %r" % buf
+        buf = f.read(4)
+        assert buf == 'TREE', "doesn't appear to be a Longbow2 TRE file. Got %r" % buf
+        buf = f.read(4)
+        assert buf == '010A', "wrong version, possibly not a Longbow2 TRE file. Got %r" % buf
+        
+        num_files = read_ui4(f)
+        print '%d files' % num_files
+        
+        header_size = read_ui4(f)
+        #print '%d' % header_size
+        
+        start_offset = []
+        for x in range(num_files):
+            start_offset.append(read_ui4(f))
+        
+        size1 = []
+        for x in range(num_files):
+            size1.append(read_ui4(f))
+        
+        size2 = []
+        for x in range(num_files):
+            size2.append(read_ui4(f))
+        
+        zero_buf = []
+        for x in range(num_files):
+            zero_buf.append(read_ui4(f))
+        
+        """
+        print start_offset
+        print size1
+        print size2
+        print zero_buf
+        """
+        
+        filenames = []
+        for x in range(num_files):
+            filename = []
+            while 1:
+                b = f.read(1)
+                if b == '\x00':
+                    break
+                else:
+                    filename.append(b)
+            filename = ''.join(filename)
+            filename_list = filename.split('\\')  # Windows/DOS seperator
+            if filename_list[0] == '..':
+                del(filename_list[0])
+            top_f = filename_list[0]
+            bot_f = filename_list[1:]
+            filename = os.path.join(top_f, *bot_f)
+            filenames.append(filename)
+        #print filenames
+        
+        f.close()
+        
+        self.tre_filename = tre_filename
+        self.start_offset = start_offset
+        self.size1 = size1
+        self.size2 = size2
+        self.zero_buf = zero_buf
+        self.filenames = filenames
+        self.num_files = num_files
+
+    def extract(self, file_num):
+        filename = self.filenames[file_num]
+        #filename = '/tmp/d.bin'  # DEBUG
+        print filename
+        if self.size1[file_num] != self.size2[file_num]:
+            print 'Warning: file may be compressed'
+        if self.zero_buf[file_num] != 0:
+            print 'Warning: unknown field is not zero'
+        
+        file_size = min(self.size1[file_num], self.size2[file_num])
+        print '\t%d bytes' % file_size
+        f = open(self.tre_filename, 'rb')
+        f.seek(self.start_offset[file_num])
+        
+        data = f.read(file_size)  # read the whole thing into RAM :-)
+        mkdir_p(os.path.dirname(filename))
+        f_out = open(filename, 'wb')
+        f_out.write(data)
+        
+        f_out.close()
+        f.close()
+
+    def extract_all(self):
+        for x in range(self.num_files):
+            self.extract(x)
+
+def doit(tre_filename):
+    t = TreFile(tre_filename)
+    #t.extract(0)  # first file
+    t.extract_all()
+
+
+def main(argv=None):
+    if argv is None:
+        argv = sys.argv
+    
+    tre_filename = argv[1]
+    #tre_filename = '/windows/lb2/MOVIES2.TRE'
+    doit(tre_filename)
+    
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())