comparison python/iso9660.py @ 287:1c7c1e619be8

File movage
author Windel Bouwman
date Thu, 21 Nov 2013 11:57:27 +0100
parents c9781c73e7e2
children
comparison
equal deleted inserted replaced
286:d9df72971cbf 287:1c7c1e619be8
1 #!/usr/bin/env python
1 2
2 import argparse 3 import argparse
3 4
4 """ 5 __doc__ = """
5 ISO 9660 filesystem utility. 6 ISO 9660 filesystem utility.
6 """ 7 """
7 8
8 def read_vol_desc(f):
9 s = f.read(2048)
10 ty = s[0]
11 Id = s[1:6]
12 assert Id == 'CD001'.encode('ascii')
13 ver = s[6]
14 assert ver == 1
15 data = s[7:]
16 assert len(data) == 2041
17 return ty, Id, s
18 9
19 def parse_boot_record(sec): 10 class VolumeDescriptor:
20 boot_sys_id = sec[7:39] 11 @classmethod
21 boot_id = sec[39:71] 12 def FromData(cls, d):
22 print(boot_sys_id) 13 ty = d[0]
23 print(boot_id) 14 Id = d[1:6]
15 assert Id == 'CD001'.encode('ascii')
16 ver = d[6]
17 assert ver == 1
18 cls = vol_desc_types[ty]
19 return cls(d)
24 20
25 def parse_primary_volume(sec):
26 sys_id = sec[8:40]
27 vol_id = sec[40:72]
28 print(sys_id)
29 print(vol_id)
30 21
31 def read_iso(f): 22 vol_desc_types = {}
32 # System area 23 def vol_type(t):
33 system = f.read(16 * 2048) 24 def reg_func(cls):
34 while True: 25 vol_desc_types[t] = cls
35 ty, Id, dat = read_vol_desc(f) 26 return cls
36 print(ty, Id) 27 return reg_func
37 if ty == 255: 28
38 break 29
39 elif ty == 0: 30 @vol_type(0)
40 parse_boot_record(dat) 31 class BootRecordVolumeDescriptor(VolumeDescriptor):
41 elif ty == 1: 32 def __init__(self, d):
42 parse_primary_volume(dat) 33 boot_sys_id = d[7:39]
34 boot_id = d[39:71]
35 print(boot_sys_id)
36 print(boot_id)
37
38
39 @vol_type(1)
40 class PrimaryVolumeDescriptor(VolumeDescriptor):
41 def __init__(self, d):
42 sys_id = d[8:40]
43 vol_id = d[40:72]
44 print(sys_id)
45 print(vol_id)
46
47
48 @vol_type(255)
49 class VolumeDescriptorTerminator(VolumeDescriptor):
50 def __init__(self, d):
51 pass
52
53
54 class ISOfs:
55 def __init__(self):
56 self.vol_descriptors = []
57
58 def read(self, f):
59 # System area:
60 self.system_area = f.read(16 * 2048)
61 while True:
62 d = f.read(2048)
63 desc = VolumeDescriptor.FromData(d)
64 self.vol_descriptors.append(desc)
65 if type(desc) is VolumeDescriptorTerminator:
66 break
67
68 def dump(self):
69 for vd in self.vol_descriptors:
70 print(vd)
43 71
44 72
45 if __name__ == '__main__': 73 if __name__ == '__main__':
46 with open('mikeos.iso', 'rb') as f: 74 parser = argparse.ArgumentParser(description=__doc__)
47 read_iso(f) 75 parser.add_argument('filename')
76 args = parser.parse_args()
77 fs = ISOfs()
78 with open(args.filename, 'rb') as f:
79 fs.read(f)
80 fs.dump()
48 81
49 82