~barry/bzr-builddeb/609186-urls

18.1.39 by James Westby
Place the package under the GPL
1
#    config.py -- Configuration of bzr-builddeb from files
18.1.43 by James Westby
Correct (C) statements. Add copyright to setup.py
2
#    Copyright (C) 2006 James Westby <jw+debian@jameswestby.net>
18.1.39 by James Westby
Place the package under the GPL
3
#    
4
#    This file is part of bzr-builddeb.
5
#
75.1.2 by Jelmer Vernooij
Fix some typos
6
#    bzr-builddeb is free software; you can redistribute it and/or modify
18.1.39 by James Westby
Place the package under the GPL
7
#    it under the terms of the GNU General Public License as published by
8
#    the Free Software Foundation; either version 2 of the License, or
9
#    (at your option) any later version.
10
#
11
#    bzr-builddeb 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 bzr-builddeb; if not, write to the Free Software
18
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
18.1.35 by James Westby
Refactored code to use multiple files.
20
327.2.3 by James Westby
Clean up from pyflakes
21
from bzrlib.config import ConfigObj, TreeConfig
327.1.5 by James Westby
Warn when a key is defined outside of any sections in a config file.
22
from bzrlib.trace import mutter, warning
402.1.1 by Jelmer Vernooij
Fall back to plain configobj when bzr doesn't ship with its own copy of configobj.
23
try:
24
  from bzrlib.util.configobj.configobj import ParseError
25
except ImportError:
26
  from configobj import ParseError
18.1.35 by James Westby
Refactored code to use multiple files.
27
28
297.4.2 by Jelmer Vernooij
Fix mergeWithUpstream.
29
class SvnBuildPackageMappedConfig(object):
30
  """Config object that provides a bzr-builddeb configuration 
31
  based on a svn-buildpackage configuration.
32
  """
33
297.4.3 by Jelmer Vernooij
Add tests for svn-buildpackage compatibility.
34
  def __init__(self, bp_config):
35
    self.bp_config = bp_config
297.4.2 by Jelmer Vernooij
Fix mergeWithUpstream.
36
37
  def get_option(self, option, section=None):
38
    """Retrieve the contents of an option, mapped from the equivalent 
39
    svn-buildpackage option."""
40
    if section == "BUILDDEB":
41
      if option == "merge":
297.4.3 by Jelmer Vernooij
Add tests for svn-buildpackage compatibility.
42
        return self.bp_config.get_merge_with_upstream()
297.4.2 by Jelmer Vernooij
Fix mergeWithUpstream.
43
      elif option == "orig-dir":
44
        return self.bp_config.get("origDir")
45
      elif option == "build-dir":
46
        return self.bp_config.get("buildArea")
47
    return None
48
49
18.1.35 by James Westby
Refactored code to use multiple files.
50
class DebBuildConfig(object):
51
  """Holds the configuration settings for builddeb. These are taken from
52
  a hierarchy of config files. .bzr-builddeb/local.conf then 
53
  ~/.bazaar/builddeb.conf, finally .bzr-builddeb/default.conf. The value is 
54
  taken from the first file in which it is specified."""
55
97.1.1 by James Westby
Important: Deprecate the local.conf file in favour of branch.conf.
56
  section = 'BUILDDEB'
57
327.2.7 by James Westby
Move to using SourceDistiller.
58
  def __init__(self, files, branch=None, tree=None):
