2020-11-03 19:22:14 +00:00
#!/usr/bin/env python3
2019-11-08 22:55:31 +00:00
#
2019-11-15 09:43:10 +00:00
# Copyright 2019, Dario Lombardo <lomato@gmail.com>
2019-11-08 22:55:31 +00:00
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This script generates a Wireshark skeleton dissector, based on the example in the doc/ directory.
#
# Example usage:
#
# generate-dissector.py --name "My Self" --email "myself@example.com" --protoname "The dumb protocol"
# --protoshortname DUMB --protoabbrev dumb --license GPL-2.0-or-later --years "2019-2020"
#
import argparse
from datetime import datetime
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
import os
2019-11-08 22:55:31 +00:00
parser = argparse . ArgumentParser ( description = ' The Wireshark Dissector Generator ' )
parser . add_argument ( " --name " , help = " The author of the dissector " , required = True )
parser . add_argument ( " --email " , help = " The email address of the author " , required = True )
parser . add_argument ( " --protoname " , help = " The name of the protocol " , required = True )
parser . add_argument ( " --protoshortname " , help = " The protocol short name " , required = True )
parser . add_argument ( " --protoabbrev " , help = " The protocol abbreviation " , required = True )
2023-01-27 15:41:07 +00:00
parser . add_argument ( " --license " , help = " The license for this dissector (please use a SPDX-License-Identifier). If omitted, %(default)s will be used " , default = " GPL-2.0-or-later " )
parser . add_argument ( " --years " , help = " Years of validity for the license. If omitted, the current year will be used " , default = str ( datetime . now ( ) . year ) )
2019-11-08 22:55:31 +00:00
parser . add_argument ( " -f " , " --force " , action = ' store_true ' , help = " Force overwriting the dissector file if it already exists " )
2023-01-27 15:41:07 +00:00
parser . add_argument ( " -p " , " --plugin " , action = ' store_true ' , help = " Create as a plugin. Default is to create in epan " )
2019-11-08 22:55:31 +00:00
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2019-11-08 22:55:31 +00:00
def wsdir ( ) :
return os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , " .. " ) )
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2023-01-27 15:41:07 +00:00
def output_dir ( args ) :
if args . plugin :
os . makedirs ( os . path . join ( wsdir ( ) , " plugins/epan/ " + args . protoabbrev ) , exist_ok = True )
return os . path . join ( wsdir ( ) , " plugins/epan/ " + args . protoabbrev )
return os . path . join ( wsdir ( ) , " epan/dissectors " )
2019-11-08 22:55:31 +00:00
def output_file ( args ) :
2023-01-27 15:41:07 +00:00
return os . path . join ( output_dir ( args ) , " packet- " + args . protoabbrev + " .c " )
2019-11-08 22:55:31 +00:00
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2023-01-27 15:41:07 +00:00
def read_skeleton ( filename ) :
skeletonfile = os . path . join ( wsdir ( ) , " doc/ " + filename )
2019-11-15 09:43:10 +00:00
print ( " Reading skeleton file: " + skeletonfile )
2019-11-08 22:55:31 +00:00
return open ( skeletonfile ) . read ( )
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2019-11-08 22:55:31 +00:00
def replace_fields ( buffer , args ) :
print ( " Replacing fields in skeleton " )
output = buffer \
. replace ( " YOUR_NAME " , args . name ) \
. replace ( " YOUR_EMAIL_ADDRESS " , args . email ) \
. replace ( " PROTONAME " , args . protoname ) \
. replace ( " PROTOSHORTNAME " , args . protoshortname ) \
. replace ( " PROTOABBREV " , args . protoabbrev ) \
. replace ( " FIELDNAME " , " Sample Field " ) \
. replace ( " FIELDABBREV " , " sample_field " ) \
. replace ( " FT_FIELDTYPE " , " FT_STRING " ) \
. replace ( " FIELDDISPLAY " , " BASE_NONE " ) \
. replace ( " FIELDCONVERT " , " NULL " ) \
. replace ( " BITMASK " , " 0x0 " ) \
. replace ( " FIELDDESCR " , " NULL " ) \
. replace ( " MAX_NEEDED_FOR_HEURISTICS " , " 1 " ) \
. replace ( " TEST_HEURISTICS_FAIL " , " 0 " ) \
. replace ( " ENC_xxx " , " ENC_NA " ) \
. replace ( " EXPERTABBREV " , " expert " ) \
. replace ( " PI_GROUP " , " PI_PROTOCOL " ) \
. replace ( " PI_SEVERITY " , " PI_ERROR " ) \
. replace ( " TEST_EXPERT_condition " , " 0 " ) \
2023-01-27 15:41:07 +00:00
. replace ( " const char *subtree " , " \" \" " ) \
. replace ( " LICENSE " , args . license ) \
. replace ( " YEARS " , args . years )
2019-11-08 22:55:31 +00:00
return output
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2019-11-08 22:55:31 +00:00
def write_dissector ( buffer , args ) :
ofile = output_file ( args )
if os . path . isfile ( ofile ) and not args . force :
raise Exception ( " The file " + ofile + " already exists. You ' re likely overwriting an existing dissector. " )
print ( " Writing output file: " + ofile )
return open ( ofile , " w " ) . write ( buffer )
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2019-11-08 22:55:31 +00:00
def patch_makefile ( args ) :
2023-01-27 15:41:07 +00:00
if args . plugin :
cmakefile = os . path . join ( wsdir ( ) , " CMakeLists.txt " )
patchline = " \t \t plugins/epan/ " + args . protoabbrev
groupstart = " set(PLUGIN_SRC_DIRS "
else :
cmakefile = os . path . join ( wsdir ( ) , " epan/dissectors/CMakeLists.txt " )
patchline = " \t $ {CMAKE_CURRENT_SOURCE_DIR} /packet- " + args . protoabbrev + " .c "
groupstart = " set(DISSECTOR_SRC "
2019-11-08 22:55:31 +00:00
print ( " Patching makefile: " + cmakefile )
output = " "
in_group = False
patched = False
for line in open ( cmakefile ) :
line_strip = line . strip ( )
if in_group and line_strip == " ) " :
in_group = False
if in_group and not patched and line_strip > patchline :
2023-01-27 15:41:07 +00:00
output + = patchline + " \n "
2019-11-08 22:55:31 +00:00
patched = True
2023-01-27 15:41:07 +00:00
if line_strip == groupstart :
2019-11-08 22:55:31 +00:00
in_group = True
if line_strip != patchline :
output + = line
open ( cmakefile , " w " ) . write ( output )
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2023-01-27 15:41:07 +00:00
def write_plugin_makefile ( args ) :
if not args . plugin :
return True
buffer = replace_fields ( read_skeleton ( " CMakeLists-PROTOABBREV.txt " ) , args )
ofile = os . path . join ( output_dir ( args ) , " CMakeLists.txt " )
print ( " Writing output file: " + ofile )
return open ( ofile , " w " ) . write ( buffer )
2019-11-08 22:55:31 +00:00
def print_header ( ) :
print ( " " )
print ( " ************************************************** " )
print ( " * Wireshark skeleton dissector generator * " )
print ( " * * " )
print ( " * Generate a new dissector for your protocol * " )
print ( " * starting from the skeleton provided in the * " )
print ( " * doc directory. * " )
print ( " * * " )
print ( " * Copyright 2019 Dario Lombardo * " )
print ( " ************************************************** " )
print ( " " )
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2019-11-08 22:55:31 +00:00
def print_trailer ( args ) :
print ( " " )
print ( " The skeleton for the dissector of the " + args . protoshortname + " protocol has been generated. " )
print ( " Please review/extend it to match your specific criterias. " )
print ( " " )
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
2019-11-08 22:55:31 +00:00
if __name__ == ' __main__ ' :
print_header ( )
args = parser . parse_args ( )
2023-01-27 15:41:07 +00:00
buffer = replace_fields ( read_skeleton ( " packet-PROTOABBREV.c " ) , args )
2019-11-08 22:55:31 +00:00
write_dissector ( buffer , args )
patch_makefile ( args )
2023-01-27 15:41:07 +00:00
write_plugin_makefile ( args )
2019-11-08 22:55:31 +00:00
print_trailer ( args )