Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts: west_commands: Remove deprecated west.log #79171

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 2 additions & 3 deletions doc/develop/west/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ details on the west APIs you can use, see :ref:`west-apis`.
from textwrap import dedent # just for nicer code indentation

from west.commands import WestCommand # your extension must subclass this
from west import log # use this for user output

class MyCommand(WestCommand):

Expand Down Expand Up @@ -125,8 +124,8 @@ details on the west APIs you can use, see :ref:`west-apis`.
# $ west my-command-name -o FOO BAR
# --optional is FOO
# required is BAR
log.inf('--optional is', args.optional)
log.inf('required is', args.required)
self.inf('--optional is', args.optional)
self.inf('required is', args.required)

You can ignore the second argument to ``do_run()`` (``unknown_args`` above), as
``WestCommand`` will reject unknown arguments by default. If you want to be
Expand Down
33 changes: 16 additions & 17 deletions scripts/west_commands/bindesc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import struct

from west.commands import WestCommand
from west import log


try:
Expand All @@ -18,7 +17,7 @@


# Based on scripts/build/uf2conv.py
def convert_from_uf2(buf):
def convert_from_uf2(cmd, buf):
UF2_MAGIC_START0 = 0x0A324655 # First magic number ('UF2\n')
UF2_MAGIC_START1 = 0x9E5D5157 # Second magic number
numblocks = len(buf) // 512
Expand All @@ -29,24 +28,24 @@ def convert_from_uf2(buf):
block = buf[ptr:ptr + 512]
hd = struct.unpack(b'<IIIIIIII', block[0:32])
if hd[0] != UF2_MAGIC_START0 or hd[1] != UF2_MAGIC_START1:
log.inf('Skipping block at ' + ptr + '; bad magic')
cmd.inf('Skipping block at ' + ptr + '; bad magic')
continue
if hd[2] & 1:
# NO-flash flag set; skip block
continue
datalen = hd[4]
if datalen > 476:
log.die(f'Invalid UF2 data size at {ptr}')
cmd.die(f'Invalid UF2 data size at {ptr}')
newaddr = hd[3]
if curraddr is None:
curraddr = newaddr
padding = newaddr - curraddr
if padding < 0:
log.die(f'Block out of order at {ptr}')
cmd.die(f'Block out of order at {ptr}')
if padding > 10*1024*1024:
log.die(f'More than 10M of padding needed at {ptr}')
cmd.die(f'More than 10M of padding needed at {ptr}')
if padding % 4 != 0:
log.die(f'Non-word padding size at {ptr}')
cmd.die(f'Non-word padding size at {ptr}')
while padding > 0:
padding -= 4
outp += b'\x00\x00\x00\x00'
Expand Down Expand Up @@ -154,11 +153,11 @@ def dump(self, args):
for tag, value in descriptors.items():
if tag in self.TAG_TO_NAME:
tag = self.TAG_TO_NAME[tag]
log.inf(f'{tag}', self.bindesc_repr(value))
self.inf(f'{tag}', self.bindesc_repr(value))

def list(self, args):
for tag in self.TAG_TO_NAME.values():
log.inf(f'{tag}')
self.inf(f'{tag}')

def common_search(self, args, search_term):
image = self.get_image_data(args.file)
Expand All @@ -167,15 +166,15 @@ def common_search(self, args, search_term):

if search_term in descriptors:
value = descriptors[search_term]
log.inf(self.bindesc_repr(value))
self.inf(self.bindesc_repr(value))
else:
log.die('Descriptor not found')
self.die('Descriptor not found')

def search(self, args):
try:
search_term = self.NAME_TO_TAG[args.descriptor]
except KeyError:
log.die(f'Descriptor {args.descriptor} is invalid')
self.die(f'Descriptor {args.descriptor} is invalid')

self.common_search(args, search_term)

Expand Down Expand Up @@ -208,7 +207,7 @@ def get_image_data(self, file_name):

if self.file_type == 'uf2':
with open(file_name, 'rb') as uf2_file:
return convert_from_uf2(uf2_file.read())
return convert_from_uf2(self, uf2_file.read())

if self.file_type == 'elf':
with open(file_name, 'rb') as f:
Expand All @@ -222,15 +221,15 @@ def get_image_data(self, file_name):
if section:
return section.data()

log.die('No "rom_start" or "text" section found')
self.die('No "rom_start" or "text" section found')

log.die('Unknown file type')
self.die('Unknown file type')

def parse_descriptors(self, image):
magic = struct.pack('>Q' if self.is_big_endian else 'Q', self.MAGIC)
index = image.find(magic)
if index == -1:
log.die('Could not find binary descriptor magic')
self.die('Could not find binary descriptor magic')