18.1.37 by James Westby
Add a few doctests and supporting files. move logging.py to bdlogging.py dues to a possible name clash
59
    """ 
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
60
    Creates a config to read from config files in a hierarchy.
61
97.1.1 by James Westby
Important: Deprecate the local.conf file in favour of branch.conf.
62
    Pass it a list of tuples (file, secure) where file is the location of
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
63
    a config file (that doesn't have to exist, and trusted is True or false,
97.1.1 by James Westby
Important: Deprecate the local.conf file in favour of branch.conf.
64
    and states whether the file can be trusted for sensitive values.
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
65
66
    The value will be returned from the first in the list that has it,
67
    unless that key is marked as needing a trusted file and the file isn't
68
    trusted.
69
97.1.1 by James Westby
Important: Deprecate the local.conf file in favour of branch.conf.
70
    If branch is not None then it will be used in preference to all others.
71
    It will not be considered trusted.
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
72
320 by Martin Pool
Remove the requirement to run the tests from the place where the sample data is.
73
    The sample files used in this test are included in the builddeb source tree.
74
75
    >>> import os
76
    >>> import bzrlib.plugins.builddeb
77
    >>> d = os.path.dirname(bzrlib.plugins.builddeb.__file__) + '/'
78
    >>> c = DebBuildConfig([
79
    ...      (d + 'local.conf', False),
80
    ...      (d + 'user.conf', True), 
81
    ...      (d + 'default.conf', False)])
69.1.8 by James Westby
* Switch to properties in the config object.
82
    >>> print c.orig_dir
18.1.37 by James Westby
Add a few doctests and supporting files. move logging.py to bdlogging.py dues to a possible name clash
83
    None
69.1.8 by James Westby
* Switch to properties in the config object.
84
    >>> print c.merge
18.1.37 by James Westby
Add a few doctests and supporting files. move logging.py to bdlogging.py dues to a possible name clash
85
    True
69.1.8 by James Westby
* Switch to properties in the config object.
86
    >>> print c.build_dir
18.1.37 by James Westby
Add a few doctests and supporting files. move logging.py to bdlogging.py dues to a possible name clash
87
    defaultbuild
69.1.8 by James Westby
* Switch to properties in the config object.
88
    >>> print c.result_dir
18.1.37 by James Westby
Add a few doctests and supporting files. move logging.py to bdlogging.py dues to a possible name clash
89
    userresult
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
90
    >>> print c.builder
91
    userbuild
18.1.37 by James Westby
Add a few doctests and supporting files. move logging.py to bdlogging.py dues to a possible name clash
92
    """
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
93
    self._config_files = []
97.1.1 by James Westby
Important: Deprecate the local.conf file in favour of branch.conf.
94
    for input in files:
327.1.35 by James Westby
Catch ParseError from ConfigObj to avoid tracebacks.
95
      try:
96
        config = ConfigObj(input[0])
97
      except ParseError, e:
98
        if len(input) > 2:
99
          content = input[2]
100
        else:
101
          content = input[0]
102
        warning("There was an error parsing '%s': %s" % (content, e.msg))
103
        continue
327.1.5 by James Westby
Warn when a key is defined outside of any sections in a config file.
104
      if len(input) > 2:
105
        config.filename = input[2]
178.1.22 by James Westby
Support $UPSTREAM_VERSION in the export-upstream-revision config option.
106
      self._config_files.append((config, input[1]))
104.1.6 by James Westby
If the branch is None then do not try and create a TreeConfig from it.
107
    if branch is not None:
108
      self._branch_config = TreeConfig(branch)
109
    else:
110
      self._branch_config = None
297.4.1 by Jelmer Vernooij
Add ability to use tree-based configuration in DebBuildConfig.
111
    self._tree_config = None
297.4.2 by Jelmer Vernooij
Fix mergeWithUpstream.
112
    if tree is not None:
113
      try:
114
        # Imported here, since not everybody will have bzr-svn installed
115
        from bzrlib.plugins.svn.config import SubversionBuildPackageConfig, NoSubversionBuildPackageConfig
116
        try:
117
          self._tree_config = SvnBuildPackageMappedConfig(SubversionBuildPackageConfig(tree))
118
        except NoSubversionBuildPackageConfig:
119
          pass # Not a svn tree
120
      except ImportError:
121
        pass # No svn, apparently
226.2.12 by James Westby
Default to doing everything in ./build-area/ when building a remote branch
122
    self.user_config = None
123
124
  def set_user_config(self, user_conf):
125
    if user_conf is not None:
126
      self.user_config = ConfigObj(user_conf)
127
128
  def _user_config_value(self, key):
129
    if self.user_config is not None:
130
      try:
131
        return self.user_config.get_value(self.section, key)
132
      except KeyError:
