libfprint/tests/create-misc-driver-test.py.in
2026-05-01 16:12:53 +00:00

99 lines
2.9 KiB
Python
Executable file

#!/usr/bin/python3
BUILDDIR='@BUILDDIR@'
SRCDIR='@SRCDIR@'
import os
import sys
library_path = BUILDDIR + '/libfprint/'
# Relaunch ourselves with a changed environment so
# that we're loading the development version of libfprint
if 'LD_LIBRARY_PATH' not in os.environ or not library_path in os.environ['LD_LIBRARY_PATH']:
os.environ['LD_LIBRARY_PATH'] = library_path
os.environ['GI_TYPELIB_PATH'] = f'{BUILDDIR}/libfprint/'
os.environ['FP_DEVICE_EMULATION'] = '1'
try:
os.execv(sys.argv[0], sys.argv)
except Exception as e:
print('Could not run script with new library path')
sys.exit(1)
import gi
gi.require_version('FPrint', '2.0')
from gi.repository import FPrint
import re
import subprocess
def print_usage():
print(f'Usage: {sys.argv[0]} driver [test-variant-name]')
print('A test variant name is optional, and must be all lower case letters, or dashes, with no spaces')
print(f'The captured data will be stored in {SRCDIR}/tests/[driver name]-[test variant name]')
print(f'Create custom.py prior to execution for non image device tests.')
if len(sys.argv) > 3:
print_usage()
sys.exit(1)
driver_name = sys.argv[1]
os.environ['FP_DRIVERS_ALLOWLIST'] = driver_name
test_variant = None
if len(sys.argv) == 3:
valid_re = re.compile('[a-z-]*')
test_variant = sys.argv[2]
if (not valid_re.match(test_variant) or
test_variant.startswith('-') or
test_variant.endswith('-')):
print(f'Invalid variant name {test_variant}\n')
print_usage()
sys.exit(1)
# Check that running as root
if os.geteuid() != 0:
print(f'{sys.argv[0]} is expected to be run as root')
sys.exit(1)
# Find the fingerprint reader
ctx = FPrint.Context()
ctx.enumerate()
devices = ctx.get_devices()
if len(devices) == 0:
print('Could not find a supported fingerprint reader')
sys.exit(1)
elif len(devices) > 1:
print('Capture requires a single supported fingerprint reader to be plugged in')
sys.exit(1)
test_name = driver_name
if test_variant:
test_name = driver_name + '-' + test_variant
misc_device = devices[0].get_property('fpi-udev-data-misc')
print(f'### Detected misc device {misc_device}')
# Make directory
test_dir = SRCDIR + '/tests/' + test_name
os.makedirs(test_dir, mode=0o775, exist_ok=True)
# Capture device info
args = ['umockdev-record', misc_device]
device_out = open(test_dir + '/device', 'w')
process = subprocess.Popen(args, stdout=device_out)
process.wait()
print('### Capturing fingerprint, please swipe or press your finger on the reader')
capture_file = "custom.ioctl"
cmd = ['umockdev-record', '--ioctl', f'{misc_device}={os.path.join(test_dir, capture_file)}', 'python3', os.path.join(test_dir, "custom.py")]
with subprocess.Popen(cmd) as capture_process:
capture_process.wait()
if capture_process.returncode != 0:
print('Failed to capture fingerprint')
sys.exit(1)
print(f"\nDone! Don't forget to add {test_name} to tests/meson.build")