From a786d30f5a6d8c145f0aae8e62b3ad2ffb8851cc Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Sat, 1 Sep 2007 18:20:16 -0700 Subject: Refactor gitosis-gitweb to move temp file handling out of main. --- gitosis/gitweb.py | 32 ++++++++++++++++++++++---------- gitosis/test/test_gitweb.py | 14 +++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/gitosis/gitweb.py b/gitosis/gitweb.py index aff0b90..69f6c7d 100644 --- a/gitosis/gitweb.py +++ b/gitosis/gitweb.py @@ -37,7 +37,7 @@ def _escape_filename(s): s = s.replace('"', '\\"') return s -def generate(config, fp): +def generate_fp(config, fp): """ Generate a config file and projects list for ``gitweb``. @@ -94,6 +94,26 @@ def generate(config, fp): line = ' '.join([urllib.quote_plus(s) for s in response]) print >>fp, line +def generate(config, path): + """ + Generate a config file and 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()) + + f = file(tmp, 'w') + try: + generate_fp(config=config, fp=f) + finally: + f.close() + + os.rename(tmp, path) + def _getParser(): import optparse parser = optparse.OptionParser( @@ -120,12 +140,4 @@ def main(): cfg = RawConfigParser() cfg.read(options.config) - tmp = '%s.%d.tmp' % (path, os.getpid()) - - f = file(tmp, 'w') - try: - generate(config=cfg, fp=f) - finally: - f.close() - - os.rename(tmp, path) + generate(config=cfg, path=path) diff --git a/gitosis/test/test_gitweb.py b/gitosis/test/test_gitweb.py index e634d42..61a3d3e 100644 --- a/gitosis/test/test_gitweb.py +++ b/gitosis/test/test_gitweb.py @@ -10,7 +10,7 @@ from gitosis.test.util import mkdir, maketemp def test_projectsList_empty(): cfg = RawConfigParser() got = StringIO() - gitweb.generate( + gitweb.generate_fp( config=cfg, fp=got) eq(got.getvalue(), '''\ @@ -20,7 +20,7 @@ def test_projectsList_repoDenied(): cfg = RawConfigParser() cfg.add_section('repo foo/bar') got = StringIO() - gitweb.generate( + gitweb.generate_fp( config=cfg, fp=got) eq(got.getvalue(), '''\ @@ -31,7 +31,7 @@ def test_projectsList_noOwner(): cfg.add_section('repo foo/bar') cfg.set('repo foo/bar', 'gitweb', 'yes') got = StringIO() - gitweb.generate( + gitweb.generate_fp( config=cfg, fp=got) eq(got.getvalue(), '''\ @@ -44,7 +44,7 @@ def test_projectsList_haveOwner(): cfg.set('repo foo/bar', 'gitweb', 'yes') cfg.set('repo foo/bar', 'owner', 'John Doe') got = StringIO() - gitweb.generate( + gitweb.generate_fp( config=cfg, fp=got) eq(got.getvalue(), '''\ @@ -60,7 +60,7 @@ def test_projectsList_multiple(): cfg.add_section('repo quux') cfg.set('repo quux', 'gitweb', 'yes') got = StringIO() - gitweb.generate( + gitweb.generate_fp( config=cfg, fp=got) eq(got.getvalue(), '''\ @@ -81,7 +81,7 @@ def test_projectsList_multiple_globalGitwebYes(): # this is still hidden cfg.set('repo thud', 'gitweb', 'no') got = StringIO() - gitweb.generate( + gitweb.generate_fp( config=cfg, fp=got) eq(got.getvalue(), '''\ @@ -99,7 +99,7 @@ def test_projectsList_reallyEndsWithGit(): cfg.add_section('repo foo') cfg.set('repo foo', 'gitweb', 'yes') got = StringIO() - gitweb.generate( + gitweb.generate_fp( config=cfg, fp=got) eq(got.getvalue(), '''\ -- cgit v1.2.3