remove boxid from cfg file name

This commit is contained in:
2025-04-08 14:50:50 +02:00
parent f28b1bcabd
commit 0b3dacfb32
27 changed files with 189 additions and 52 deletions

View File

@ -24,49 +24,102 @@ class UndefinedConfigFile(Exception):
"""config file not found or ambiguous"""
def convert_cfg(file, macaddr=None):
hostname, boxid = file.stem.split('_')
if macaddr is None:
macaddr = f'--:--:--:{boxid[0:2]}:{boxid[2:4]}:{boxid[4:6]}'
box = BoxInfo(macaddr, True)
config = box.read_config()
network = config.get('NETWORK', {})
if len(network) > 2:
mac1 = '00:0d:b9'
if config.get('DISPLAY'):
typ = 'control-box'
else:
typ = 'bare-apu'
elif len(network) == 2:
mac1 = 'd8:3a:dd'
typ = 'dual-eth-rpi'
else:
if config.get('BOX', {}).get('type') == 'ionopi':
mac1 = 'e4:5f:01'
typ = 'ionopi'
else:
mac1 = 'b8:27:eb'
typ = 'ionopimax'
macaddr = macaddr.replace('--:--:--', mac1)
with open(file) as f:
content = f.read()
newfile = file.parent / f'{hostname}.cfg'
with open(newfile, 'w') as f:
f.write(f'[BOX]\ntype={typ}\nMAC={macaddr}\n\n{content}')
if not file.name.endswith('_'):
file.rename(file.parent / (file.name + '_'))
return newfile
def convert_all():
for file in Path('cfg').glob('*_*.cfg*'):
convert_cfg(file)
class BoxInfo:
TOOLS = Path('/home/l_samenv/boxtools')
CFGPATH = str(TOOLS / 'cfg' / '%s_%06x.cfg')
CFGDIR = TOOLS / 'cfg'
BOX_TYPES = {
'00:0d:b9': 'apu', # bare apu or control box
'b8:27:eb': 'cm3', # guess iono pi max
'e4:5f:01': 'ionopi', # guess iono pi
'e4:5f:01': 'ionopi', # guess iono pi
'd8:3a:dd': 'cm4', # guess dual-eth-rpi
}
def __init__(self):
def __init__(self, macaddr=None, relcfg=None):
if relcfg:
self.CFGDIR = Path('cfg')
self.id = None
self.typ = None
self.hostname = socket.gethostname()
self.change_if_names = False
self.cfgfile = None
self.config = None
self.macaddr = {}
self.network_interfaces = {}
self.main_if = None
for ifdev in sorted(glob('/sys/class/net/*/address')):
ifname = ifdev.split('/')[-2]
if ifname == 'lo': # do not consider loopback interface
continue
if ifname.startswith('enp'):
self.change_if_names = True
ifname = f'eth{int(ifname[3]) - 1}'
with open(ifdev) as f:
self.macaddr[ifname] = addr = f.read().strip().lower()
if ifname in ('eth0', 'enp1s0'):
self.id = int(''.join(addr.split(':')[-3:]), 16) & 0xffffff
self.typ = self.BOX_TYPES.get(addr[:8])
self.main_if = ifname
self.oldcfg = False
if macaddr is None:
for ifdev in sorted(glob('/sys/class/net/*/address')):
ifname = ifdev.split('/')[-2]
if ifname == 'lo': # do not consider loopback interface
continue
if ifname.startswith('enp'):
self.change_if_names = True
ifname = f'eth{int(ifname[3]) - 1}'
with open(ifdev) as f:
self.network_interfaces[ifname] = addr = f.read().strip().lower()
if ifname in ('eth0', 'enp1s0'):
print('my mac address', addr)
macaddr = addr
self.main_if = ifname
self.macaddr = macaddr
if macaddr:
self.id = int(''.join(macaddr.split(':')[-3:]), 16) & 0xffffff
self.typ = self.BOX_TYPES.get(macaddr[:8])
self.hwtype = self.typ # this is one of the values in BOX_TYPE and will not change
def get_macaddr(self):
return self.macaddr.get(self.main_if)
def read_config(self, section=None):
cfgfiles = glob(self.CFGPATH % ('*', self.id))
cfgfiles = []
for file in self.CFGDIR.glob('*.cfg'):
with open(file) as f:
for line in f:
if line.startswith(f'MAC={self.macaddr}'):
cfgfiles.append(file)
break
if not cfgfiles:
cfgfiles = list(self.CFGDIR.glob(f'*_{self.id:06x}.cfg*'))
self.oldcfg = True
if len(cfgfiles) > 1:
raise AmbiguousConfigFile('ambiguous cfgfile: %r' % cfgfiles)
if section and not cfgfiles:
raise UndefinedConfigFile('no cfg file found for %s' % self.id)
raise UndefinedConfigFile('no cfg file found for %s' % self.macaddr)
if cfgfiles:
self.cfgfile = cfgfiles[0]
else:
@ -75,7 +128,7 @@ class BoxInfo:
parser.read(self.cfgfile)
if section:
if section in parser.sections():
return dict(parser[section])
return dict(parser[section])
return None
return {k: dict(parser[k]) for k in parser.sections()}