aboutsummaryrefslogtreecommitdiff
path: root/gitosis/cgit.py
blob: c98c2a2913a8b3a6e952c158d99de8d7de12a29d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
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 osurlliblogging
 
from ConfigParser import NoSectionErrorNoOptionError
 
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(None1)
        if not sectiontitle or sectiontitle[0] != 'repo':
            continue
 
        enable = getboolean_default(configsection'cgit'global_enable)
 
        if not enable:
            continue
        groupname = get_default(configsection'cgit_group'"")
        grouped_section.setdefault(groupname,[]).append(section)
 
    for groupnamegroup in grouped_sections.iteritems():
        if groupname
            print >> fp'repo.group=%s'%(groupname)
 
        for section in group:
            sectiontitle = section.split(None1)
            print >> fp'#section: %s, local: %d'%(sectiontitle[1],enable)
 
            name = sectiontitle[1]
 
            fullpath = _repository_path(logrepositoriesnamename)
 
            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(sectionfield_pair[0])
                except (NoSectionErrorNoOptionError):
                    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(repositoriesname)
    if not os.path.exists(fullpath):
        namedotgit = '%s.git' % name
        fullpath=os.path.join(repositoriesnamedotgit)
        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' % (pathos.getpid())
 
    fp = file(tmp'w')
    try:
        generate_project_list_fp(config=config, fp=fp)
    finally:
        fp.close()
 
    os.rename(tmppath)