| 1 |
import re |
| 2 |
|
| 3 |
|
| 4 |
class FirmwareFile(object): |
| 5 |
def __init__(self, binary, desc=None, source=None, version=None) -> None: |
| 6 |
self.binary = binary |
| 7 |
self.desc = desc |
| 8 |
self.source = source |
| 9 |
self.version = version |
| 10 |
|
| 11 |
|
| 12 |
class FirmwareGroup(object): |
| 13 |
def __init__(self, driver, files, licence) -> None: |
| 14 |
self.driver = driver |
| 15 |
self.files = files |
| 16 |
self.licence = licence |
| 17 |
|
| 18 |
|
| 19 |
class FirmwareWhence(list): |
| 20 |
def __init__(self, file) -> None: |
| 21 |
self.read(file) |
| 22 |
|
| 23 |
@staticmethod |
| 24 |
def _unquote(name): |
| 25 |
if len(name) >= 3 and name[0] == '"' and name[-1] == '"': |
| 26 |
name = name[1:-1] |
| 27 |
return name |
| 28 |
|
| 29 |
def read(self, file) -> None: |
| 30 |
in_header = True |
| 31 |
driver = None |
| 32 |
files = {} |
| 33 |
licence = None |
| 34 |
binary = [] |
| 35 |
desc = None |
| 36 |
source = [] |
| 37 |
version = None |
| 38 |
|
| 39 |
for line in file: |
| 40 |
if line.startswith('----------'): |
| 41 |
if in_header: |
| 42 |
in_header = False |
| 43 |
else: |
| 44 |
# Finish old group |
| 45 |
if driver and files: |
| 46 |
self.append(FirmwareGroup(driver, files, licence)) |
| 47 |
driver = None |
| 48 |
files = {} |
| 49 |
licence = None |
| 50 |
continue |
| 51 |
|
| 52 |
if in_header: |
| 53 |
continue |
| 54 |
|
| 55 |
if line == '\n': |
| 56 |
# End of field; end of file fields |
| 57 |
for b in binary: |
| 58 |
# XXX The WHENCE file isn't yet consistent in its |
| 59 |
# association of binaries and their sources and |
| 60 |
# metadata. This associates all sources and |
| 61 |
# metadata in a group with each binary. |
| 62 |
files[b] = FirmwareFile(b, desc, source, version) |
| 63 |
binary = [] |
| 64 |
desc = None |
| 65 |
source = [] |
| 66 |
version = None |
| 67 |
continue |
| 68 |
|
| 69 |
match = re.match( |
| 70 |
r'(Driver|(?:Raw)?File|Info|Licen[cs]e|Source|Version' |
| 71 |
r'|Original licen[cs]e info(?:rmation)?):\s*(.*)\n', |
| 72 |
line) |
| 73 |
if match: |
| 74 |
# If we've seen a license for the previous group, |
| 75 |
# start a new group |
| 76 |
if licence: |
| 77 |
self.append(FirmwareGroup(driver, files, licence)) |
| 78 |
files = {} |
| 79 |
licence = None |
| 80 |
keyword, value = match.group(1, 2) |
| 81 |
if keyword == 'Driver': |
| 82 |
driver = value.split(' ')[0].lower() |
| 83 |
elif keyword in ['File', 'RawFile']: |
| 84 |
match = re.match(r'("[^"\n]+"|\S+)(?:\s+--\s+(.*))?', value) |
| 85 |
binary.append(self._unquote(match.group(1))) |
| 86 |
desc = match.group(2) |
| 87 |
elif keyword in ['Info', 'Version']: |
| 88 |
version = value |
| 89 |
elif keyword == 'Source': |
| 90 |
source.append(self._unquote(value)) |
| 91 |
else: |
| 92 |
licence = value |
| 93 |
elif licence is not None: |
| 94 |
licence = (licence + '\n' |
| 95 |
+ re.sub(r'^(?:[/ ]\*| \*/)?\s*(.*?)\s*$', r'\1', |
| 96 |
line)) |
| 97 |
|
| 98 |
# Finish last group if non-empty |
| 99 |
for b in binary: |
| 100 |
files[b] = FirmwareFile(b, desc, source, version) |
| 101 |
if driver: |
| 102 |
self.append(FirmwareGroup(driver, files, licence)) |