~larstiq/bzr-builddeb/builddeb.lq

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#    config.py -- Configuration of bzr-builddeb from files
#    Copyright (C) 2006 James Westby <jw+debian@jameswestby.net>
#    
#    This file is part of bzr-builddeb.
#
#    bzr-builldeb is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    bzr-builddeb is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with bzr-builddeb; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

import os

from bzrlib.config import ConfigObj
from bzrlib.trace import mutter

from util import add_ignore


class DebBuildConfig(object):
  """Holds the configuration settings for builddeb. These are taken from
  a hierarchy of config files. .bzr-builddeb/local.conf then 
  ~/.bazaar/builddeb.conf, finally .bzr-builddeb/default.conf. The value is 
  taken from the first file in which it is specified."""

  def __init__(self, localfile=None, globalfile=None, defaultfile=None,
               add_to_ignores=False):
    """ 
    >>> c = DebBuildConfig('local.conf','user.conf','default.conf',False)
    >>> print c.orig_dir
    None
    >>> print c.merge
    True
    >>> print c.builder
    localbuilder
    >>> print c.build_dir
    defaultbuild
    >>> print c.result_dir
    userresult
    """
    if globalfile is None:
      globalfile = os.path.expanduser('~/.bazaar/builddeb.conf')
    if localfile is None:
      localfile = ('.bzr-builddeb/local.conf')
    if defaultfile is None:
      defaultfile = ('.bzr-builddeb/default.conf')
    self._config_files = [ConfigObj(localfile), ConfigObj(globalfile), ConfigObj(defaultfile)]
    if add_to_ignores:
      add_ignore(localfile)

  def _get_opt(self, config, key):
    """Returns the value for key from config, of None if it is not defined in 
    the file"""
    try:
      return config.get_value('BUILDDEB', key)
    except KeyError:
      return None

  def _get_best_opt(self, key):
    """Returns the value for key from the first file in which it is defined,
    or None if none of the files define it."""

    for file in self._config_files:
      value = self._get_opt(file, key)
      if value is not None:
        mutter("Using %s for %s, taken from %s", value, key, file.filename)
        return value
    return None

  def _get_bool(self, config, key):
    try:
      return True, config.get_bool('BUILDDEB', key)
    except KeyError:
      return False, False

  def _get_best_bool(self, key, default=False):
    for file in self._config_files:
      (found, value) = self._get_bool(file, key)
      if found:
        mutter("Using %s for %s, taken from %s", value, key, file.filename)
        return value
    return default

  def _opt_property(name, help=None):
    return property(lambda self: self._get_best_opt(name), None, None, help)

  def _bool_property(name, help=None, default=False):
    return property(lambda self: self._get_best_bool(name, default),
                    None, None, help)

  build_dir = _opt_property('build-dir', "The dir to build in")

  orig_dir = _opt_property('orig-dir', "The dir to get upstream tarballs from")

  builder = _opt_property('builder', "The command to build with")

  result_dir = _opt_property('result-dir', "The dir to put the results in")

  merge = _bool_property('merge', "Run in merge mode")

  builder = _opt_property('quick-builder', "A quick command to build with")

  builder = _opt_property('source-builder',
                          "The command to build source packages with")

  merge = _bool_property('ignore-unknowns',
                         "Build even when the tree has unknowns")

  native = _bool_property('native', "Build a native package")

  split = _bool_property('split', "Split a full source package")

  export_upstream = _opt_property('export-upstream',
                         "Get the upstream source from another branch")

  prepull_upstream = _bool_property('export-upstream-prepull',
                         "Pull the upstream branch before exporting it.")

  prepull_upstream_stop = _bool_property('export-upstream-stop-on-trivial-pull',
                         "Stop the build if the upstream pull does nothing.")

def _test():
  import doctest
  doctest.testmod()

if __name__ == '__main__':
  _test()