From eee7283e896c65fbd86d5449886515dec6b7449c Mon Sep 17 00:00:00 2001 From: Catalin Marinas Date: Fri, 17 Nov 2006 22:20:40 +0000 Subject: [PATCH] Only read the config files when running a command This reduces the time to display the help by about 12ms (on my PC). Signed-off-by: Catalin Marinas --- stgit/config.py | 71 ++++++++++++++++++++++++++++++------------------ stgit/gitmergeonefile.py | 10 ++----- stgit/main.py | 6 +++- 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/stgit/config.py b/stgit/config.py index a2e29bb..6679668 100644 --- a/stgit/config.py +++ b/stgit/config.py @@ -25,33 +25,50 @@ from stgit import basedir config = ConfigParser.RawConfigParser() -# Set the defaults -config.add_section('stgit') -config.set('stgit', 'autoresolved', 'no') -config.set('stgit', 'smtpserver', 'localhost:25') -config.set('stgit', 'smtpdelay', '5') -config.set('stgit', 'pullcmd', 'git-pull') -config.set('stgit', 'merger', - 'diff3 -L current -L ancestor -L patched -m -E ' \ - '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"') -config.set('stgit', 'keeporig', 'yes') -config.set('stgit', 'keepoptimized', 'no') -config.set('stgit', 'extensions', '.ancestor .current .patched') - -# Read the configuration files (if any) and override the default settings -config.read('/etc/stgitrc') -config.read(os.path.expanduser('~/.stgitrc')) -config.read(os.path.join(basedir.get(), 'stgitrc')) - -# Set the PAGER environment to the config value (if any) -if config.has_option('stgit', 'pager'): - os.environ['PAGER'] = config.get('stgit', 'pager') - -# [gitmergeonefile] section is deprecated. In case it exists copy the -# options/values to the [stgit] one -if config.has_section('gitmergeonefile'): - for option, value in config.items('gitmergeonefile'): - config.set('stgit', option, value) +def config_setup(): + global config + + # Set the defaults + config.add_section('stgit') + config.set('stgit', 'autoresolved', 'no') + config.set('stgit', 'smtpserver', 'localhost:25') + config.set('stgit', 'smtpdelay', '5') + config.set('stgit', 'pullcmd', 'git-pull') + config.set('stgit', 'merger', + 'diff3 -L current -L ancestor -L patched -m -E ' \ + '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"') + config.set('stgit', 'keeporig', 'yes') + config.set('stgit', 'keepoptimized', 'no') + config.set('stgit', 'extensions', '.ancestor .current .patched') + + # Read the configuration files (if any) and override the default settings + config.read('/etc/stgitrc') + config.read(os.path.expanduser('~/.stgitrc')) + config.read(os.path.join(basedir.get(), 'stgitrc')) + + # Set the PAGER environment to the config value (if any) + if config.has_option('stgit', 'pager'): + os.environ['PAGER'] = config.get('stgit', 'pager') + + # [gitmergeonefile] section is deprecated. In case it exists copy the + # options/values to the [stgit] one + if config.has_section('gitmergeonefile'): + for option, value in config.items('gitmergeonefile'): + config.set('stgit', option, value) + + +class ConfigOption: + """Delayed cached reading of a configuration option. + """ + def __init__(self, section, option): + self.__section = section + self.__option = option + self.__value = None + + def __str__(self): + if not self.__value: + self.__value = config.get(self.__section, self.__option) + return self.__value # cached extensions diff --git a/stgit/gitmergeonefile.py b/stgit/gitmergeonefile.py index 47ad8b8..39915fb 100644 --- a/stgit/gitmergeonefile.py +++ b/stgit/gitmergeonefile.py @@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sys, os from stgit import basedir -from stgit.config import config, file_extensions +from stgit.config import file_extensions, ConfigOption from stgit.utils import append_string @@ -31,12 +31,8 @@ class GitMergeException(Exception): # # Options # -try: - merger = config.get('stgit', 'merger') - keeporig = config.get('stgit', 'keeporig') -except Exception, err: - raise GitMergeException, 'Configuration error: %s' % err - +merger = ConfigOption('stgit', 'merger') +keeporig = ConfigOption('stgit', 'keeporig') # # Utility functions diff --git a/stgit/main.py b/stgit/main.py index 60df5ef..c2797c9 100644 --- a/stgit/main.py +++ b/stgit/main.py @@ -220,12 +220,16 @@ def main(): # These modules are only used from this point onwards and do not # need to be imported earlier + from stgit.config import config_setup + from ConfigParser import ParsingError from stgit.stack import Series, StackException from stgit.git import GitException from stgit.commands.common import CmdException from stgit.gitmergeonefile import GitMergeException try: + config_setup() + # 'clone' doesn't expect an already initialised GIT tree. A Series # object will be created after the GIT tree is cloned if cmd != 'clone': @@ -236,7 +240,7 @@ def main(): stgit.commands.common.crt_series = command.crt_series command.func(parser, options, args) - except (IOError, CmdException, StackException, GitException, + except (IOError, ParsingError, CmdException, StackException, GitException, GitMergeException), err: print >> sys.stderr, '%s %s: %s' % (prog, cmd, err) sys.exit(2) -- 2.11.0