views:

37

answers:

2

Is it possible to do something like using the trac module in Python in order to get a listing of all of the projects in a given directory? If possible it would be nice to get things like the description, too. I can't seem to figure out how to do it.

Thanks for any help you can provide.

+1  A: 

Using the TRAC_ENV_PARENT_DIR environment variable almost certainly does what you need: it will create an index page for you. See the part about "use multiple projects" here: http://trac.edgewall.org/wiki/TracCgi#Apacheweb-serverconfiguration

Ivo
+1  A: 

You can script something like this:

import trac.admin.console
import trac.config
import os

def _get_project_options(envdir):
    confpath = os.path.join(envdir, 'conf/trac.ini')
    config = trac.config.Configuration(confpath)
    return dict([x for x in config.options(u'project')])

def _get_project_name(envdir):
    admin = trac.admin.console.TracAdmin(envdir)
    if admin.env_check():
        options = _get_project_options(envdir)
        return options[u'name']
    else:
        return None

def iter_trac_projects_from_dir(dirname):
    for which in os.listdir(dirname):
        if not which in ('.', '..') and os.path.isdir(dirname):
            envdirname = os.path.join(dirname, which)
            project_name = _get_project_name(envdirname)
            if project_name:
                yield (project_name, envdirname)

def get_trac_projects_from_dir(dirname):
    return [pr for pr in iter_trac_projects_from_dir(dirname)]

Then you can use either iter_trac_projects_from_dir or get_trac_projects_from_dir whichever you think is best for you.

Alternatively you could use the function get_enviroments from module trac.web.main but only as alternative to os.listdir -- you would still have to check whether or not each alleged env is really a trac environment. See why:

>>> import trac.web.main
>>> env = {'trac.env_parent_dir': 
...        '/home/manu/tmp'}
>>> trac.web.main.get_environments(env)
{'test': '/home/manu/tmp/test', 'no-a-real-trac-project': '/home/manu/tmp/no-a-real-trac-project', 'test2': '/home/manu/tmp/test2'}
manu
Notice that the _get_project_options returns all the options. You can get the description there too.
manu