Skip to content

Commit 8cdf086

Browse files
committed
Code review cleanup
1 parent 1d2997a commit 8cdf086

File tree

11 files changed

+217
-74
lines changed

11 files changed

+217
-74
lines changed

.github/workflows/build_test.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ jobs:
3939
python3 -m mypy ./lglpy
4040
python3 -m mypy ./generator
4141
42+
- name: Run unit tests
43+
# Note: Only run tests that do not require a connected device
44+
run: |
45+
python3 -m lglpy.ui.test
46+
4247
build-ubuntu-x64-clang:
4348
name: Ubuntu x64 Clang
4449
runs-on: ubuntu-22.04

generator/generate_vulkan_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# SPDX-License-Identifier: MIT
33
# -----------------------------------------------------------------------------
4-
# Copyright (c) 2024 Arm Limited
4+
# Copyright (c) 2024-2025 Arm Limited
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the "Software"), to

lgl_host_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/env python3
22
# SPDX-License-Identifier: MIT
33
# -----------------------------------------------------------------------------
4-
# Copyright (c) 2024 Arm Limited
4+
# Copyright (c) 2024-2025 Arm Limited
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the 'Software'), to

lglpy/android/adb.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ class ADBConnect:
3838
A wrapper around adb which can be used to connect to a specific device,
3939
and run commands as a specific package.
4040
41+
- adb() runs a command using adb and waits for the result.
42+
- adb_async() runs a command using adb and does not wait for the result.
43+
- adb_run() runs a device shell command as the "shell" user and waits for
44+
the result.
45+
- adb_runas() runs a device shell command as the current package user and
46+
waits for the result.
47+
48+
The current device and package are attributes of the connection instance
49+
which can be set at construction, or via the set_device() and set_package()
50+
methods.
51+
4152
Attributes:
4253
device: The name of the connected device, or None for generic use.
4354
package: The name of the debuggable package, or None for generic use.
@@ -159,9 +170,7 @@ def adb(self, *args: str, text: bool = True, shell: bool = False,
159170

160171
# Invoke the command
161172
rep = sp.run(packed_commands, check=check, shell=shell, text=text,
162-
stdin=sp.DEVNULL,
163-
stdout=sp.PIPE,
164-
stderr=sp.PIPE)
173+
stdin=sp.DEVNULL, stdout=sp.PIPE, stderr=sp.PIPE)
165174

166175
# Return the output
167176
return rep.stdout
@@ -207,9 +216,7 @@ def adb_async(self, *args: str, text: bool = True, shell: bool = False,
207216
# pylint: disable=consider-using-with
208217
process = sp.Popen(packed_commands,
209218
text=text, shell=shell,
210-
stdin=sp.DEVNULL,
211-
stdout=output,
212-
stderr=sp.DEVNULL)
219+
stdin=sp.DEVNULL, stdout=output, stderr=sp.DEVNULL)
213220

