servo_updater: Do not raise ServoUpdaterException on module import
External tools such as platform/dev/contrib/dut-console may import servo_updater as a module. On import time, this module will check the existence of FIRMWARE_PATH and potentially raise ServoUpdaterException, which is unnecessary if the function the importing program calls does not have reference to the global variable FIRMWARE_PATH (for example, find_available_version()). In addition, there is only one function, findfiles(), that is currently using FIRMWARE_PATH and other similar global variables. Therefore, move the check for path existence inside findfiles(), so that ServoUpdaterException will not be raised on module import time. Also add servo board name constants to servo_updater to reduce duplicate string literals. BRANCH=none BUG=chromium:1061354 TEST=Run 'sudo servod -b kukui -m jacuzzi' and make sure findfiles() is called without exceptions Cq-Depend: chromium:2102179 Change-Id: I0179c44ba2a3947cb26035d97cd1ebeb453ef67a Signed-off-by: Yu-Ping Wu <yupingso@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2102247 Reviewed-by: Wai-Hong Tam <waihong@google.com>
This commit is contained in:
parent
4d032c74e0
commit
0b7596910e
|
@ -21,6 +21,11 @@ import ecusb.tiny_servo_common as c
|
|||
class ServoUpdaterException(Exception):
|
||||
"""Raised on exceptions generated by servo_updater."""
|
||||
|
||||
BOARD_C2D2 = "c2d2"
|
||||
BOARD_SERVO_MICRO = "servo_micro"
|
||||
BOARD_SERVO_V4 = "servo_v4"
|
||||
BOARD_SWEETBERRY = "sweetberry"
|
||||
SERVO_BOARDS = (BOARD_C2D2, BOARD_SERVO_MICRO, BOARD_SERVO_V4, BOARD_SWEETBERRY)
|
||||
|
||||
DEFAULT_BASE_PATH = '/usr/'
|
||||
TEST_IMAGE_BASE_PATH = '/usr/local/'
|
||||
|
@ -30,20 +35,6 @@ COMMON_PATH = 'share/servo_updater'
|
|||
FIRMWARE_DIR = "firmware/"
|
||||
CONFIGS_DIR = "configs/"
|
||||
|
||||
if os.path.exists(os.path.join(DEFAULT_BASE_PATH, COMMON_PATH)):
|
||||
BASE_PATH = DEFAULT_BASE_PATH
|
||||
elif os.path.exists(os.path.join(TEST_IMAGE_BASE_PATH, COMMON_PATH)):
|
||||
BASE_PATH = TEST_IMAGE_BASE_PATH
|
||||
else:
|
||||
raise ServoUpdaterException('servo_updater/ dir not found in known spots.')
|
||||
|
||||
FIRMWARE_PATH = os.path.join(BASE_PATH, COMMON_PATH, FIRMWARE_DIR)
|
||||
CONFIGS_PATH = os.path.join(BASE_PATH, COMMON_PATH, CONFIGS_DIR)
|
||||
|
||||
for p in [FIRMWARE_PATH, CONFIGS_PATH]:
|
||||
if not os.path.exists(p):
|
||||
raise ServoUpdaterException('Could not find required path %r' % p)
|
||||
|
||||
def flash(brdfile, serialno, binfile):
|
||||
"""Call fw_update to upload to updater USB endpoint."""
|
||||
p = fw_update.Supdate()
|
||||
|
@ -186,9 +177,23 @@ def findfiles(cname, fname):
|
|||
Returns:
|
||||
cname, fname: validated filenames selected from the path.
|
||||
"""
|
||||
for p in (DEFAULT_BASE_PATH, TEST_IMAGE_BASE_PATH):
|
||||
updater_path = os.path.join(p, COMMON_PATH)
|
||||
if os.path.exists(updater_path):
|
||||
break
|
||||
else:
|
||||
raise ServoUpdaterException('servo_updater/ dir not found in known spots.')
|
||||
|
||||
firmware_path = os.path.join(updater_path, FIRMWARE_DIR)
|
||||
configs_path = os.path.join(updater_path, CONFIGS_DIR)
|
||||
|
||||
for p in (firmware_path, configs_path):
|
||||
if not os.path.exists(p):
|
||||
raise ServoUpdaterException('Could not find required path %r' % p)
|
||||
|
||||
if not os.path.isfile(cname):
|
||||
# If not an existing file, try checking on the default path.
|
||||
newname = CONFIGS_PATH + cname
|
||||
newname = os.path.join(configs_path, cname)
|
||||
if os.path.isfile(newname):
|
||||
cname = newname
|
||||
elif os.path.isfile(newname + ".json"):
|
||||
|
@ -198,17 +203,20 @@ def findfiles(cname, fname):
|
|||
raise ServoUpdaterException("Can't find file: %s." % cname)
|
||||
|
||||
if not fname:
|
||||
# If None, try defaults.
|
||||
dev = None
|
||||
for default_f in ['c2d2', 'servo_micro', 'servo_v4', 'sweetberry']:
|
||||
if default_f in cname:
|
||||
dev = default_f
|
||||
if os.path.isfile(FIRMWARE_PATH + dev + ".bin"):
|
||||
fname = FIRMWARE_PATH + dev + ".bin"
|
||||
# If None, try to infer board name from config.
|
||||
for board in SERVO_BOARDS:
|
||||
if board in cname:
|
||||
newname = os.path.join(firmware_path, board + ".bin")
|
||||
if os.path.isfile(newname):
|
||||
fname = newname
|
||||
break
|
||||
else:
|
||||
raise ServoUpdaterException("Can't find firmware binary file")
|
||||
elif not os.path.isfile(fname):
|
||||
# If a name is specified but not found, try the default path.
|
||||
if os.path.isfile(FIRMWARE_PATH + fname):
|
||||
fname = FIRMWARE_PATH + fname
|
||||
newname = os.path.join(firmware_path, fname)
|
||||
if os.path.isfile(newname):
|
||||
fname = newname
|
||||
else:
|
||||
raise ServoUpdaterException("Can't find file: %s." % fname)
|
||||
|
||||
|
@ -239,7 +247,7 @@ def main():
|
|||
parser.add_argument('-s', '--serialno', type=str,
|
||||
help="serial number to program", default=None)
|
||||
parser.add_argument('-b', '--board', type=str,
|
||||
help="Board configuration json file", default="servo_v4")
|
||||
help="Board configuration json file", default=BOARD_SERVO_V4)
|
||||
parser.add_argument('-f', '--file', type=str,
|
||||
help="Complete ec.bin file", default=None)
|
||||
parser.add_argument('--force', action="store_true",
|
||||
|
|
Loading…
Reference in New Issue