mirror of
https://github.com/LibreELEC/LibreELEC.tv.git
synced 2025-07-28 13:16:41 +00:00
Merge pull request #4998 from lrusak/uboot-helper-argparse
uboot_helper: convert to use argparse
This commit is contained in:
commit
fb96fe278e
@ -3,7 +3,7 @@
|
|||||||
# SPDX-License-Identifier: GPL-2.0
|
# SPDX-License-Identifier: GPL-2.0
|
||||||
# Copyright (C) 2017-present Team LibreELEC (https://libreelec.tv)
|
# Copyright (C) 2017-present Team LibreELEC (https://libreelec.tv)
|
||||||
|
|
||||||
import sys
|
import argparse
|
||||||
|
|
||||||
# When adding new devices to the list please keep them in alphabetical order
|
# When adding new devices to the list please keep them in alphabetical order
|
||||||
# board-name should contain 'dashes' (-) not 'underscores' (_) and shouldn't contain capitals
|
# board-name should contain 'dashes' (-) not 'underscores' (_) and shouldn't contain capitals
|
||||||
@ -316,77 +316,69 @@ devices = \
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
def usage(PROJECT=None, SOC=None, FILE=sys.stdout):
|
class OptionsAction(argparse.Action):
|
||||||
print('Usage: %s <project> <soc> <board-name> dtb|config|crust_config' % sys.argv[0], file=FILE)
|
|
||||||
print(' %s <project> <soc>' % sys.argv[0], file=FILE)
|
|
||||||
print(' %s <project>' % sys.argv[0], file=FILE)
|
|
||||||
print('', file=FILE)
|
|
||||||
|
|
||||||
if PROJECT and PROJECT not in devices:
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
PROJECT = SOC = None
|
message = None
|
||||||
if PROJECT and SOC and SOC not in devices[PROJECT]:
|
|
||||||
SOC = None
|
|
||||||
|
|
||||||
print('Projects:', file=FILE)
|
if self.dest == 'project':
|
||||||
print('', file=FILE)
|
if values is None:
|
||||||
for project in sorted(devices):
|
print(' '.join(project for project in sorted(devices)))
|
||||||
if PROJECT is None or PROJECT == project:
|
parser.exit()
|
||||||
print(' %s:' % project, file=FILE)
|
|
||||||
for soc in sorted(devices[project]):
|
|
||||||
if SOC is None or SOC == soc:
|
|
||||||
print(' %s:' % soc, file=FILE)
|
|
||||||
for board in sorted(devices[project][soc]):
|
|
||||||
print(' %s' % board, file=FILE)
|
|
||||||
print('', file=FILE)
|
|
||||||
print('', file=FILE)
|
|
||||||
|
|
||||||
def exit_error(msg, PROJECT=None, SOC=None):
|
if values not in devices.keys():
|
||||||
print(msg, file=sys.stderr)
|
message = "invalid choice: {0!r} (choose from {1})".format(values, ', '.join([repr(project) for project in devices]))
|
||||||
print('', file=sys.stderr)
|
|
||||||
usage(PROJECT=PROJECT, SOC=SOC, FILE=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# Basic argument validation
|
if self.dest == 'soc':
|
||||||
if len(sys.argv) == 2 and sys.argv[1] in ['help', 'usage']:
|
project = getattr(namespace, 'project')
|
||||||
usage()
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
if len(sys.argv) > 1 and sys.argv[1] not in devices:
|
if values is None:
|
||||||
exit_error('Invalid project: %s' % sys.argv[1])
|
print(' '.join(soc for soc in sorted(devices[project])))
|
||||||
|
parser.exit()
|
||||||
|
|
||||||
if len(sys.argv) > 2 and sys.argv[2] not in devices[sys.argv[1]]:
|
if values not in devices[project].keys():
|
||||||
exit_error('Invalid soc: %s' % sys.argv[2], PROJECT=sys.argv[1])
|
message = "invalid choice: {0!r} (choose from {1})".format(values, ', '.join([repr(soc) for soc in devices[project]]))
|
||||||
|
|
||||||
if len(sys.argv) > 3 and sys.argv[3] not in devices[sys.argv[1]][sys.argv[2]]:
|
if self.dest == 'board':
|
||||||
exit_error('Invalid board-name: %s' % sys.argv[3], PROJECT=sys.argv[1], SOC=sys.argv[2])
|
project = getattr(namespace, 'project')
|
||||||
|
soc = getattr(namespace, 'soc')
|
||||||
|
|
||||||
if len(sys.argv) == 4:
|
if values is None:
|
||||||
exit_error('Invalid option: must specify dtb, config or crust_config', PROJECT=sys.argv[1], SOC=sys.argv[2])
|
print(' '.join(board for board in sorted(devices[project][soc])))
|
||||||
elif len(sys.argv) > 4 and sys.argv[4] not in ['dtb', 'config', 'crust_config']:
|
parser.exit()
|
||||||
exit_error('Invalid option: %s' % sys.argv[4], PROJECT=sys.argv[1], SOC=sys.argv[2])
|
|
||||||
|
|
||||||
if len(sys.argv) > 5:
|
if values not in devices[project][soc].keys():
|
||||||
exit_error('Invalid number of arguments: %s' % ' '.join(sys.argv[1:]), PROJECT=sys.argv[1], SOC=sys.argv[2])
|
message = "invalid choice: {0!r} (choose from {1})".format(values, ', '.join([repr(board) for board in devices[project][soc]]))
|
||||||
|
|
||||||
# Get dtb, u-boot or crust config for a given project, soc, and board
|
if self.dest == 'value':
|
||||||
# ./scripts/uboot_helper project device board-name dtb|config|crust_config
|
project = getattr(namespace, 'project')
|
||||||
if len(sys.argv) == 5:
|
soc = getattr(namespace, 'soc')
|
||||||
if sys.argv[4] in devices[sys.argv[1]][sys.argv[2]][sys.argv[3]]:
|
board = getattr(namespace, 'board')
|
||||||
print(devices[sys.argv[1]][sys.argv[2]][sys.argv[3]][sys.argv[4]])
|
|
||||||
|
|
||||||
# List boards supported by a given project and soc
|
if values is None:
|
||||||
# ./scripts/uboot_helper project device
|
print(' '.join(value for value in sorted(devices[project][soc][board].keys())))
|
||||||
elif len(sys.argv) == 3:
|
parser.exit()
|
||||||
print(' '.join([board for board in sorted(devices[sys.argv[1]][sys.argv[2]])]))
|
|
||||||
|
|
||||||
# List socs supported by a given project
|
if values not in devices[project][soc][board].keys():
|
||||||
# ./scripts/uboot_helper project
|
parser.exit()
|
||||||
elif len(sys.argv) == 2:
|
|
||||||
print(' '.join([soc for soc in sorted(devices[sys.argv[1]])]))
|
|
||||||
|
|
||||||
# List projects
|
if message is not None:
|
||||||
# ./scripts/uboot_helper
|
raise argparse.ArgumentError(self, message)
|
||||||
elif len(sys.argv) == 1:
|
|
||||||
print(' '.join([project for project in sorted(devices)]))
|
|
||||||
|
|
||||||
sys.exit(0)
|
setattr(namespace, self.dest, values)
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description='Script to help with u-boot configuration')
|
||||||
|
|
||||||
|
parser.add_argument('project', nargs='?', action=OptionsAction)
|
||||||
|
|
||||||
|
parser.add_argument('soc', nargs='?', action=OptionsAction)
|
||||||
|
|
||||||
|
parser.add_argument('board', nargs='?', action=OptionsAction)
|
||||||
|
|
||||||
|
parser.add_argument('value', nargs='?', action=OptionsAction)
|
||||||
|
|
||||||
|
options = parser.parse_args()
|
||||||
|
|
||||||
|
print(devices[options.project][options.soc][options.board][options.value])
|
||||||
|
|
||||||
|
parser.exit()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user