Merge 0.4 documentation updates.
[jelmer/subvertpy.git] / config.py
1 # Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 """Stores per-repository settings."""
17
18 from bzrlib import osutils
19 from bzrlib.config import IniBasedConfig, config_dir, ensure_config_dir_exists, GlobalConfig
20
21 import os
22
23 from scheme import BranchingScheme
24
25 # Settings are stored by UUID. 
26 # Data stored includes default branching scheme and locations the repository 
27 # was seen at.
28
29 def subversion_config_filename():
30     """Return per-user configuration ini file filename."""
31     return osutils.pathjoin(config_dir(), 'subversion.conf')
32
33
34 class SvnRepositoryConfig(IniBasedConfig):
35     """Per-repository settings."""
36
37     def __init__(self, uuid):
38         name_generator = subversion_config_filename
39         super(SvnRepositoryConfig, self).__init__(name_generator)
40         self.uuid = uuid
41         if not self.uuid in self._get_parser():
42             self._get_parser()[self.uuid] = {}
43
44     def set_branching_scheme(self, scheme, mandatory=False):
45         """Change the branching scheme.
46
47         :param scheme: New branching scheme.
48         """
49         self.set_user_option('branching-scheme', str(scheme))
50         self.set_user_option('branching-scheme-mandatory', str(mandatory))
51
52     def _get_user_option(self, name, use_global=True):
53         try:
54             return self._get_parser()[self.uuid][name]
55         except KeyError:
56             if not use_global:
57                 return None
58             return GlobalConfig()._get_user_option(name)
59
60     def get_branching_scheme(self):
61         """Get the branching scheme.
62
63         :return: BranchingScheme instance.
64         """
65         schemename = self._get_user_option("branching-scheme", use_global=False)
66         if schemename is not None:
67             return BranchingScheme.find_scheme(schemename.encode('ascii'))
68         return None
69
70     def get_set_revprops(self):
71         """Check whether or not bzr-svn should attempt to store Bazaar
72         revision properties in Subversion revision properties during commit."""
73         try:
74             return self._get_parser().get_bool(self.uuid, "set-revprops")
75         except KeyError:
76             return None
77
78     def get_supports_change_revprop(self):
79         """Check whether or not the repository supports changing existing 
80         revision properties."""
81         try:
82             return self._get_parser().get_bool(self.uuid, "supports-change-revprop")
83         except KeyError:
84             return None
85
86     def branching_scheme_is_mandatory(self):
87         """Check whether or not the branching scheme for this repository 
88         is mandatory.
89         """
90         try:
91             return self._get_parser().get_bool(self.uuid, "branching-scheme-mandatory")
92         except KeyError:
93             return False
94
95     def get_override_svn_revprops(self):
96         """Check whether or not bzr-svn should attempt to override Subversion revision 
97         properties after committing."""
98         try:
99             return self._get_parser().get_bool(self.uuid, "override-svn-revprops")
100         except KeyError:
101             pass
102         global_config = GlobalConfig()
103         try:
104             return global_config._get_parser().get_bool(global_config._get_section(), "override-svn-revprops")
105         except KeyError:
106             return None
107
108     def get_locations(self):
109         """Find the locations this repository has been seen at.
110
111         :return: Set with URLs.
112         """
113         val = self._get_user_option("locations", use_global=False)
114         if val is None:
115             return set()
116         return set(val.split(";"))
117
118     def add_location(self, location):
119         """Add a location for this repository.
120
121         :param location: URL of location to add.
122         """
123         locations = self.get_locations()
124         locations.add(location.rstrip("/"))
125         self.set_user_option('locations', ";".join(list(locations)))
126
127     def set_user_option(self, name, value):
128         """Change a user option.
129
130         :param name: Name of the option.
131         :param value: Value of the option.
132         """
133         conf_dir = os.path.dirname(self._get_filename())
134         ensure_config_dir_exists(conf_dir)
135         self._get_parser()[self.uuid][name] = value
136         f = open(self._get_filename(), 'wb')
137         self._get_parser().write(f)
138         f.close()