133
        pass
134
    return None
18.1.35 by James Westby
Refactored code to use multiple files.
135
178.1.13 by James Westby
Internal hook execution support.
136
  def _get_opt(self, config, key, section=None):
18.1.35 by James Westby
Refactored code to use multiple files.
137
    """Returns the value for key from config, of None if it is not defined in 
138
    the file"""
178.1.13 by James Westby
Internal hook execution support.
139
    if section is None:
140
      section = self.section
18.1.35 by James Westby
Refactored code to use multiple files.
141
    try:
178.1.13 by James Westby
Internal hook execution support.
142
      return config.get_value(section, key)
18.1.35 by James Westby
Refactored code to use multiple files.
143
    except KeyError:
327.1.5 by James Westby
Warn when a key is defined outside of any sections in a config file.
144
      pass
145
    if config.filename is not None:
146
      try:
147
        value = config[key]
148
        warning("'%s' defines a value for '%s', but it is not in a '%s' "
149
             "section, so it is ignored" % (config.filename, key, section))
150
      except KeyError:
151
        pass
152
    return None
18.1.35 by James Westby
Refactored code to use multiple files.
153
178.1.13 by James Westby
Internal hook execution support.
154
  def _get_best_opt(self, key, trusted=False, section=None):
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
155
    """Returns the value for key, obeying precedence.
156
    
157
    Returns the value for the key from the first file in which it is defined,
158
    or None if none of the files define it.
159
    
160
    If trusted is True then the the value will only be taken from a file
161
    marked as trusted.
162
    
163
    """
178.1.13 by James Westby
Internal hook execution support.
164
    if section is None:
165
      section = self.section
297.4.1 by Jelmer Vernooij
Add ability to use tree-based configuration in DebBuildConfig.
166
    if not trusted:
167
      if self._branch_config is not None:
97.1.1 by James Westby
Important: Deprecate the local.conf file in favour of branch.conf.
168
        value = self._branch_config.get_option(key, section=self.section)
169
        if value is not None:
170
          mutter("Using %s for %s, taken from the branch", value, key)
171
          return value
297.4.1 by Jelmer Vernooij
Add ability to use tree-based configuration in DebBuildConfig.
172
      if self._tree_config is not None:
173
        value = self._tree_config.get_option(key, section=self.section)
174
        if value is not None:
175
          mutter("Using %s for %s, taken from the tree", value, key)
176
          return value
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
177
    for config_file in self._config_files:
178
      if not trusted or config_file[1]:
178.1.13 by James Westby
Internal hook execution support.
179
        value = self._get_opt(config_file[0], key, section=section)
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
180
        if value is not None:
181
          mutter("Using %s for %s, taken from %s", value, key,
182
                 config_file[0].filename)
183
          return value
18.1.35 by James Westby
Refactored code to use multiple files.
184
    return None
185
178.1.13 by James Westby
Internal hook execution support.
186
  def get_hook(self, hook_name):
187
    return self._get_best_opt(hook_name, section='HOOKS')
188
18.1.35 by James Westby
Refactored code to use multiple files.
189
  def _get_bool(self, config, key):
190
    try:
191
      return True, config.get_bool('BUILDDEB', key)
192
    except KeyError:
327.1.5 by James Westby
Warn when a key is defined outside of any sections in a config file.
193
      pass
194
    if config.filename is not None:
195
      try:
196
        value = config.as_bool(key)
197
        warning("'%s' defines a value for '%s', but it is not in a 'BUILDDEB' "
198
             "section, so it is ignored" % (config.filename, key))
199
      except KeyError:
200
        pass
201
    return False, False
18.1.35 by James Westby
Refactored code to use multiple files.
202
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
203
  def _get_best_bool(self, key, trusted=False, default=False):
204
    """Returns the value of key, obeying precedence.
205
206
    Returns the value for the key from the first file in which it is defined,
207
    or default if none of the files define it.
208
    
209
    If trusted is True then the the value will only be taken from a file
210
    marked as trusted.
211
    
212
    """
297.4.1 by Jelmer Vernooij
Add ability to use tree-based configuration in DebBuildConfig.
213
    if not trusted:
