• Immutable Page
  • Info
  • Attachments

StructDumper

StructDumper

Rough python script to dump structs from header files to .html.

Endless room to improve, no ego on the part of the developer.

But enough of that.

#!/usr/bin/python """Version: 0.1

  • Description:

    • Script to descend selected paths in source tree and dump structure definitions to single HTML page. We rely heavily on properly formatted code, and appreciate the effort spent keeping the codebase tidy, so that these regular expressions can be trivial.

    Usage:

    1. Edit the all_your_paths list right after the includes.

      • One day, this script will reference the .config. But you may want to constrain it, anyway.

    2. Edit the output file path. Obviously these will become command-line

      • parameters.

    3. Execute script, open output file in web browser (even lynx!).

    Thoughts:

    1. With all three paths below, there are ~2300 structs. So there

      • needs to be some thought given to organizing.

    2. It would be nice to format the output a little more consistently,

      • but who wants to write a C parser?

    3. It would also be cool to detect when structs embed each other, and

      • then record some dependency information.

    License: GPL v2 Copyright 2006, Christopher L. Smith, smitty1elkml@gmail.com

"""

import re

import os

import tempfile

from os.path import join

#Edit the desired list of structs to this list.

root = "/usr/src/linux/"

all_your_paths = []

all_your_paths.append( root + 'arch/i386' )

#all_your_paths.append( root + 'fs/ext3' )

#all_your_paths.append( root + 'include/linux' )

tmp_file = []

output = open( "/mnt/dmz/proj/allstructs.htm", "w" )

FAT_TAB = "  " #      "

COLUMN_COUNT = 4

#File scanning regexes

re_h = re.compile("\.h$" ) #a header file

re_s = re.compile("^(struct)\s*([\w|_]*)(.*{$)") #a structure

re_c = re.compile("^}.*;$") #end of a struct

struct_names = {} #index builder

long_name = 0

struct_counter = 1

def print_struct( file_handle, i ):

  • """Keep printing out lines until re_c strikes gold, and return

    • the line on which it does.

    """

    #wikis are fun

    for line in file_handle:

    • if re_c.search( line ):

      • return line[ 0:len(line) - 1 ]

      else:

      • tmp_file.append( str(line[ 0:len(line) - 1 ]).replace( "\t"

        • , FAT_TAB )

        • + "<br>" )

      i += 1

def scan_file( path_name, file_handle, i ):

  • """Walk FILE_HANDLE, dumping structures to TMP_FILE.

    """

    #wikis are fun

    global long_name

    global struct_counter

    for line in file_handle:

    • m = re_s.match( line )

      if ( m ):

      • the_name = m.group(2)

        print the_name

        if not the_name in struct_names:

        • struct_names[the_name] = 0

          if ( len(the_name) > long_name ):

          • long_name = len(the_name)

        else:

        • struct_names[the_name] += 1

        tmp_file.append( "<br>file: %s line: %s<br>".replace( "\t"

        • , FAT_TAB ) \

        • % ( path_name, i ) )

        #tmp_file.append( "%s.<br>" \

        # % struct_counter )

        struct_counter += 1

        tmp_file.append( "%s <a name=%s><b>%s</b></a>%s<br>".replace( "\t"

        • , FAT_TAB ) \

        • % ( m.group(1)

          • , m.group(2)

            , m.group(2)

            , m.group(3) ))

        try:

        • tmp_file.append( print_struct( file_handle, i ) + "<br>" )

        except:

        • pass

      i += 1

def descend_path( start_here ):

  • """START_HERE is the root of the tree descent to look for structures.

    • We blindly depend on good code formatting discipline for this to work.

    """

    #wikis are fun

    for root, dirs, files in os.walk( start_here ):

    • for name in files:

      • if re_h.search( name ):

        • p = join( root , name )

          f = open( p , 'r' )

          i = 1

          scan_file( p, f, i )

          f.close()

def format_struct_names( key_list, long_name ):

  • """After all of the processing is done, we take the list of keys,

    • sort it, and print it to the output file as hyperlinks.

    """

    #wikis are fun

    key_list.sort()

    structs_per_col = len(key_list) // COLUMN_COUNT

    overflow_cols = len(key_list) % COLUMN_COUNT

    index = 0

    running_total = 0

    fib = ""

    output.write( "<table><tr valign='top'><td>" )

    for col_counter in range( 0, COLUMN_COUNT ):

    • column_chunk = structs_per_col

      fib = "<br>"

      if ( col_counter < overflow_cols ):

      • column_chunk += 1

        fib = ""

      for index in range( 0, column_chunk ):

      • output.write( "<a href='#%s'>%s</a><br>" \

        • % ( key_list[index + running_total]

          • , key_list[index + running_total] ))

      running_total = running_total + index + 1

      output.write( "</td><td>" )

    output.write( "</td></tr></table><br>" )

if name == "main":

  • [descend_path( this_path ) for this_path in all_your_paths]

    output.write( "<html><body>" )

    key_list = struct_names.keys()

    output.write( "list length: %s<br>" % len(key_list) )

    format_struct_names( key_list, long_name )

    output.write( str(tmp_file).replace("', '", "") ) #spurious and

    output.write( "</body></html>" )

    output.close()

Tell others about this page:

last edited 2006-12-15 03:14:42 by ip24-255-116-107