genlink.py: refactor to expose some kind of device info API
Reviewed-by: Karl Palsson <karlp@tweak.au>
This commit is contained in:
committed by
Karl Palsson
parent
1f3abd4376
commit
0817f9175e
@@ -23,103 +23,162 @@ import sys
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
|
||||||
if len(sys.argv) != 4:
|
|
||||||
print("usage: %s <device-data-file> <device> <mode>" % sys.argv[0], file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
data_file_path = sys.argv[1]
|
def get_device_data(data_file_path, device_id):
|
||||||
find = sys.argv[2].lower()
|
device = {
|
||||||
mode = sys.argv[3].upper()
|
'info': {},
|
||||||
|
'defs': [],
|
||||||
|
'family': [],
|
||||||
|
}
|
||||||
|
|
||||||
device = {
|
# open device data file
|
||||||
'info': {},
|
with open(data_file_path, 'r') as data_file:
|
||||||
'defs': [],
|
# iterate lines
|
||||||
'family': [],
|
for line in data_file:
|
||||||
}
|
# strip whitespace from the beginning and end of line
|
||||||
|
line = line.strip()
|
||||||
|
|
||||||
# open device data file
|
# skip empty lines and comments
|
||||||
with open(data_file_path, 'r') as data_file:
|
if line == '' or line.startswith('#'):
|
||||||
# iterate lines
|
|
||||||
for line in data_file:
|
|
||||||
# strip whitespace from the beginning and end of line
|
|
||||||
line = line.strip()
|
|
||||||
|
|
||||||
# skip empty lines and comments
|
|
||||||
if line == '' or line.startswith('#'):
|
|
||||||
continue
|
|
||||||
|
|
||||||
# split line into it's parts: <pattern> <parent> <data..>
|
|
||||||
parts = line.split()
|
|
||||||
pattern, parent, data = parts[0], parts[1], parts[2:]
|
|
||||||
|
|
||||||
# skip line if pattern did not match first element
|
|
||||||
if not fnmatch.fnmatch(find, pattern):
|
|
||||||
continue
|
|
||||||
|
|
||||||
# extract data
|
|
||||||
for d in data:
|
|
||||||
# split into K=V
|
|
||||||
try:
|
|
||||||
(k, v) = d.split('=')
|
|
||||||
except:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# skip invalid datas
|
# split line into it's parts: <pattern> <parent> <data..>
|
||||||
if not re.match('^[A-Z0-9_]+$', k):
|
parts = line.split()
|
||||||
|
pattern, parent, data = parts[0], parts[1], parts[2:]
|
||||||
|
|
||||||
|
# skip line if pattern did not match first element
|
||||||
|
if not fnmatch.fnmatch(device_id, pattern):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# add FPU and CPU to info, not defs
|
# extract data
|
||||||
if k in ('FPU', 'CPU'):
|
for d in data:
|
||||||
device['info'][k.lower()] = v
|
# split into K=V
|
||||||
|
try:
|
||||||
|
(k, v) = d.split('=')
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# skip invalid datas
|
||||||
|
if not re.match('^[A-Z0-9_]+$', k):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# add FPU and CPU to info, not defs
|
||||||
|
if k in ('FPU', 'CPU'):
|
||||||
|
device['info'][k.lower()] = v
|
||||||
|
continue
|
||||||
|
|
||||||
|
device['defs'].append((k, v))
|
||||||
|
|
||||||
|
# if parent is +, there's more data for this pattern
|
||||||
|
if parent == '+':
|
||||||
continue
|
continue
|
||||||
|
|
||||||
device['defs'].append((k, v))
|
# device family
|
||||||
|
device['family'].append(device_id)
|
||||||
|
|
||||||
# if parent is +, there's more data for this pattern
|
# break if this was the last line in this chain
|
||||||
if parent == '+':
|
if parent == 'END':
|
||||||
continue
|
break
|
||||||
|
|
||||||
# device family
|
# look for the parent
|
||||||
device['family'].append(find)
|
device_id = parent
|
||||||
|
|
||||||
# break if this was the last line in this chain
|
# reverse device list
|
||||||
if parent == 'END':
|
device['family'] = device['family'][::-1]
|
||||||
break
|
|
||||||
|
|
||||||
# look for the parent
|
# device was not found
|
||||||
find = parent
|
if len(device['family']) == 0:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# reverse device list
|
return device
|
||||||
device['family'] = device['family'][::-1]
|
|
||||||
|
|
||||||
# device was not found
|
|
||||||
if len(device['family']) == 0:
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# for CPPFLAGS and DEFS, define device family
|
def get_device_family_flags(data_file_path, device_id, device=None):
|
||||||
if mode in ('CPPFLAGS', 'DEFS'):
|
if not device:
|
||||||
sys.stdout.write(' '.join('-D%s' % d.upper() for d in device['family']))
|
device = get_device_data(data_file_path, device_id)
|
||||||
|
|
||||||
# defines
|
return ' '.join('-D%s' % d.upper() for d in device['family'])
|
||||||
if mode == 'DEFS':
|
|
||||||
if len(device['defs']) > 0:
|
|
||||||
defs = ' '.join('-D_%s=%s' % d for d in device['defs'])
|
def get_device_defines(data_file_path, device_id, device=None):
|
||||||
sys.stdout.write(' ' + defs)
|
if not device:
|
||||||
|
device = get_device_data(data_file_path, device_id)
|
||||||
|
|
||||||
|
defs = ' '.join('-D_%s=%s' % d for d in device['defs'])
|
||||||
|
return defs
|
||||||
|
|
||||||
|
|
||||||
|
def get_device_family(data_file_path, device_id, device=None):
|
||||||
|
if not device:
|
||||||
|
device = get_device_data(data_file_path, device_id)
|
||||||
|
|
||||||
# device family
|
|
||||||
elif mode == 'FAMILY':
|
|
||||||
if len(device['family']) > 0:
|
if len(device['family']) > 0:
|
||||||
sys.stdout.write(device['family'][0])
|
return device['family'][0]
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
def get_device_subfamily(data_file_path, device_id, device=None):
|
||||||
|
if not device:
|
||||||
|
device = get_device_data(data_file_path, device_id)
|
||||||
|
|
||||||
# device subfamily
|
|
||||||
elif mode == 'SUBFAMILY':
|
|
||||||
if len(device['family']) > 1:
|
if len(device['family']) > 1:
|
||||||
sys.stdout.write(device['family'][1])
|
return device['family'][1]
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
def get_device_info(data_file_path, device_id, info, device=None):
|
||||||
|
if not device:
|
||||||
|
device = get_device_data(data_file_path, device_id)
|
||||||
|
|
||||||
|
info = info.lower()
|
||||||
|
|
||||||
# device info
|
|
||||||
else:
|
|
||||||
info = mode.lower()
|
|
||||||
if info in device['info']:
|
if info in device['info']:
|
||||||
sys.stdout.write(device['info'][info])
|
return device['info'][info]
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) != 4:
|
||||||
|
print("usage: %s <device-data-file> <device> <mode>" % sys.argv[0],
|
||||||
|
file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
data_file_path = sys.argv[1]
|
||||||
|
find = sys.argv[2].lower()
|
||||||
|
mode = sys.argv[3].upper()
|
||||||
|
device = get_device_data(data_file_path, find)
|
||||||
|
|
||||||
|
# for CPPFLAGS and DEFS, define device family
|
||||||
|
if mode in ('CPPFLAGS', 'DEFS'):
|
||||||
|
sys.stdout.write(
|
||||||
|
get_device_family_flags(data_file_path, find, device=device))
|
||||||
|
|
||||||
|
# defines
|
||||||
|
if mode == 'DEFS':
|
||||||
|
defs = get_device_defines(data_file_path, find, device=device)
|
||||||
|
if defs:
|
||||||
|
sys.stdout.write(' ' + defs)
|
||||||
|
|
||||||
|
# device family
|
||||||
|
elif mode == 'FAMILY':
|
||||||
|
sys.stdout.write(get_device_family(data_file_path, find,
|
||||||
|
device=device))
|
||||||
|
|
||||||
|
# device subfamily
|
||||||
|
elif mode == 'SUBFAMILY':
|
||||||
|
sys.stdout.write(
|
||||||
|
get_device_subfamily(data_file_path, find, device=device))
|
||||||
|
|
||||||
|
# device info
|
||||||
|
else:
|
||||||
|
sys.stdout.write(
|
||||||
|
get_device_info(data_file_path, find, mode, device=device))
|
||||||
|
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user