Skip to content

Commit

Permalink
usb: Added tool examples to generate device lists
Browse files Browse the repository at this point in the history
Signed-off-by: Hubert Figuière <[email protected]>
  • Loading branch information
hfiguiere authored and GeorgesStavracas committed Oct 16, 2024
1 parent 1d56bd3 commit cced00f
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tools/usb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# USB device list generators

These are examples of functional Python 3 scripts to generate the USB
devices lists from some well-known packages.

## epsonscan2-parse.py

This will generate the list for Epson Scan2 based on the udev rules
shipped with the package.

## gphoto2-parse.py

This will parse the output of `print-camera-list` from libgphoto to
generate the list of USB devices.

## libsane-parse.py

This will generate the list for SANE based on the udev rules shipped
with the package.

## utsushi-parse.py

This will parse the SANE .desc shipping with Utsushi to generate the
USB device list.
43 changes: 43 additions & 0 deletions tools/usb/epsonscan2-parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/env python3
#
# Copyright © 2024 GNOME Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Hubert Figuière <[email protected]>
#
# Convert epsonscan2 rules.


import re

vendor_match = re.compile(r"^ATTR\{idVendor\}!=\"([\dabcdef]*)\"")
device_match = re.compile(r"^ATTRS\{idProduct\}==\"([\dabcdef]*)\"")
file = open('epsonscan2.rules', 'r')

vendor = 0
while True:
line = file.readline()
if not line:
break
line = line.strip()
m = vendor_match.match(line)
if m is not None:
vendor = m.group(1)
continue

m = device_match.match(line)
if m is not None:
print("--usb=vnd:{}+dev:{}".format(vendor, m.group(1)))
38 changes: 38 additions & 0 deletions tools/usb/gphoto2-parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/env python3
#
# Copyright © 2024 GNOME Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Hubert Figuière <[email protected]>
#
# Convert the output of libgphoto2 print-camera-list.

import subprocess
import re

device_match = re.compile(r"^([\dabcdef]*):([\dabcdef]*)")
file = subprocess.Popen(['./packaging/generic/print-camera-list', 'idlist'], stdout=subprocess.PIPE).stdout

vendor = 0
while True:
line = file.readline()
if not line:
break
line = line.decode('utf-8')
m = device_match.match(line)
if m is not None:
print("--usb=vnd:{}+dev:{}".format(m.group(1), m.group(2)))
print("--usb=cls:6")
37 changes: 37 additions & 0 deletions tools/usb/libsane-parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/env python3
#
# Copyright © 2024 GNOME Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Hubert Figuière <[email protected]>
#
# Convert libsane udev rules.

import re

device_match = re.compile(r"ATTR\{idVendor\}==\"([\dabcdef]*)\",[\s]*ATTR\{idProduct\}==\"([\dabcdef]*)\"")
file = open('libsane.rules', 'r')

vendor = 0
while True:
line = file.readline()
if not line:
break

m = device_match.match(line)
if m is not None:
print("--usb=vnd:{}+dev:{}".format(m.group(1), m.group(2)))

36 changes: 36 additions & 0 deletions tools/usb/utsushi-parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/env python3
#
# Copyright © 2024 GNOME Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Hubert Figuière <[email protected]>
#
# Convert utsushi device list (SANE .desc).

import subprocess
import re

device_match = re.compile(r"^:usbid[\s]+\"0x([\dabcdef]*)\"[\s]+\"0x([\dabcdef]*)\"")
file = open('sane/utsushi.desc', 'r')

vendor = 0
while True:
line = file.readline()
if not line:
break
m = device_match.match(line)
if m is not None:
print("--usb=vnd:{}+dev:{}".format(m.group(1), m.group(2)))

0 comments on commit cced00f

Please sign in to comment.