aboutsummaryrefslogtreecommitdiffstats
path: root/tools/make-usb.py
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2019-04-12 12:40:51 -0700
committerGerald Combs <gerald@wireshark.org>2019-04-12 20:12:10 +0000
commit6f57aa72a8a14ee2247ea9180a34dcd21dd6ed3b (patch)
treeb8dd4714253d3daa4745d616fb8a09a578609d53 /tools/make-usb.py
parentc442ee056bc46bcda59e473c00d5741ea90a1453 (diff)
Make a couple of scripts Python 3 only.
Remove Python 2 support from tools/make-manuf.py and tools/make-usb.py. Don't double-escape UTF-8 sequences in make-usb.py so that we generate { 0x045e000e, "SideWinder\xc2\xae Freestyle Pro" }, instead of { 0x045e000e, "SideWinder\\xc2\\xae Freestyle Pro" }, Change-Id: I918f854ccba868a122fd7b138c1654b2c7615f94 Reviewed-on: https://code.wireshark.org/review/32839 Reviewed-by: Gerald Combs <gerald@wireshark.org>
Diffstat (limited to 'tools/make-usb.py')
-rwxr-xr-xtools/make-usb.py36
1 files changed, 21 insertions, 15 deletions
diff --git a/tools/make-usb.py b/tools/make-usb.py
index bc9c6cb236..19d776bd85 100755
--- a/tools/make-usb.py
+++ b/tools/make-usb.py
@@ -1,4 +1,5 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
#
# make-usb - Creates a file containing vendor and product ids.
# It use the databases at
@@ -9,11 +10,7 @@
import re
import sys
-
-if sys.version_info[0] < 3:
- import urllib
-else:
- import urllib.request, urllib.error, urllib.parse
+import urllib.request, urllib.error, urllib.parse
MODE_IDLE = 0
MODE_VENDOR_PRODUCT = 1
@@ -22,12 +19,15 @@ MIN_PRODUCTS = 15000 # 15415 as of 2015-06-28
mode = MODE_IDLE
-# Grab from linux-usb.org
if sys.version_info[0] < 3:
- response = urllib.urlopen('http://www.linux-usb.org/usb.ids')
-else:
- response = urllib.request.urlopen('http://www.linux-usb.org/usb.ids')
-lines = response.read().splitlines()
+ print("This requires Python 3")
+ sys.exit(2)
+
+# Grab from linux-usb.org
+req_headers = { 'User-Agent': 'Wireshark make-usb' }
+req = urllib.request.Request('http://www.linux-usb.org/usb.ids', headers=req_headers)
+response = urllib.request.urlopen(req)
+lines = response.read().decode('UTF-8', 'replace').splitlines()
vendors = dict()
products = dict()
@@ -35,8 +35,14 @@ vendors_str="static const value_string usb_vendors_vals[] = {\n"
products_str="static const value_string usb_products_vals[] = {\n"
-for line in lines:
- line = line.rstrip()
+for utf8line in lines:
+ # Convert single backslashes to double (escaped) backslashes, escape quotes, etc.
+ utf8line = utf8line.rstrip()
+ utf8line = utf8line.replace('\\', '\\\\')
+ utf8line = utf8line.replace('"', '\\"')
+ utf8line = re.sub("\?+", "?", utf8line)
+ # Finally, convert non-ASCII UTF-8 sequences to C-style escapes
+ line = utf8line.encode('UTF-8').decode('ascii', 'backslashreplace')
if line == "# Vendors, devices and interfaces. Please keep sorted.":
mode = MODE_VENDOR_PRODUCT
@@ -48,11 +54,11 @@ for line in lines:
if mode == MODE_VENDOR_PRODUCT:
if re.match("^[0-9a-f]{4}", line):
last_vendor=line[:4]
- vendors[last_vendor] = re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\")))
+ vendors[last_vendor] = line[4:].strip()
elif re.match("^\t[0-9a-f]{4}", line):
line = line.strip()
product = "%s%s"%(last_vendor, line[:4])
- products[product] = re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\")))
+ products[product] = line[4:].strip()
# Grab from libgphoto (indirectly through tools/usb-ptp-extract-models.pl)