forked from Turbo87/cabwiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfReader.py
134 lines (103 loc) · 4.39 KB
/
InfReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class InfReader:
def __init__(self):
self.__data = {}
def read_file(self, file):
section = ''
for line in file:
pos = line.find(';')
if pos != -1:
line = line[:pos]
line = line.strip()
if line == "":
continue
if line.startswith('[') and line.endswith(']'):
section = line.lstrip('[').rstrip(']')
self.__data[section] = []
elif section != '':
self.__data[section].append(line)
if not self.__check_sections():
return False
self.__apply_replacements()
self.__array_to_dict()
return True
def read(self, path):
with open(path, 'r') as file:
return self.read_file(file)
def __check_section(self, section):
if self.has_section(section):
return True
print 'Error: Section "' + section + '" is missing'
return False
def __check_sections(self):
return (self.__check_section('Version') and
self.__check_section('CEStrings') and
self.__check_section('DefaultInstall') and
self.__check_section('DestinationDirs'))
def raw(self):
return self.__data
def has_section(self, section):
return section in self.__data
def get_section(self, section):
return self.__data[section]
def __read_replacements_section(self, section):
replacements = []
for line in self.get_section(section):
line = line.split('=')
if len(line) < 2:
continue
key = line[0].strip()
value = line[1].strip()
replacements.append([key, value])
return replacements
def __read_replacements(self):
# Read replacements from CEStrings section
replacements = self.__read_replacements_section('CEStrings')
# Read replacements from Strings section if it exists
if self.has_section('Strings'):
replacements.extend(self.__read_replacements_section('Strings'))
# Return replacements array
return replacements
def __apply_replacement(self, replacement):
new_sections = {}
for section, content in self.__data.iteritems():
new_content = []
for line in content:
new_content.append(line.replace('%' + replacement[0] + '%', replacement[1]))
new_sections[section] = new_content
self.__data = new_sections
def __apply_replacements(self):
# Read replacement sections from INF file
replacements = self.__read_replacements()
# Apply replacements to themselves until none left
found = True
while found:
found = False
for replacement in replacements:
for i in range(len(replacements)):
if replacement[0] in replacements[i][1]:
found = True
replacements[i][1] = replacements[i][1].replace('%' + replacement[0] + '%', replacement[1])
# Apply replacements to the other sections
for replacement in replacements:
self.__apply_replacement(replacement)
def __array_section_to_dict(self, section, content):
dict = {}
for line in content:
line = line.split('=')
if len(line) < 2:
continue
key = line[0].strip()
value = line[1].strip()
dict[key] = value
self.__data[section] = dict
def __array_to_dict(self):
for section, content in self.__data.iteritems():
if (section.startswith('Version') or
section.startswith('CEStrings') or
section.startswith('Strings') or
section.startswith('CEDevice') or
section.startswith('SourceDisksNames') or
section.startswith('SourceDisksFiles') or
section.startswith('DefaultInstall') or
section.startswith('DestinationDirs')):
self.__array_section_to_dict(section, content)