214
      if self._branch_config is not None:
97.1.1 by James Westby
Important: Deprecate the local.conf file in favour of branch.conf.
215
        value = self._branch_config.get_option(key, section=self.section)
216
        if value is not None:
217
          mutter("Using %s for %s, taken from the branch", value, key)
218
          return value
297.4.1 by Jelmer Vernooij
Add ability to use tree-based configuration in DebBuildConfig.
219
      if self._tree_config is not None:
220
        value = self._tree_config.get_option(key, section=self.section)
221
        if value is not None:
222
          mutter("Using %s for %s, taken from the tree", value, key)
223
          return value
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
224
    for config_file in self._config_files:
225
      if not trusted or config_file[1]:
226
        (found, value) = self._get_bool(config_file[0], key)
227
        if found:
228
          mutter("Using %s for %s, taken from %s", value, key,
229
                 config_file[0].filename)
230
          return value
18.1.35 by James Westby
Refactored code to use multiple files.
231
    return default
232
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
233
  def _opt_property(name, help=None, trusted=False):
234
    return property(lambda self: self._get_best_opt(name, trusted), None,
235
                    None, help)
69.1.8 by James Westby
* Switch to properties in the config object.
236
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
237
  def _bool_property(name, help=None, trusted=False, default=False):
238
    return property(lambda self: self._get_best_bool(name, trusted, default),
69.1.8 by James Westby
* Switch to properties in the config object.
239
                    None, None, help)
240
241
  build_dir = _opt_property('build-dir', "The dir to build in")
242
226.2.12 by James Westby
Default to doing everything in ./build-area/ when building a remote branch
243
  user_build_dir = property(
244
          lambda self: self._user_config_value('build-dir'))
245
69.1.8 by James Westby
* Switch to properties in the config object.
246
  orig_dir = _opt_property('orig-dir', "The dir to get upstream tarballs from")
247
226.2.12 by James Westby
Default to doing everything in ./build-area/ when building a remote branch
248
  user_orig_dir = property(
249
          lambda self: self._user_config_value('orig-dir'))
250
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
251
  builder = _opt_property('builder', "The command to build with", True)
69.1.8 by James Westby
* Switch to properties in the config object.
252
253
  result_dir = _opt_property('result-dir', "The dir to put the results in")
254
226.2.12 by James Westby
Default to doing everything in ./build-area/ when building a remote branch
255
  user_result_dir = property(
256
          lambda self: self._user_config_value('result-dir'))
257
69.1.8 by James Westby
* Switch to properties in the config object.
258
  merge = _bool_property('merge', "Run in merge mode")
259
73 by James Westby
* Fix mistakes when changing to properties.
260
  quick_builder = _opt_property('quick-builder',
69.1.10 by James Westby
* Builddeb configuration files that are used in the branch are marked as
261
                          "A quick command to build with", True)
262
69.1.8 by James Westby
* Switch to properties in the config object.
263
  native = _bool_property('native', "Build a native package")
264
265
  split = _bool_property('split', "Split a full source package")
266
325 by James Westby
Make upstream-location in the config file work for no-args case.
267
  upstream_branch = _opt_property('upstream-branch',
268
          "The upstream branch to merge from")
269
327.2.9 by James Westby
Re-instate export-upstream for merge mode.
270
  export_upstream = _opt_property('export-upstream',
271
                         "Get the upstream source from another branch")
272
362 by James Westby
Also query the config for the revision to use in export-upstream
273
  export_upstream_revision = _opt_property('export-upstream-revision',
274
                         "The revision of the upstream source to use.")
275
327.2.9 by James Westby
Re-instate export-upstream for merge mode.
276
18.1.37 by James Westby
Add a few doctests and supporting files. move logging.py to bdlogging.py dues to a possible name clash
277
def _test():
278
  import doctest
279
  doctest.testmod()
280
281
if __name__ == '__main__':
282
  _test()
283
178.1.29 by James Westby
Add support for incremental import dsc.
284
# vim: ts=2 sts=2 sw=2