public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] ecos.db generator script in python
@ 2003-09-10 11:03 Peter Soetens
  0 siblings, 0 replies; only message in thread
From: Peter Soetens @ 2003-09-10 11:03 UTC (permalink / raw)
  To: ecos-discuss; +Cc: ecos-patches

[-- Attachment #1: Type: text/plain, Size: 950 bytes --]

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
------------------------------------------------------------------------

[-- Attachment #2: ecosdbgen.py --]
[-- Type: text/x-python, Size: 3558 bytes --]

#!/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()
                   
    

        
        
        
    



[-- Attachment #3: Type: text/plain, Size: 146 bytes --]

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-09-10 11:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-10 11:03 [ECOS] ecos.db generator script in python Peter Soetens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).