descriptors = {}

Expand All @@ -250,7 +249,7 @@ def parse_descriptors(self, image):
elif tag_type == self.TYPE_BYTES:
decoded_data = data
else:
log.die(f'Unknown type for tag 0x{current_tag:04x}')
self.die(f'Unknown type for tag 0x{current_tag:04x}')

key = f'0x{current_tag:04x}'
descriptors[key] = decoded_data
Expand Down
25 changes: 12 additions & 13 deletions scripts/west_commands/blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import textwrap
from urllib.parse import urlparse

from west import log
from west.commands import WestCommand

from zephyr_ext_common import ZEPHYR_BASE
Expand Down Expand Up @@ -87,7 +86,7 @@ def get_blobs(self, args):
unknown = set(modules) - set(all_names)

if len(unknown):
log.die(f'Unknown module(s): {unknown}')
self.die(f'Unknown module(s): {unknown}')

for module in all_modules:
# Filter by module
Expand All @@ -103,30 +102,30 @@ def list(self, args):
blobs = self.get_blobs(args)
fmt = args.format or self.DEFAULT_LIST_FMT
for blob in blobs:
log.inf(fmt.format(**blob))
self.inf(fmt.format(**blob))

def ensure_folder(self, path):
path.parent.mkdir(parents=True, exist_ok=True)

def fetch_blob(self, url, path):
scheme = urlparse(url).scheme
log.dbg(f'Fetching {path} with {scheme}')
self.dbg(f'Fetching {path} with {scheme}')
import fetchers
fetcher = fetchers.get_fetcher_cls(scheme)

log.dbg(f'Found fetcher: {fetcher}')
self.dbg(f'Found fetcher: {fetcher}')
inst = fetcher()
self.ensure_folder(path)
inst.fetch(url, path)

# Compare the checksum of a file we've just downloaded
# to the digest in blob metadata, warn user if they differ.
def verify_blob(self, blob):
log.dbg('Verifying blob {module}: {abspath}'.format(**blob))
self.dbg('Verifying blob {module}: {abspath}'.format(**blob))