214221
# Return the output process a user can use to wait, if needed.
215222
return process
@@ -244,10 +251,9 @@ def adb_run(self, *args: str, text: bool = True, shell: bool = False,
244251
packed_commands = self.pack_commands(commands, shell, quote)
245252

246253
# Invoke the command
247-
rep = sp.run(packed_commands, check=check, shell=shell, text=text,
248-
stdin=sp.DEVNULL,
249-
stdout=sp.PIPE,
250-
stderr=sp.PIPE)
254+
rep = sp.run(packed_commands,
255+
check=check, shell=shell, text=text,
256+
stdin=sp.DEVNULL, stdout=sp.PIPE, stderr=sp.PIPE)
251257

252258
# Return the output
253259
return rep.stdout
@@ -285,10 +291,9 @@ def adb_runas(self, *args: str, text: bool = True, shell: bool = False,
285291
packed_commands = self.pack_commands(commands, shell, quote)
286292

287293
# Invoke the command
288-
rep = sp.run(packed_commands, check=check, shell=shell, text=text,
289-
stdin=sp.DEVNULL,
290-
stdout=sp.PIPE,
291-
stderr=sp.PIPE)
294+
rep = sp.run(packed_commands,
295+
check=check, shell=shell, text=text,
296+
stdin=sp.DEVNULL, stdout=sp.PIPE, stderr=sp.PIPE)
292297

293298
# Return the output
294299
return rep.stdout

lglpy/android/filesystem.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
# -----------------------------------------------------------------------------
2323

2424
'''
25-
This module implements higher level Android queries and utilities, built on top
26-
of the low level Android Debug Bridge wrapper.
25+
This module implements higher level Android filesystem utilities, built on top
26+
of the low level ADBConnect wrapper around Android Debug Bridge.
2727
'''
2828

2929
import os
@@ -36,6 +36,11 @@
3636
class AndroidFilesystem:
3737
'''
3838
A library of utility methods for transferring files to/from a device.
39+
40+
Attributes:
41+
TEMP_DIR: The generally accessible device temp directory.
42+
DATA_PERM: The file permissions to use for data files.
43+
EXEC_PERM: The file permissions to use for executable files.
3944
'''
4045

4146
TEMP_DIR = '/data/local/tmp'
@@ -49,12 +54,12 @@ def push_file_to_tmp(
4954
'''
5055
Push a file to the device temp directory.
5156
52-
File will be copied to: /data/local/tmp/<file>.
57+
File will be copied to: TEMP_DIR/<file>.
5358
5459
Args:
5560
conn: The adb connection.
5661
host_path: The path of the file on the host file system.
57-
executable: True if the file should be configured as executable.
62+
executable: True if the file should have executable permissions.
5863
5964
Returns:
6065
True if the file was copied, False otherwise.
@@ -65,16 +70,17 @@ def push_file_to_tmp(
6570

6671
try:
6772
# Remove old file to prevent false success
68-
conn.adb('shell', 'rm', '-f', device_path)
73+
conn.adb_run('rm', '-f', device_path)
6974

7075
# Push new file
7176
conn.adb('push', host_path, device_path)
7277

7378
# Check it actually copied
74-
conn.adb('shell', 'ls', device_path)
79+
conn.adb_run('ls', device_path)
7580

7681
permission = cls.EXEC_PERM if executable else cls.DATA_PERM
77-
conn.adb('shell', 'chmod', permission, device_path)
82+
conn.adb_run('chmod', permission, device_path)
83+
7884
except sp.CalledProcessError:
7985
return False
8086

@@ -94,7 +100,7 @@ def pull_file_from_tmp(
94100
file_name: The name of the file in the tmp directory.
95101
host_path: The destination directory on the host file system.
96102
Host directory will be created if it doesn't exist.
97-
delete: Delete the file on the device after copying it.
103+
delete: Should the file on the device be deleted after copying?
98104
99105
Returns:
100106
True if the file was copied, False otherwise.
@@ -121,7 +127,7 @@ def delete_file_from_tmp(
121127
'''
122128
Delete a file from the device temp directory.
123129
124-
File will be deleted from: /data/local/tmp/<file>.
130+
File will be deleted from: TEMP_DIR/<file>.
125131
126132
Args:
127133
conn: The adb connection.
@@ -135,9 +141,9 @@ def delete_file_from_tmp(
135141

136142
try:
137143
if error_ok:
138-
conn.adb('shell', 'rm', '-f', device_path)
144+
conn.adb_run('rm', '-f', device_path)
139145
else:
140-
conn.adb('shell', 'rm', device_path)
146+
conn.adb_run('rm', device_path)
141147
except sp.CalledProcessError:
142148
return False
143149

@@ -155,7 +161,7 @@ def push_file_to_package(
155161
Args:
156162
conn: The adb connection.
157163
host_path: The path of the file on the host file system.
158-
executable: True if the file should be configured as executable.
164+
executable: True if the file should have executable permissions.
159165
160166
Returns:
161167
True if the file was copied, False otherwise.
@@ -197,7 +203,7 @@ def pull_file_from_package(
197203
src_file: The name of the file in the tmp directory.
198204
host_path: The destination directory on the host file system.
199205
Host directory will be created if it doesn't exist.
200-
delete: Delete the file on the device after copying it.
206+
delete: Should the file on the device be deleted after copying?
201207
202208
Returns:
203209
True if the file was copied, False otherwise.
@@ -218,6 +224,7 @@ def pull_file_from_package(
218224

219225
if delete:
220226
cls.delete_file_from_package(conn, src_file)
227+
221228
except sp.SubprocessError:
222229
return False
223230

@@ -227,13 +234,13 @@ def pull_file_from_package(
227234
def rename_file_in_package(
228235
cls, conn: ADBConnect, file_name: str, new_file_name: str) -> bool:
229236
'''
230-
Delete a file from the package directory.
237+
Rename a file in the package directory.
231238
232-
File will be deleted from, e.g.: /data/user/0/<package>/<file>
239+
File will be renamed to, e.g.: /data/user/0/<package>/<new_file>
233240
234241
Args:
235242
conn: The adb connection.
236-
file_name: The name of the file to rename.
243+
file_name: The name of the existing file to rename.
237244
new_file_name: The new file name to use.
238245
239246
Returns:

lglpy/android/test.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
from .utils import AndroidUtils
4141
from .filesystem import AndroidFilesystem
4242

43-
SLOW_TESTS = False # Set to True to enable slot tests, False to skip them
43+
44+
SLOW_TESTS = True # Set to True to enable slow tests, False to skip them
4445

4546

4647
def get_script_relative_path(file_name: str) -> str:
@@ -564,6 +565,46 @@ def test_util_copy_to_package_exec(self):
564565
success = AndroidFilesystem.delete_file_from_package(conn, test_file)
565566
self.assertTrue(success)
566567

568+
def test_util_rename_in_package(self):
569+
'''
570+
Test filesystem rename in package data directory.
571+
'''
572+
conn = ADBConnect()
573+
574+
# Fetch some packages that we can use
575+
packages = AndroidUtils.get_packages(conn, True, False)
576+
self.assertGreater(len(packages), 0)
577+
conn.set_package(packages[0])
578+
579+
test_file = 'test_data.txt'
580+
test_path = get_script_relative_path(test_file)
581+
582+
# Push the file
583+
success = AndroidFilesystem.push_file_to_package(conn, test_path)
584+
self.assertTrue(success)
585+
586+
# Validate it pushed OK
587+
data = conn.adb_runas('ls', test_file)
588+
self.assertEqual(data.strip(), test_file)
589+
590+
# Rename the file
591+
new_test_file = 'test_data_2.txt'
592+
success = AndroidFilesystem.rename_file_in_package(
593+
conn, test_file, new_test_file)
594+
self.assertTrue(success)
595+
596+
# Validate it was moved - this should fail
597+
with self.assertRaises(sp.CalledProcessError):
598+
data = conn.adb_runas('ls', test_file)
599+
600+
data = conn.adb_runas('ls', new_test_file)
601+
self.assertEqual(data.strip(), new_test_file)
602+
603+
# Cleanup tmp - this should fail because the file does not exist
604+
success = AndroidFilesystem.delete_file_from_package(
605+
conn, new_test_file)
606+
self.assertTrue(success)
607+
567608
def test_util_copy_from_package(self):
568609
'''
569610
Test filesystem copy from package data directory to host.

0 commit comments

Comments
 (0)