Skip to content

Commit a712ee2

Browse files
Stefan Wiehlera3f
authored andcommitted
remote/client: add write-files command
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> Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent 5537bab commit a712ee2

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ New Features in 24.0
1212
to the serial console during testing.
1313
- The `QEMUDriver` now has an additional ``disk_opts`` property which can be
1414
used to pass additional options for the disk directly to QEMU
15+
- labgrid-client now has a ``write-files`` subcommand to copy files onto mass
16+
storage devices.
1517

1618
Bug fixes in 24.0
1719
~~~~~~~~~~~~~~~~~

contrib/completion/labgrid-client.bash

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,40 @@ _labgrid_client_write_image()
769769
esac
770770
}
771771
772+
_labgrid_client_write_files()
773+
{
774+
local cur prev words cword
775+
_init_completion || return
776+
777+
case "$prev" in
778+
-w|--wait)
779+
;&
780+
-p|--partition)
781+
;&
782+
-t|--target-directory)
783+
;&
784+
-T)
785+
;&
786+
-n|--name)
787+
_labgrid_complete match-names "$cur"
788+
return
789+
;;
790+
esac
791+
792+
case "$cur" in
793+
-*)
794+
local options="--wait --partition --target-directory --name $_labgrid_shared_options"
795+
COMPREPLY=( $(compgen -W "$options" -- "$cur") )
796+
;;
797+
*)
798+
local args
799+
_labgrid_count_args "@(-w|--wait|-p|--partition|-t|--target-directory|-T|-n|--name)" || return
800+
801+
_filedir
802+
;;
803+
esac
804+
}
805+
772806
_labgrid_client_reserve()
773807
{
774808
_labgrid_client_generic_subcommand "--wait --shell --prio"
@@ -888,6 +922,7 @@ _labgrid_client()
888922
audio \
889923
tmc \
890924
write-image \
925+
write-files \
891926
reserve \
892927
cancel-reservation \
893928
wait \

labgrid/remote/client.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import contextlib
66
import enum
77
import os
8+
import pathlib
89
import subprocess
910
import traceback
1011
import logging
@@ -1214,6 +1215,32 @@ def tmc_channel(self):
12141215
for k, v in sorted(data.items()):
12151216
print(f"{k:<16s} {str(v):<10s}")
12161217

1218+
def write_files(self):
1219+
place = self.get_acquired_place()
1220+
target = self._get_target(place)
1221+
name = self.args.name
1222+
drv = self._get_driver_or_new(target, "USBStorageDriver", activate=False, name=name)
1223+
drv.storage.timeout = self.args.wait
1224+
target.activate(drv)
1225+
1226+
try:
1227+
if self.args.partition == 0:
1228+
self.args.partition = None
1229+
1230+
if self.args.rename:
1231+
if len(self.args.SOURCE) != 2:
1232+
self.args.parser.error("the following arguments are required: SOURCE DEST")
1233+
1234+
drv.write_files([self.args.SOURCE[0]], self.args.SOURCE[1],
1235+
self.args.partition, target_is_directory=False)
1236+
else:
1237+
drv.write_files(self.args.SOURCE, self.args.target_directory,
1238+
self.args.partition, target_is_directory=True)
1239+
except subprocess.CalledProcessError as e:
1240+
raise UserError(f"could not copy files to network usb storage: {e}")
1241+
except FileNotFoundError as e:
1242+
raise UserError(e)
1243+
12171244
def write_image(self):
12181245
place = self.get_acquired_place()
12191246
target = self._get_target(place)
@@ -1761,6 +1788,27 @@ def main():
17611788
tmc_subparser.add_argument('action', choices=['info', 'values'])
17621789
tmc_subparser.set_defaults(func=ClientSession.tmc_channel)
17631790

1791+
subparser = subparsers.add_parser('write-files', help="copy files onto mass storage device",
1792+
usage="%(prog)s [OPTION]... -T SOURCE DEST\n" +
1793+
" %(prog)s [OPTION]... [-t DIRECTORY] SOURCE...")
1794+
subparser.add_argument('-w', '--wait', type=float, default=10.0,
1795+
help='storage poll timeout in seconds')
1796+
subparser.add_argument('-p', '--partition', type=int, choices=range(0, 256),
1797+
metavar='0-255', default=1,
1798+
help='partition number to mount or 0 to mount whole disk (default: %(default)s)')
1799+
group = subparser.add_mutually_exclusive_group()
1800+
group.add_argument('-t', '--target-directory', type=pathlib.PurePath, metavar='DIRECTORY',
1801+
default=pathlib.PurePath("/"),
1802+
help='copy all SOURCE files into DIRECTORY (default: partition root)')
1803+
group.add_argument('-T', action='store_true', dest='rename',
1804+
help='copy SOURCE file and rename to DEST')
1805+
subparser.add_argument('--name', '-n', help="optional resource name")
1806+
subparser.add_argument('SOURCE', type=pathlib.PurePath, nargs='+',
1807+
help='source file(s) to copy')
1808+
subparser.add_argument('DEST', type=pathlib.PurePath, nargs='?',
1809+
help='destination file name for SOURCE')
1810+
subparser.set_defaults(func=ClientSession.write_files, parser=subparser)
1811+
17641812
subparser = subparsers.add_parser('write-image', help="write an image onto mass storage")
17651813
subparser.add_argument('-w', '--wait', type=float, default=10.0)
17661814
subparser.add_argument('-p', '--partition', type=int, help="partition number to write to")

man/labgrid-client.1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ not at all.
219219
.sp
220220
\fBtmc\fP command Control a USB TMC device
221221
.sp
222+
\fBwrite\-files\fP filename(s) Copy files onto mass storage device
223+
.sp
222224
\fBwrite\-image\fP Write images onto block devices (USBSDMux, USB Sticks, …)
223225
.sp
224226
\fBreserve\fP filter Create a reservation

man/labgrid-client.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ LABGRID-CLIENT COMMANDS
211211

212212
``tmc`` command Control a USB TMC device
213213

214+
``write-files`` filename(s) Copy files onto mass storage device
215+
214216
``write-image`` Write images onto block devices (USBSDMux, USB Sticks, …)
215217

216218
``reserve`` filter Create a reservation

0 commit comments

Comments
 (0)