chiark / gitweb /
Only print the progress message if on a TTY
[stgit] / stgit / config.py
1 """Handles the Stacked GIT configuration files
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
20
21 import os, ConfigParser
22 from StringIO import StringIO
23 from stgit import basedir
24
25 config = ConfigParser.RawConfigParser()
26
27 def git_config(filename):
28     """Open a git config file and convert it to be understood by
29     Python."""
30     try:
31         f = file(filename)
32         cont = False
33         lines = []
34         for line in f:
35             line = line.strip()
36
37             if cont:
38                 # continued line, add a space at the beginning
39                 line = ' ' + line
40
41             if line and line[-1] == '\\':
42                 line = line[:-1].rstrip()
43                 cont = True
44             else:
45                 line = line + '\n'
46                 cont = False
47
48             lines.append(line)
49
50         f.close()
51         cfg_str = ''.join(lines)
52     except IOError:
53         cfg_str = ''
54
55     strio = StringIO(cfg_str)
56     strio.name = filename
57
58     return strio
59
60 def config_setup():
61     global config
62
63     # Set the defaults
64     config.add_section('stgit')
65     config.set('stgit', 'autoresolved', 'no')
66     config.set('stgit', 'smtpserver', 'localhost:25')
67     config.set('stgit', 'smtpdelay', '5')
68     config.set('stgit', 'pullcmd', 'git-pull')
69     config.set('stgit', 'merger',
70                'diff3 -L current -L ancestor -L patched -m -E ' \
71                '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"')
72     config.set('stgit', 'keeporig', 'yes')
73     config.set('stgit', 'keepoptimized', 'no')
74     config.set('stgit', 'extensions', '.ancestor .current .patched')
75
76     # Read the configuration files (if any) and override the default settings
77     # stgitrc are read for backward compatibility
78     config.read('/etc/stgitrc')
79     config.read(os.path.expanduser('~/.stgitrc'))
80     config.read(os.path.join(basedir.get(), 'stgitrc'))
81
82     # GIT configuration files can have a [stgit] section
83     config.readfp(git_config(os.path.expanduser('~/.gitconfig')))
84     config.readfp(git_config(os.path.join(basedir.get(), 'config')))
85
86     # Set the PAGER environment to the config value (if any)
87     if config.has_option('stgit', 'pager'):
88         os.environ['PAGER'] = config.get('stgit', 'pager')
89
90     # [gitmergeonefile] section is deprecated. In case it exists copy the
91     # options/values to the [stgit] one
92     if config.has_section('gitmergeonefile'):
93         for option, value in config.items('gitmergeonefile'):
94             config.set('stgit', option, value)
95
96
97 class ConfigOption:
98     """Delayed cached reading of a configuration option.
99     """
100     def __init__(self, section, option):
101         self.__section = section
102         self.__option = option
103         self.__value = None
104
105     def __str__(self):
106         if not self.__value:
107             self.__value = config.get(self.__section, self.__option)
108         return self.__value
109
110
111 # cached extensions
112 __extensions = None
113
114 def file_extensions():
115     """Returns a dictionary with the conflict file extensions
116     """
117     global __extensions
118
119     if not __extensions:
120         cfg_ext = config.get('stgit', 'extensions').split()
121         if len(cfg_ext) != 3:
122             raise CmdException, '"extensions" configuration error'
123
124         __extensions = { 'ancestor': cfg_ext[0],
125                          'current':  cfg_ext[1],
126                          'patched':  cfg_ext[2] }
127
128     return __extensions