This is the mail archive of the ecos-patches@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

ecos.db generator script in python


Hi,

I have written a script to automatically generate an ecos.db script to std 
output. It is written in python. It will process all packages cdl files 
recursively in the current directory (eg packages/ ) or can be given one 
argument to another path. it can be used from within a (sub-)package too. 

Any suggestions are welcome.

http://people.mech.kuleuven.ac.be/~psoetens/ecos/ecosdbgen.py

Peter

-- 
------------------------------------------------------------------------
Peter Soetens                                      http://www.orocos.org
Katholieke Universiteit Leuven
Division Production Engineering,                      tel. +32 16 322773
Machine Design and Automation                         fax. +32 16 322987
Celestijnenlaan 300B                   peter.soetens@mech.kuleuven.ac.be
B-3001 Leuven Belgium                 http://www.mech.kuleuven.ac.be/pma
------------------------------------------------------------------------
#!/usr/bin/python
#
# A script to generate an ecos.db file from an existing package directory
# ecosdbgen.py (c) 2003 Peter Soetens
#
# ***************************************************************************
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU General Public License as published by  *
# *   the Free Software Foundation; either version 2 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# ***************************************************************************/
"""
File : ecosdbgen.py

Usage : python ecosdbgen.py [path/to/repository/root]

The current directory is processed if no path is given and output is generated
on stdout.
"""


import os
import re
import sys

def package_factory(arg, dirname, filelist):
    "Create package found in dirname (if any)"
    pat = re.compile(r"\w+\.cdl$")
    for fn in filelist:
        if pat.match(fn) :
            p = Package()
            if p.parse_script(dirname, fn):
                arg.add_package(p)
    

class EcosDB :
    "Our own db of found packages"

    def __init__(self, out = sys.stdout ) :
        self.List = []
        self.out = out
        
    def add_package(self, pack):
        self.List.append(pack)

    def print_packages(self):
        self.out.write("#\n#  Autogenerated ecos.db by ecosdbgen.py\n")
        self.out.write("#  ecosdbgen.py is (c) 2003 by Peter Soetens \n\n")
        for pkg in self.List:
            pkg.print_output( self.out )
            self.out.write("\n")
        
    def find_cdl_files(self, root_dir ):
        os.path.walk( root_dir, package_factory, self)

    

class Package :
    "The contents of an ecos package"
    def __init__(self) :
        pass
    
    def parse_script(self, _dir, _script) :
        f = open( _dir + '/' + _script, 'r')
        self.script = _script
        self.directory = _dir[ :_dir.find("current")-1 ]
        name_pat = re.compile(r"\s*cdl_package\s(\w*)\s{\s*") # extract the name
        disp_pat = re.compile(r"(display\s\"(.*)\")") # extract the display message
        descr_pat = re.compile(r"\s*description\s\"(.*)\"") # extract the description
        alias_pat = re.compile(r"\w\w\wPKG_(\w+)") # extract the alias
        text = f.read() # the whole file
        
        res = name_pat.search(text)
        if res :
            self.name    = res.group(1)
        else:
            return None

        res = disp_pat.search( text )
        if res :
            self.display = res.group(2)
        else:
            self.display = "Display Property not found"

        res = alias_pat.search( self.name )
        if res :
            self.alias   = res.group(1).lower()
        
        return self

        # TODO : Description, is this needed ?

    def print_output(self, _out=sys.stdout):
        _out.write("package "+self.name+" {\n")
        _out.write("  alias { \""+self.display+"\" "+self.alias+" }\n")
        _out.write("  directory "+self.directory+"\n")
        _out.write("  script "+self.script+"\n")
        _out.write("}\n")


# Script start

mydb = EcosDB()

# find all packages in "."
if ( len(sys.argv) == 2 ) :
    path = sys.argv[1]
else:
    path = "."

mydb.find_cdl_files( path )

#print them to stdout
mydb.print_packages()
                   
    

        
        
        
    


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]