~nomed/bzr-builddeb/nomed

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#    builder.py -- Classes for building packages
#    Copyright (C) 2006 James Westby <jw+debian@jameswestby.net>
#    
#    This file is part of bzr-builddeb.
#
#    bzr-builddeb 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 glob
import shutil
import tarfile
import tempfile
import os

from bzrlib.branch import Branch
from bzrlib.errors import NoWorkingTree
from bzrlib.export import export
from bzrlib.revisionspec import RevisionSpec
from bzrlib.trace import info, mutter
from bzrlib.workingtree import WorkingTree

from changes import DebianChanges
from errors import (DebianError,
                    NoSourceDirError,
                    BuildFailedError,
                    StopBuild,
                    )
from util import recursive_copy

def remove_dir(base, dir):
  """Removes a directory from within a base."""
  
  remove_dir = os.path.join(base, dir)
  if os.path.isdir(remove_dir) and not os.path.islink(remove_dir):
    shutil.rmtree(remove_dir)

def remove_bzrbuilddeb_dir(dir):
  """Removes the .bzr-builddeb dir from the specfied directory."""

  #XXX: Is this what we want??
  remove_dir(dir, ".bzr-builddeb")

def remove_debian_dir(dir):
  """Remove the debian/ dir from the specified directory."""

  remove_dir(dir, "debian")

class DebBuild(object):
  """The object that does the building work."""

  def __init__(self, properties, tree):
    self._properties = properties
    self._tree = tree

  def prepare(self, keep_source_dir=False):
    build_dir = self._properties.build_dir()
    info("Preparing the build area: %s", build_dir);
    if not os.path.exists(build_dir):
      os.makedirs(build_dir)
    source_dir = self._properties.source_dir()
    if os.path.exists(source_dir):
      if not keep_source_dir:
        info("Purging the build dir: %s", source_dir)
        shutil.rmtree(source_dir)
      else:
        info("Not purging build dir as requested: %s", build_dir)
    else:
      if keep_source_dir:
        raise NoSourceDirError;

  def _find_tarball(self):
    tarballdir = self._properties.tarball_dir()
    tarball = os.path.join(tarballdir,self._tarball_name())
    info("Looking for %s to use as upstream source", tarball)
    if not os.path.exists(tarballdir):
      raise DebianError('Could not find dir with upstream tarballs: '
          +tarballdir)
    if not os.path.exists(tarball):
      raise DebianError('Could not find upstream tarball at '+tarball)
    return tarball

  def _tarball_name(self):
    package = self._properties.package()
    upstream = self._properties.upstream_version()
    return package+"_"+upstream+".orig.tar.gz"

  def export(self, use_existing=False):
    # It's not documented the use_existing will use the same 
    # tarball, and it doesn't save much here, but we will
    # do it anyway.
    # TODO: should we still copy the tarball across if the target doesn't
    # exists when use_existing is True. It would save having to remember
    # state, but kind of goes against the name.
    if not use_existing:
      # Just copy the tarball across, no need to unpack it.
      tarball = self._find_tarball()
      build_dir = self._properties.build_dir()
      shutil.copyfile(tarball, os.path.join(build_dir, self._tarball_name()))
    source_dir = self._properties.source_dir()
    info("Exporting to %s", source_dir)
    export(self._tree,source_dir,None,None)
    remove_bzrbuilddeb_dir(source_dir)

  def build(self, builder):
    wd = os.getcwdu()
    source_dir = self._properties.source_dir()
    info("Building the package in %s, using %s", source_dir, builder)
    os.chdir(source_dir)
    result = os.system(builder)
    os.chdir(wd)
    if result > 0:
      raise BuildFailedError;

  def clean(self):
    source_dir = self._properties.source_dir()
    info("Cleaning build dir: %s", source_dir)
    shutil.rmtree(source_dir)

  def move_result(self, result):
    info("Placing result in %s", result)
    package = self._properties.package()
    version = self._properties.full_version()
    changes = DebianChanges(package, version, self._properties.build_dir())
    files = changes.files()
    if not os.path.exists(result):
      os.makedirs(result)
    mutter("Moving %s to %s", changes.filename(), result)
    shutil.move(changes.filename(), result)
    mutter("Moving all files given in %s", changes.filename())
    for file in files:
      mutter("Moving %s to %s", file['name'], result)
      shutil.move(os.path.join(self._properties.build_dir(), file['name']), 
                  result)

  def tag_release(self):
    #TODO decide what command should be able to remove a tag notice
    info("If you are happy with the results and upload use tagdeb to tag this"
        +" release. If you do not release it...")


