Skip to content

Commit

Permalink
remote/client: add write-files command
Browse files Browse the repository at this point in the history
Analogously to write-image, add a write-file command that can be used to
overwrite (or create anew) a single file in a file system located on a
USBStorageDriver. This is useful for faster iteration during bootloader
development when a bootloader is loaded from a file system.

Following usages are supported:

  labgrid-client write-files a b      # writes files a, b into /mnt/
  labgrid-client write-files -t a b c # writes files b, c into /mnt/a/
  labgrid-client write-files -T a b   # writes file  b      to /mnt/a

Where /mnt is some dynamic path on the, possibly remote, host exporting
the USBStorageDriver.

Signed-off-by: Stefan Wiehler <stefan.wiehler@missinglinkelectronics.com>
Co-authored-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
  • Loading branch information
Stefan Wiehler and a3f committed Jun 25, 2023
1 parent b2b1b99 commit 3122da9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ New Features in 23.1
- A new log level called ``CONSOLE`` has been added between the default
``INFO`` and ``DEBUG`` levels. This level will show all reads and writes made
to the serial console during testing.
- labgrid-client now has a ``write-files`` subcommand to copy files onto mass
storage devices.

Bug fixes in 23.1
~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions contrib/completion/labgrid-client.bash
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ _labgrid_client()
audio \
tmc \
write-image \
write-files \
reserve \
cancel-reservation \
wait \
Expand Down
48 changes: 48 additions & 0 deletions labgrid/remote/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import contextlib
import enum
import os
import pathlib
import subprocess
import traceback
import logging
Expand Down Expand Up @@ -1204,6 +1205,32 @@ def tmc_channel(self):
for k, v in sorted(data.items()):
print(f"{k:<16s} {str(v):<10s}")

def write_files(self):
place = self.get_acquired_place()
target = self._get_target(place)
name = self.args.name
drv = self._get_driver_or_new(target, "USBStorageDriver", activate=False, name=name)
drv.storage.timeout = self.args.wait
target.activate(drv)

try:
if self.args.partition == 0:
self.args.partition=None

if self.args.rename:
if len(self.args.SOURCE) != 2:
self.args.parser.error("the following arguments are required: SOURCE DEST")

drv.write_files([self.args.SOURCE[0]], self.args.SOURCE[1],
self.args.partition, target_is_directory=False)
else:
drv.write_files(self.args.SOURCE, self.args.target_directory,
self.args.partition, target_is_directory=True)
except subprocess.CalledProcessError as e:
raise UserError(f"could not copy files to network usb storage: {e}")
except FileNotFoundError as e:
raise UserError(e)

def write_image(self):
place = self.get_acquired_place()
target = self._get_target(place)
Expand Down Expand Up @@ -1751,6 +1778,27 @@ def main():
tmc_subparser.add_argument('action', choices=['info', 'values'])
tmc_subparser.set_defaults(func=ClientSession.tmc_channel)

subparser = subparsers.add_parser('write-files', help="copy files onto mass storage device",
usage="%(prog)s [OPTION]... -T SOURCE DEST\n" +
" %(prog)s [OPTION]... [-t DIRECTORY] SOURCE...")
subparser.add_argument('-w', '--wait', type=float, default=10.0,
help='storage poll timeout in seconds')
subparser.add_argument('-p', '--partition', type=int, choices=range(0, 256),
metavar='0-255', default=1,
help='partition number to mount or 0 to mount whole disk (default: %(default)s)')
group = subparser.add_mutually_exclusive_group()
group.add_argument('-t', '--target-directory', type=pathlib.PurePath, metavar='DIRECTORY',
default=pathlib.PurePath("/"),
help='copy all SOURCE files into DIRECTORY (default: partition root)')
group.add_argument('-T', action='store_true', dest='rename',
help='copy SOURCE file and rename to DEST')
subparser.add_argument('--name', '-n', help="optional resource name")
subparser.add_argument('SOURCE', type=pathlib.PurePath, nargs='+',
help='source file(s) to copy')
subparser.add_argument('DEST', type=pathlib.PurePath, nargs='?',
help='destination file name for SOURCE')
subparser.set_defaults(func=ClientSession.write_files, parser=subparser)

subparser = subparsers.add_parser('write-image', help="write an image onto mass storage")
subparser.add_argument('-w', '--wait', type=float, default=10.0)
subparser.add_argument('-p', '--partition', type=int, help="partition number to write to")
Expand Down
2 changes: 2 additions & 0 deletions man/labgrid-client.1
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ not at all.
.sp
\fBtmc\fP command Control a USB TMC device
.sp
\fBwrite\-files\fP filename(s) Copy files onto mass storage device
.sp
\fBwrite\-image\fP Write images onto block devices (USBSDMux, USB Sticks, …)
.sp
\fBreserve\fP filter Create a reservation
Expand Down
2 changes: 2 additions & 0 deletions man/labgrid-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ LABGRID-CLIENT COMMANDS

``tmc`` command Control a USB TMC device

``write-files`` filename(s) Copy files onto mass storage device

``write-image`` Write images onto block devices (USBSDMux, USB Sticks, …)

``reserve`` filter Create a reservation
Expand Down

0 comments on commit 3122da9

Please sign in to comment.