diff python/iso9660.py @ 287:1c7c1e619be8

File movage
author Windel Bouwman
date Thu, 21 Nov 2013 11:57:27 +0100
parents c9781c73e7e2
children
line wrap: on
line diff
--- a/python/iso9660.py	Fri Nov 15 13:52:32 2013 +0100
+++ b/python/iso9660.py	Thu Nov 21 11:57:27 2013 +0100
@@ -1,49 +1,82 @@
+#!/usr/bin/env python
 
 import argparse
 
-"""
+__doc__ = """
   ISO 9660 filesystem utility.
 """
 
-def read_vol_desc(f):
-    s = f.read(2048)
-    ty = s[0]
-    Id = s[1:6]
-    assert Id == 'CD001'.encode('ascii')
-    ver = s[6]
-    assert ver == 1
-    data = s[7:]
-    assert len(data) == 2041
-    return ty, Id, s
+
+class VolumeDescriptor:
+    @classmethod
+    def FromData(cls, d):
+        ty = d[0]
+        Id = d[1:6]
+        assert Id == 'CD001'.encode('ascii')
+        ver = d[6]
+        assert ver == 1
+        cls = vol_desc_types[ty]
+        return cls(d)
+
 
-def parse_boot_record(sec):
-    boot_sys_id = sec[7:39]
-    boot_id = sec[39:71]
-    print(boot_sys_id)
-    print(boot_id)
+vol_desc_types = {}
+def vol_type(t):
+    def reg_func(cls):
+        vol_desc_types[t] = cls
+        return cls
+    return reg_func
+
+
+@vol_type(0)
+class BootRecordVolumeDescriptor(VolumeDescriptor):
+    def __init__(self, d):
+        boot_sys_id = d[7:39]
+        boot_id = d[39:71]
+        print(boot_sys_id)
+        print(boot_id)
+
 
-def parse_primary_volume(sec):
-    sys_id = sec[8:40]
-    vol_id = sec[40:72]
-    print(sys_id)
-    print(vol_id)
+@vol_type(1)
+class PrimaryVolumeDescriptor(VolumeDescriptor):
+    def __init__(self, d):
+        sys_id = d[8:40]
+        vol_id = d[40:72]
+        print(sys_id)
+        print(vol_id)
+
+
+@vol_type(255)
+class VolumeDescriptorTerminator(VolumeDescriptor):
+    def __init__(self, d):
+        pass
+
 
-def read_iso(f):
-    # System area
-    system = f.read(16 * 2048)
-    while True:
-        ty, Id, dat = read_vol_desc(f)
-        print(ty, Id)
-        if ty == 255:
-            break
-        elif ty == 0:
-            parse_boot_record(dat)
-        elif ty == 1:
-            parse_primary_volume(dat)
+class ISOfs:
+    def __init__(self):
+        self.vol_descriptors = []
+
+    def read(self, f):
+        # System area:
+        self.system_area = f.read(16 * 2048)
+        while True:
+            d = f.read(2048)
+            desc = VolumeDescriptor.FromData(d)
+            self.vol_descriptors.append(desc)
+            if type(desc) is VolumeDescriptorTerminator:
+                break
+
+    def dump(self):
+        for vd in self.vol_descriptors:
+            print(vd)
 
 
 if __name__ == '__main__':
-    with open('mikeos.iso', 'rb') as f:
-        read_iso(f)
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('filename')
+    args = parser.parse_args()
+    fs = ISOfs()
+    with open(args.filename, 'rb') as f:
+        fs.read(f)
+    fs.dump()