Merge asn2deb and idl2deb from official Debian package
[metze/wireshark/wip.git] / tools / asn2deb
1 #!/usr/bin/env python
2
3 # asn2deb - quick hack by W. Borgert <debacle@debian.org> to create
4 # Debian GNU/Linux packages from ASN.1 files for Wireshark.
5 # Copyright 2004, W. Borgert
6
7 # ASN.1 module for Wireshark, use of snacc type table:
8 # Copyright 2003, Matthijs Melchior <matthijs.melchior@xs4all.nl>
9 #
10 # Wireshark - Network traffic analyzer
11 # By Gerald Combs <gerald@wireshark.com>
12 # Copyright 1998 Gerald Combs
13
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27
28 import getopt, os, string, sys, time
29
30 scriptinfo = """asn2deb version 2004-02-17
31 Copyright 2004, W. Borgert
32 Free software, released under the terms of the GPL."""
33
34 options = {'asn':      None,
35            'dbopts':   "",
36            'email':    "invalid@invalid.invalid",
37            'help':     0,
38            'name':     "No Name",
39            'preserve': 0,
40            'version':  0}
41
42 def bootstrap():
43     """Generate Makefile.in and configure script."""
44     os.system("aclocal-1.7")
45     os.system("autoconf")
46     os.system("automake-1.7 --add-missing --copy --foreign")
47
48 def create_file(filename, content, mode = None):
49     """Create a file with given content."""
50     global options
51     if options['preserve'] and os.path.isfile(filename):
52         return
53     f = open(filename, 'w')
54     f.write(content)
55     f.close()
56     if mode:
57         os.chmod(filename, mode)
58
59 def create_files(version, deb, email, asn, name, iso, rfc):
60     """Create all files for the .deb build process."""
61     base = asn.lower()[:-5]
62     create_file("Makefile.am", """#
63
64 BUILT_SOURCES = %s.tt
65 asn1ttdir = $(datadir)/wireshark/asn1
66 asn1tt_DATA = %s.tt
67
68 %s.tt: """  % ((base,) * 3) + asn + """
69         snacc -u /usr/include/snacc/asn1/asn-useful.asn1 -T $@ $<
70 """)
71
72     create_file("configure.ac", """AC_INIT(%s, 1.0)
73 AM_INIT_AUTOMAKE
74 AM_MAINTAINER_MODE
75 AC_PROG_INSTALL
76 SNACC=\"`type -p snacc`\"
77 AC_SUBST(SNACC)
78 dnl WIRESHARK_VERSION=\"%s\"
79 dnl plugindir=\"$prefix/share/wireshark/asn.1\"
80 dnl AC_SUBST(plugindir)
81 AC_OUTPUT([Makefile])
82 """ % (base, version))
83
84     if not os.path.isdir("debian"):
85         os.mkdir("debian")
86
87     create_file("debian/rules", """#!/usr/bin/make -f
88
89 include /usr/share/cdbs/1/rules/debhelper.mk
90 include /usr/share/cdbs/1/class/autotools.mk
91
92 PREFIX=`pwd`/debian/wireshark-asn1-%s
93
94 binary-post-install/wireshark-asn1-%s::
95         rm -f $(PREFIX)/usr/lib/wireshark/plugins/%s/*.a
96 """ % (base, base, version), 0755)
97
98     create_file("debian/control", """Source: wireshark-asn1-%s
99 Section: net
100 Priority: optional
101 Maintainer: %s <%s>
102 Standards-Version: 3.6.1.0
103 Build-Depends: snacc, autotools-dev, debhelper, cdbs
104
105 Package: wireshark-asn1-%s
106 Architecture: all
107 Depends: wireshark (= %s)
108 Description: ASN.1/BER dissector for %s
109  This package provides a type table for decoding BER (Basic Encoding
110  Rules) data over TCP or UDP, described by an ASN.1 (Abstract Syntax
111  Notation 1) file '%s.asn1'.
112 """ % (base, name, email, base, deb, base, base))
113
114     create_file("debian/changelog",
115             """wireshark-asn1-%s (0.0.1-1) unstable; urgency=low
116
117   * Automatically created package.
118
119  -- %s <%s>  %s
120 """ % (base, name, email, rfc + "\n    (" + iso + ")"))
121
122     create_file("debian/copyright",
123             """This package has been created automatically be asn2deb on
124 %s for Debian GNU/Linux.
125
126 Wireshark: http://www.wireshark.com/
127
128 Copyright:
129
130 GPL, as evidenced by existence of GPL license file \"COPYING\".
131 (the GNU GPL may be viewed on Debian systems in
132 /usr/share/common-licenses/GPL)
133 """ % (iso))
134
135 def get_wrs_version():
136     """Detect version of wireshark-dev package."""
137     deb = os.popen(
138         "dpkg-query -W --showformat='${Version}' wireshark-dev").read()
139     debv = string.find(deb, "-")
140     if debv == -1: debv = len(deb)
141     version = deb[string.find(deb, ":")+1:debv]
142     return version, deb
143
144 def get_time():
145     """Detect current time and return ISO and RFC time string."""
146     currenttime = time.gmtime()
147     return time.strftime("%Y-%m-%d %H:%M:%S +0000", currenttime), \
148            time.strftime("%a, %d %b %Y %H:%M:%S +0000", currenttime)
149
150 def main():
151     global options
152     process_opts(sys.argv)
153     iso, rfc = get_time()
154     version, deb = get_wrs_version()
155     create_files(version, deb,
156                  options['email'], options['asn'], options['name'],
157                  iso, rfc)
158     bootstrap()
159     os.system("dpkg-buildpackage " + options['dbopts'])
160
161 def process_opts(argv):
162     """Process command line options."""
163     global options
164     try:
165         opts, args = getopt.getopt(argv[1:], "a:d:e:hn:pv",
166                                    ["asn=",
167                                     "dbopts=",
168                                     "email=",
169                                     "help",
170                                     "name=",
171                                     "preserve",
172                                     "version"])
173     except getopt.GetoptError:
174         usage(argv[0])
175         sys.exit(1)
176     for o, a in opts:
177         if o in ("-a", "--asn"):
178             options['asn'] = a
179         if o in ("-d", "--dbopts"):
180             options['dbopts'] = a
181         if o in ("-e", "--email"):
182             options['email'] = a
183         if o in ("-h", "--help"):
184             options['help'] = 1
185         if o in ("-n", "--name"):
186             options['name'] = a
187         if o in ("-p", "--preserve"):
188             options['preserve'] = 1
189         if o in ("-v", "--version"):
190             options['version'] = 1
191     if options['help']:
192         usage(argv[0])
193         sys.exit(0)
194     if options['version']:
195         print scriptinfo
196         sys.exit(0)
197     if not options['asn']:
198         print "mandatory ASN.1 file parameter missing"
199         sys.exit(1)
200     if not os.access(options['asn'], os.R_OK):
201         print "ASN.1 file not accessible"
202         sys.exit(1)
203
204 def usage(name):
205     """Print usage help."""
206     print "Usage: " + name + " <parameters>\n" + \
207           "Parameters are\n" + \
208           "  --asn      -a asn1file, ASN.1 file to use (mandatory)\n" + \
209           "  --dbopts   -d opts,     options for dpkg-buildpackage\n" + \
210           "  --email    -e address,  use e-mail address\n" + \
211           "  --help     -h,          print help and exit\n" + \
212           "  --name     -n name,     use user name\n" + \
213           "  --preserve -p,          do not overwrite files\n" + \
214           "  --version  -v,          print version and exit\n" + \
215           "Example:\n" + \
216           name + " -e me@foo.net -a bar.asn1 -n \"My Name\" " + \
217           "-d \"-rfakeroot -uc -us\""
218 if __name__ == '__main__':
219     main()