From 66186bc71cbfeb0dc92add797b08053e5c6b0f70 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sat, 15 Dec 2007 05:05:54 -0800 Subject: Clean up gitweb.py to pass pylint, including refactoring out duplicate code. --- gitosis/gitweb.py | 94 ++++++++++++++++++++++++++----------------------------- 1 file 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) -- cgit v1.2.3