status = zephyr_module.get_blob_status(blob['abspath'], blob['sha256'])
if status == zephyr_module.BLOB_OUTDATED:
log.err(textwrap.dedent(
self.err(textwrap.dedent(
f'''\
The checksum of the downloaded file does not match that
in the blob metadata:
Expand All @@ -146,27 +145,27 @@ def fetch(self, args):
blobs = self.get_blobs(args)
for blob in blobs:
if blob['status'] == zephyr_module.BLOB_PRESENT:
log.dbg('Blob {module}: {abspath} is up to date'.format(**blob))
self.dbg('Blob {module}: {abspath} is up to date'.format(**blob))
continue
log.inf('Fetching blob {module}: {abspath}'.format(**blob))
self.inf('Fetching blob {module}: {abspath}'.format(**blob))
self.fetch_blob(blob['url'], blob['abspath'])
self.verify_blob(blob)

def clean(self, args):
blobs = self.get_blobs(args)
for blob in blobs:
if blob['status'] == zephyr_module.BLOB_NOT_PRESENT:
log.dbg('Blob {module}: {abspath} not in filesystem'.format(**blob))
self.dbg('Blob {module}: {abspath} not in filesystem'.format(**blob))
continue
log.inf('Deleting blob {module}: {status} {abspath}'.format(**blob))
self.inf('Deleting blob {module}: {status} {abspath}'.format(**blob))
blob['abspath'].unlink()

def do_run(self, args, _):
log.dbg(f'subcmd: \'{args.subcmd[0]}\' modules: {args.modules}')
self.dbg(f'subcmd: \'{args.subcmd[0]}\' modules: {args.modules}')

subcmd = getattr(self, args.subcmd[0])

if args.subcmd[0] != 'list' and args.format is not None:
log.die(f'unexpected --format argument; this is a "west blobs list" option')
self.die(f'unexpected --format argument; this is a "west blobs list" option')

subcmd(args)
7 changes: 3 additions & 4 deletions scripts/west_commands/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys
import textwrap

from west import log
from west.commands import WestCommand

from zephyr_ext_common import ZEPHYR_BASE
Expand Down Expand Up @@ -93,13 +92,13 @@ def do_run(self, args, _):
for board in list_boards.find_boards(args):
if name_re is not None and not name_re.search(board.name):
continue
log.inf(args.format.format(name=board.name, arch=board.arch,
dir=board.dir, hwm=board.hwm, qualifiers=''))
self.inf(args.format.format(name=board.name, arch=board.arch,
dir=board.dir, hwm=board.hwm, qualifiers=''))

for board in list_boards.find_v2_boards(args):
if name_re is not None and not name_re.search(board.name):
continue
log.inf(
self.inf(
args.format.format(
name=board.name,
dir=board.dir,
Expand Down
3 changes: 1 addition & 2 deletions scripts/west_commands/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import argparse
import os

from west import log
from west.commands import WestCommand

# Relative to the folder where this script lives
Expand Down Expand Up @@ -78,4 +77,4 @@ def do_run(self, args, unknown_args):
with open(cf, 'r') as f:
print(f.read())
except FileNotFoundError as e:
log.die('Unable to find completion file: {}'.format(e))
self.die('Unable to find completion file: {}'.format(e))
39 changes: 19 additions & 20 deletions scripts/west_commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from shutil import rmtree

from west.commands import WestCommand
from west import log

from zcmake import run_cmake

Expand Down Expand Up @@ -44,25 +43,25 @@ def do_run(self, args, unknown_args):
# The 'share' subdirectory of the top level zephyr repository.
share = Path(__file__).parents[2] / 'share'

run_cmake_export(share / 'zephyr-package' / 'cmake')
run_cmake_export(share / 'zephyrunittest-package' / 'cmake')
self.run_cmake_export(share / 'zephyr-package' / 'cmake')
self.run_cmake_export(share / 'zephyrunittest-package' / 'cmake')

def run_cmake_export(path):
# Run a package installation script.
#
# Filtering out lines that start with -- ignores the normal
# CMake status messages and instead only prints the important
# information.
def run_cmake_export(self, path):
# Run a package installation script.
#
# Filtering out lines that start with -- ignores the normal
# CMake status messages and instead only prints the important
# information.

lines = run_cmake(['-P', str(path / 'zephyr_export.cmake')],
capture_output=True)
msg = [line for line in lines if not line.startswith('-- ')]
log.inf('\n'.join(msg))
lines = run_cmake(['-P', str(path / 'zephyr_export.cmake')],
capture_output=True)
msg = [line for line in lines if not line.startswith('-- ')]
self.inf('\n'.join(msg))

def remove_if_exists(pathobj):
if pathobj.is_file():
log.inf(f'- removing: {pathobj}')
pathobj.unlink()
elif pathobj.is_dir():
log.inf(f'- removing: {pathobj}')
rmtree(pathobj)
def remove_if_exists(self, pathobj):
if pathobj.is_file():
self.inf(f'- removing: {pathobj}')
pathobj.unlink()
elif pathobj.is_dir():
self.inf(f'- removing: {pathobj}')
rmtree(pathobj)
3 changes: 1 addition & 2 deletions scripts/west_commands/shields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import sys
import textwrap

from west import log
from west.commands import WestCommand

from zephyr_ext_common import ZEPHYR_BASE
Expand Down Expand Up @@ -83,4 +82,4 @@ def do_run(self, args, _):
for shield in list_shields.find_shields(args):
if name_re is not None and not name_re.search(shield.name):
continue
log.inf(args.format.format(name=shield.name, dir=shield.dir))
self.inf(args.format.format(name=shield.name, dir=shield.dir))
7 changes: 3 additions & 4 deletions scripts/west_commands/twister_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import os
import sys

from west import log
from west.commands import WestCommand
from west.commands import Verbosity, WestCommand

from zephyr_ext_common import ZEPHYR_SCRIPTS

Expand Down Expand Up @@ -52,8 +51,8 @@ def do_add_parser(self, parser_adder):
return parser

def do_run(self, args, remainder):
log.dbg(
"args: {} remainder: {}".format(args, remainder), level=log.VERBOSE_EXTREME
self.dbg(
"args: {} remainder: {}".format(args, remainder), level=Verbosity.DBG_EXTREME
)

options = parse_arguments(self.parser, args=remainder, options=args)
Expand Down
5 changes: 2 additions & 3 deletions scripts/west_commands/zephyr_ext_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import os
from pathlib import Path

from west import log
from west.commands import WestCommand

# This relies on this file being zephyr/scripts/foo/bar.py.
Expand Down Expand Up @@ -43,5 +42,5 @@ def check_force(self, cond, msg):
self.args.force being True can allow execution to proceed.
'''
if not (cond or self.args.force):
log.err(msg)
log.die('refusing to proceed without --force due to above error')
self.err(msg)
self.die('refusing to proceed without --force due to above error')
Loading