aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin H. Johnson <robbat2@gentoo.org>2007-12-15 05:05:54 -0800
committerRobin H. Johnson <robbat2@gentoo.org>2007-12-18 01:12:21 -0800
commit66186bc71cbfeb0dc92add797b08053e5c6b0f70 (patch)
tree49035b20b48e31bac3c7f4978fa8c1b5a1876571
parentUse the new getboolean_default utility function to refactor code. (diff)
downloadgitosis-dakkar-66186bc71cbfeb0dc92add797b08053e5c6b0f70.tar.gz
gitosis-dakkar-66186bc71cbfeb0dc92add797b08053e5c6b0f70.tar.bz2
gitosis-dakkar-66186bc71cbfeb0dc92add797b08053e5c6b0f70.zip
Clean up gitweb.py to pass pylint, including refactoring out duplicate code.
-rw-r--r--gitosis/gitweb.py94
1 files changed, 44 insertions, 50 deletions
diff --git a/gitosis/gitweb.py b/gitosis/gitweb.py
index b4b538b..723cc9c 100644
--- a/gitosis/gitweb.py
+++ b/gitosis/gitweb.py
@@ -30,12 +30,14 @@ import os, urllib, logging
from ConfigParser import NoSectionError, NoOptionError
from gitosis import util
+from gitosis.configutil import getboolean_default
-def _escape_filename(s):
- s = s.replace('\\', '\\\\')
- s = s.replace('$', '\\$')
- s = s.replace('"', '\\"')
- return s
+def _escape_filename(i):
+ """Try to make the filename safer."""
+ i = i.replace('\\', '\\\\')
+ i = i.replace('$', '\\$')
+ i = i.replace('"', '\\"')
+ return i
def generate_project_list_fp(config, fp):
"""
@@ -51,37 +53,21 @@ def generate_project_list_fp(config, fp):
repositories = util.getRepositoryDir(config)
- try:
- global_enable = config.getboolean('gitosis', 'gitweb')
- except (NoSectionError, NoOptionError):
- global_enable = False
+ global_enable = getboolean_default(config, 'gitosis', 'gitweb', False)
for section in config.sections():
- l = section.split(None, 1)
- type_ = l.pop(0)
- if type_ != 'repo':
- continue
- if not l:
+ sectiontitle = section.split(None, 1)
+ if not sectiontitle or sectiontitle[0] != 'repo':
continue
- try:
- enable = config.getboolean(section, 'gitweb')
- except (NoSectionError, NoOptionError):
- enable = global_enable
+ enable = getboolean_default(config, section, 'gitweb', global_enable)
if not enable:
continue
- name, = l
+ name = sectiontitle[1]
- if not os.path.exists(os.path.join(repositories, name)):
- namedotgit = '%s.git' % name
- if os.path.exists(os.path.join(repositories, namedotgit)):
- name = namedotgit
- else:
- log.warning(
- 'Cannot find %(name)r in %(repositories)r'
- % dict(name=name, repositories=repositories))
+ name = _repository_exists(log, repositories, name, name)
response = [name]
try:
@@ -91,8 +77,25 @@ def generate_project_list_fp(config, fp):
else:
response.append(owner)
- line = ' '.join([urllib.quote_plus(s) for s in response])
- print >>fp, line
+ line = ' '.join([urllib.quote_plus(_) for _ in response])
+ print >> fp, line
+
+
+def _repository_exists(log, repositories, name, default_value):
+ """
+ Check if the repository exists by the common name, or with a .git suffix,
+ and return the relative name.
+ """
+ if not os.path.exists(os.path.join(repositories, name)):
+ namedotgit = '%s.git' % name
+ if os.path.exists(os.path.join(repositories, namedotgit)):
+ name = namedotgit
+ else:
+ log.warning(
+ 'Cannot find %(name)r in %(repositories)r'
+ % dict(name=name, repositories=repositories))
+ return default_value
+ return name
def generate_project_list(config, path):
"""
@@ -106,11 +109,11 @@ def generate_project_list(config, path):
"""
tmp = '%s.%d.tmp' % (path, os.getpid())
- f = file(tmp, 'w')
+ fp = file(tmp, 'w')
try:
- generate_project_list_fp(config=config, fp=f)
+ generate_project_list_fp(config=config, fp=fp)
finally:
- f.close()
+ fp.close()
os.rename(tmp, path)
@@ -124,11 +127,8 @@ def set_descriptions(config):
repositories = util.getRepositoryDir(config)
for section in config.sections():
- l = section.split(None, 1)
- type_ = l.pop(0)
- if type_ != 'repo':
- continue
- if not l:
+ sectiontitle = section.split(None, 1)
+ if not sectiontitle or sectiontitle[0] != 'repo':
continue
try:
@@ -139,17 +139,11 @@ def set_descriptions(config):
if not description:
continue
- name, = l
+ name = sectiontitle[1]
- if not os.path.exists(os.path.join(repositories, name)):
- namedotgit = '%s.git' % name
- if os.path.exists(os.path.join(repositories, namedotgit)):
- name = namedotgit
- else:
- log.warning(
- 'Cannot find %(name)r in %(repositories)r'
- % dict(name=name, repositories=repositories))
- continue
+ name = _repository_exists(log, repositories, name, False)
+ if not name:
+ continue
path = os.path.join(
repositories,
@@ -157,9 +151,9 @@ def set_descriptions(config):
'description',
)
tmp = '%s.%d.tmp' % (path, os.getpid())
- f = file(tmp, 'w')
+ fp = file(tmp, 'w')
try:
- print >>f, description
+ print >> fp, description
finally:
- f.close()
+ fp.close()
os.rename(tmp, path)