From 3a721a3a8eedef123f0eaeaffc38dd83632110ba Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Thu, 15 Jan 2009 15:56:23 +0100 Subject: added support for cgit (tested) --- gitosis/cgit.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ gitosis/run_hook.py | 5 +++ 2 files changed, 112 insertions(+) create mode 100644 gitosis/cgit.py diff --git a/gitosis/cgit.py b/gitosis/cgit.py new file mode 100644 index 0000000..0e85154 --- /dev/null +++ b/gitosis/cgit.py @@ -0,0 +1,107 @@ +""" +Generate ``cgit`` project list based on ``gitosis.conf``. + +To plug this into ``cgit``, put in your global config file the +following line:: + + include=/path/to/your/repos.list +""" + +import os, urllib, logging + +from ConfigParser import NoSectionError, NoOptionError + +from gitosis import util +from gitosis.configutil import getboolean_default + +field_map={'description':'repo.desc', + 'owner':'repo.owner', + 'readme':'repo.readme', + } + +def generate_project_list_fp(config, fp): + """ + Generate projects list for ``cgit``. + + :param config: configuration to read projects from + :type config: RawConfigParser + + :param fp: writable for ``repos.list`` + :type fp: (file-like, anything with ``.write(data)``) + """ + log = logging.getLogger('gitosis.cgit.generate_projects_list') + + repositories = util.getRepositoryDir(config) + + global_enable = getboolean_default(config, 'gitosis', 'cgit', False) + + print >> fp, '# path: %s, global: %d'%(repositories,global_enable) + + for section in config.sections(): + sectiontitle = section.split(None, 1) + if not sectiontitle or sectiontitle[0] != 'repo': + continue + + enable = getboolean_default(config, section, 'cgit', global_enable) + + print >> fp, '#section: %s, local: %d'%(sectiontitle[1],enable) + + if not enable: + continue + + name = sectiontitle[1] + + fullpath = _repository_path(log, repositories, name, name) + + print >> fp, 'repo.url=%s'%(name) + + if fullpath is None: + continue + + print >> fp, 'repo.path=%s'%(fullpath) + + for field_pair in field_map.iteritems(): + try: + field_value = config.get(section, field_pair[0]) + except (NoSectionError, NoOptionError): + continue + else: + print >> fp, '%s=%s'%(field_pair[1],field_value) + +def _repository_path(log, repositories, name, default_value): + """ + Check if the repository exists by the common name, or with a .git suffix, + and return the full pathname. + """ + fullpath=os.path.join(repositories, name) + if not os.path.exists(fullpath): + namedotgit = '%s.git' % name + fullpath=os.path.join(repositories, namedotgit) + if os.path.exists(fullpath): + return fullpath + else: + log.warning( + 'Cannot find %(name)r in %(repositories)r' + % dict(name=name, repositories=repositories)) + return None + return fullpath + +def generate_project_list(config, path): + """ + Generate projects list for ``gitweb``. + + :param config: configuration to read projects from + :type config: RawConfigParser + + :param path: path to write projects list to + :type path: str + """ + tmp = '%s.%d.tmp' % (path, os.getpid()) + + fp = file(tmp, 'w') + try: + generate_project_list_fp(config=config, fp=fp) + finally: + fp.close() + + os.rename(tmp, path) diff --git a/gitosis/run_hook.py b/gitosis/run_hook.py index ef12310..99d8b70 100644 --- a/gitosis/run_hook.py +++ b/gitosis/run_hook.py @@ -9,6 +9,7 @@ import sys from gitosis import repository from gitosis import ssh from gitosis import gitweb +from gitosis import cgit from gitosis import gitdaemon from gitosis import app from gitosis import util @@ -29,6 +30,10 @@ def build_reposistory_data(config): config=config, path=os.path.join(generated, 'projects.list'), ) + cgit.generate_project_list( + config=config, + path=os.path.join(generated, 'repos.list'), + ) gitdaemon.set_export_ok( config=config, ) -- cgit v1.2.3