Add my pre-commit git script (with checkAPI/hf/encoding args...) Need to copy in...
[metze/wireshark/wip.git] / tools / make-usb.py
1 #!/usr/bin/env python
2 #
3 # $Id$
4 #
5 # make-usb - Creates a file containing vendor and product ids.
6 # It use the databases at
7 # http://www.linux-usb.org/usb.ids
8 # to create our file epan/dissectors/usb.c
9 #
10 # It also uses the values culled out of libgphoto2 using usb-ptp-extract-models.pl
11
12 import re
13 import sys
14
15 if sys.version_info[0] < 3:
16     import urllib
17 else:
18     import urllib.request, urllib.error, urllib.parse
19
20 MODE_IDLE           = 0
21 MODE_VENDOR_PRODUCT = 1
22
23
24 mode = MODE_IDLE
25
26 # Grab from linux-usb.org
27 if sys.version_info[0] < 3:
28     response = urllib.urlopen('http://www.linux-usb.org/usb.ids')
29 else:
30     response = urllib.request.urlopen('http://www.linux-usb.org/usb.ids')
31 lines = response.read().splitlines()
32
33 vendors  = dict()
34 products = dict()
35 vendors_str="static const value_string usb_vendors_vals[] = {\n"
36 products_str="static const value_string usb_products_vals[] = {\n"
37
38
39 for line in lines:
40     line = line.rstrip()
41
42     if line == "# Vendors, devices and interfaces. Please keep sorted.":
43         mode = MODE_VENDOR_PRODUCT
44         continue
45     elif line == "# List of known device classes, subclasses and protocols":
46         mode = MODE_IDLE
47         continue
48
49     if mode == MODE_VENDOR_PRODUCT:
50         if re.match("^[0-9a-f]{4}", line):
51             last_vendor=line[:4]
52             vendors[last_vendor] = re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\")))
53         elif re.match("^\t[0-9a-f]{4}", line):
54             line = line.strip()
55             product = "%s%s"%(last_vendor, line[:4])
56             products[product] = re.sub("\"", "\\\"", re.sub("\?+", "?", repr(line[4:].strip())[1:-1].replace("\\", "\\\\")))
57
58
59 # Grab from libgphoto (indirectly through tools/usb-ptp-extract-models.pl)
60 u = open('tools/usb-ptp-extract-models.txt','r')
61 for line in u.readlines():
62     fields=line.split()
63     products[fields[0]]= ' '.join(fields[1:])
64
65
66 for v in sorted(vendors):
67     vendors_str += "    { 0x%s, \"%s\" },\n"%(v,vendors[v])
68
69 vendors_str += """    { 0, NULL }\n};
70 value_string_ext ext_usb_vendors_vals = VALUE_STRING_EXT_INIT(usb_vendors_vals);
71 """
72
73 for p in sorted(products):
74     products_str += "    { 0x%s, \"%s\" },\n"%(p,products[p])
75
76 products_str += """    { 0, NULL }\n};
77 value_string_ext ext_usb_products_vals = VALUE_STRING_EXT_INIT(usb_products_vals);
78 """
79
80 header="""/* usb.c
81  * USB vendor id and product ids
82  * This file was generated by running python ./tools/make-usb.py
83  * Don't change it directly.
84  *
85  * Copyright 2012, Michal Labedzki for Tieto Corporation
86  *
87  * Other values imported from libghoto2/camlibs/ptp2/library.c, music-players.h
88  *
89  * Copyright (C) 2001-2005 Mariusz Woloszyn <emsi@ipartners.pl>
90  * Copyright (C) 2003-2013 Marcus Meissner <marcus@jet.franken.de>
91  * Copyright (C) 2005 Hubert Figuiere <hfiguiere@teaser.fr>
92  * Copyright (C) 2009 Axel Waggershauser <awagger@web.de>
93  * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
94  * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se>
95  * Copyright (C) 2007 Ted Bullock
96  * Copyright (C) 2012 Sony Mobile Communications AB
97  *
98  * $Id$
99  *
100  * Wireshark - Network traffic analyzer
101  * By Gerald Combs <gerald@wireshark.org>
102  * Copyright 1998 Gerald Combs
103  *
104  * This program is free software; you can redistribute it and/or
105  * modify it under the terms of the GNU General Public License
106  * as published by the Free Software Foundation; either version 2
107  * of the License, or (at your option) any later version.
108  *
109  * This program is distributed in the hope that it will be useful,
110  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
112  * GNU General Public License for more details.
113  *
114  * You should have received a copy of the GNU General Public License
115  * along with this program; if not, write to the Free Software
116  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
117  */
118
119 #include "config.h"
120 #include <epan/packet.h>
121 """
122
123 f = open('epan/dissectors/usb.c', 'w')
124 f.write(header)
125 f.write("\n")
126 f.write(vendors_str)
127 f.write("\n\n")
128 f.write(products_str)
129 f.write("\n")
130 f.close()
131
132 print("Success!")
133
134