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>
This commit is contained in:
Gerald Combs 2019-04-12 12:40:51 -07:00
parent c442ee056b
commit 6f57aa72a8
2 changed files with 34 additions and 40 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Wireshark - Network traffic analyzer
@ -20,17 +20,13 @@ the listing in "oui.txt", "iab.txt", etc, with the entries in
"manuf.tmpl" taking precedence.
'''
import codecs
import csv
import io
import os
import re
import sys
if sys.version_info[0] >= 3:
import urllib.request, urllib.error, urllib.parse
import codecs
else:
import urllib2
import urllib.request, urllib.error, urllib.parse
have_icu = False
try:
@ -54,14 +50,9 @@ def open_url(url):
'''
req_headers = { 'User-Agent': 'Wireshark make-manuf' }
try:
if sys.version_info[0] >= 3:
req = urllib.request.Request(url, headers=req_headers)
response = urllib.request.urlopen(req)
body = response.read().decode('UTF-8', 'replace')
else:
req = urllib2.Request(url, headers=req_headers)
response = urllib2.urlopen(req)
body = response.read()
req = urllib.request.Request(url, headers=req_headers)
response = urllib.request.urlopen(req)
body = response.read().decode('UTF-8', 'replace')
except:
exit_msg('Error opening ' + url)
@ -137,6 +128,10 @@ def prefix_to_oui(prefix):
return '{}/{:d}'.format(oui, int(pfx_len))
def main():
if sys.version_info[0] < 3:
print("This requires Python 3")
sys.exit(2)
this_dir = os.path.dirname(__file__)
template_path = os.path.join(this_dir, '..', 'manuf.tmpl')
manuf_path = os.path.join(this_dir, '..', 'manuf')
@ -190,12 +185,8 @@ def main():
print('Merging {} data from {}'.format(db, db_url))
(body, response_d) = open_url(db_url)
ieee_csv = csv.reader(body.splitlines())
if sys.version_info[0] >= 3:
ieee_d[db]['last-modified'] = response_d['Last-Modified']
ieee_d[db]['length'] = response_d['Content-Length']
else:
ieee_d[db]['last-modified'] = response_d['last-modified']
ieee_d[db]['length'] = response_d['content-length']
ieee_d[db]['last-modified'] = response_d['Last-Modified']
ieee_d[db]['length'] = response_d['Content-Length']
# Pop the title row.
next(ieee_csv)
@ -203,10 +194,7 @@ def main():
#Registry,Assignment,Organization Name,Organization Address
#IAB,0050C2DD6,Transas Marine Limited,Datavagen 37 Askim Vastra Gotaland SE 436 32
oui = prefix_to_oui(ieee_row[1].upper())
if sys.version_info[0] >= 3:
manuf = ieee_row[2].strip()
else:
manuf = ieee_row[2].strip().decode('UTF-8')
manuf = ieee_row[2].strip()
if oui in oui_d:
print(u'{} - Skipping IEEE "{}" in favor of "{}"'.format(oui, manuf, oui_d[oui]))
ieee_d[db]['skipped'] += 1

View File

@ -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)