class DebMergeBuild(DebBuild):
  """A subclass of DebBuild that uses the merge method."""

  def _export_upstream_branch(self):
    return False

  def export(self, use_existing=False):
    package = self._properties.package()
    upstream = self._properties.upstream_version()
    build_dir = self._properties.build_dir()
    source_dir = self._properties.source_dir()
    info("Exporting to %s in merge mode", source_dir)
    if not use_existing:
      upstream = self._export_upstream_branch()
      tarball = self._find_tarball()
      mutter("Extracting %s to %s", tarball, source_dir)
      tempdir = tempfile.mkdtemp(prefix='builddeb-', dir=build_dir)
      tar = tarfile.open(tarball)
      tar.extractall(tempdir)
      tar.close
      files = glob.glob(tempdir+'/*')
      os.makedirs(source_dir)
      for file in files:
        shutil.move(file, source_dir)
      shutil.rmtree(tempdir)
      if not upstream:
        shutil.copy(tarball, build_dir)
    else:
      info("Reusing existing build dir as requested")

    info("Exporting debian/ part to %s", source_dir)
    basetempdir = tempfile.mkdtemp(prefix='builddeb-', dir=build_dir)
    tempdir = os.path.join(basetempdir,"export")
    if self._properties.larstiq():
      os.makedirs(tempdir)
      export_dir = os.path.join(tempdir,'debian')
    else:
      export_dir = tempdir
    export(self._tree,export_dir,None,None)
    recursive_copy(tempdir, source_dir)
    shutil.rmtree(basetempdir)
    remove_bzrbuilddeb_dir(os.path.join(source_dir, "debian"))

class DebNativeBuild(DebBuild):
  """A subclass of DebBuild that builds native packages."""

  def export(self, use_existing=False):
    # Just copy the tree across. use_existing makes no sense here
    # as there is no tarball.
    source_dir = self._properties.source_dir()
    info("Exporting to %s", source_dir)
    export(self._tree,source_dir,None,None)
    remove_bzrbuilddeb_dir(source_dir)

class DebSplitBuild(DebBuild):
  """A subclass of DebBuild that splits the branch to create the 
     .orig.tar.gz."""

  def export(self, use_existing=False):
    # To acheive this we export delete debian/ and tar the result,
    # then we blow that away and export the whole thing again.
    source_dir = self._properties.source_dir()
    build_dir = self._properties.build_dir()
    tarball = os.path.join(build_dir, self._tarball_name())
    export(self._tree,source_dir,None,None)
    info("Creating .orig.tar.gz: %s", tarball)
    remove_bzrbuilddeb_dir(source_dir)
    remove_debian_dir(source_dir)
    tar = tarfile.open(tarball, "w:gz")
    source_dir_rel = self._properties.source_dir(False)
    tar.add(source_dir, source_dir_rel)
    shutil.rmtree(source_dir)
    info("Exporting to %s", source_dir)
    export(self._tree,source_dir,None,None)
    remove_bzrbuilddeb_dir(source_dir)

class DebMergeExportUpstreamBuild(DebMergeBuild):
  """Subclass of DebMergeBuild that will export an upstream branch to
     .orig.tar.gz before building."""

  def __init__(self, properties, tree, export_upstream, export_revision,
               export_prepull, stop_on_no_change):
    DebMergeBuild.__init__(self, properties, tree)
    self._export_upstream = export_upstream
    self._export_revision = export_revision
    self._export_prepull = export_prepull
    self._stop_on_no_change = stop_on_no_change

  def _export_upstream_branch(self):
    build_dir = self._properties.build_dir()
    source_dir_rel = self._properties.source_dir(False)
    # Export from the branch that we got earlier to the
    # appropriately named tarball.
    export_upstream = self._export_upstream
    export_prepull = self._export_prepull
    if export_upstream is None:
      raise DebianError('No branch given for export-upstream')

    if export_prepull:
      try:
        tree_to = WorkingTree.open(export_upstream)
        branch_to = tree_to.branch
      except NoWorkingTree:
        tree_to = None
        branch_to = Branch.open(export_upstream)
      location = branch_to.get_parent()
      if location is None:
        raise DebianError('No default pull location for %s, run "bzr '
                          +'pull location" in that branch to set one up',
                          export_upstream)
      branch_from = Branch.open(location)
      info('Pulling the upstream branch.')
      if tree_to is not None:
        count = tree_to.pull(branch_from)
      else:
        count = branch_to.pull(branch_from)
      info('Pulled %d revision(s).', count)
      if self._stop_on_no_change:
        raise StopBuild('No changes to upstream branch')
      b = branch_to
    else:
      b = Branch.open(export_upstream)

    export_revision = self._export_revision
    if export_revision is None:
      rev_id = b.last_revision()
    else:
      rev_spec = RevisionSpec.from_string(export_revision)
      rev_id = rev_spec.in_history(b).rev_id

    info('Exporting upstream source from %s, revision %s', export_upstream,
         rev_id)

    t = b.repository.revision_tree(rev_id)
    dest = os.path.join(build_dir, self._tarball_name())
    info(source_dir_rel)
    export(t, dest, 'tgz', source_dir_rel)
    return True

  def _find_tarball(self):
    build_dir = self._properties.build_dir()
    return os.path.join(build_dir, self._tarball_name())