Bzr.pm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #
  2. # bzr support for dpkg-source
  3. #
  4. # Copyright © 2007 Colin Watson <cjwatson@debian.org>.
  5. # Based on Dpkg::Source::Package::V3_0::git, which is:
  6. # Copyright © 2007 Joey Hess <joeyh@debian.org>.
  7. # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. package Dpkg::Source::Package::V3::Bzr;
  22. use strict;
  23. use warnings;
  24. our $VERSION = '0.01';
  25. use parent qw(Dpkg::Source::Package);
  26. use Cwd;
  27. use File::Basename;
  28. use File::Find;
  29. use File::Temp qw(tempdir);
  30. use Dpkg;
  31. use Dpkg::Gettext;
  32. use Dpkg::Compression;
  33. use Dpkg::ErrorHandling;
  34. use Dpkg::Source::Archive;
  35. use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
  36. use Dpkg::Source::Functions qw(erasedir);
  37. our $CURRENT_MINOR_VERSION = '0';
  38. sub import {
  39. foreach my $dir (split(/:/, $ENV{PATH})) {
  40. if (-x "$dir/bzr") {
  41. return 1;
  42. }
  43. }
  44. error(_g('cannot unpack bzr-format source package because ' .
  45. 'bzr is not in the PATH'));
  46. }
  47. sub sanity_check {
  48. my $srcdir = shift;
  49. if (! -d "$srcdir/.bzr") {
  50. error(_g('source directory is not the top directory of a bzr repository (%s/.bzr not present), but Format bzr was specified'),
  51. $srcdir);
  52. }
  53. # Symlinks from .bzr to outside could cause unpack failures, or
  54. # point to files they shouldn't, so check for and don't allow.
  55. if (-l "$srcdir/.bzr") {
  56. error(_g('%s is a symlink'), "$srcdir/.bzr");
  57. }
  58. my $abs_srcdir = Cwd::abs_path($srcdir);
  59. find(sub {
  60. if (-l $_) {
  61. if (Cwd::abs_path(readlink($_)) !~ /^\Q$abs_srcdir\E(\/|$)/) {
  62. error(_g('%s is a symlink to outside %s'),
  63. $File::Find::name, $srcdir);
  64. }
  65. }
  66. }, "$srcdir/.bzr");
  67. return 1;
  68. }
  69. sub can_build {
  70. my ($self, $dir) = @_;
  71. return (0, _g("doesn't contain a bzr repository")) unless -d "$dir/.bzr";
  72. return 1;
  73. }
  74. sub do_build {
  75. my ($self, $dir) = @_;
  76. my @argv = @{$self->{options}{ARGV}};
  77. # TODO: warn here?
  78. #my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
  79. my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
  80. $dir =~ s{/+$}{}; # Strip trailing /
  81. my ($dirname, $updir) = fileparse($dir);
  82. if (scalar(@argv)) {
  83. usageerr(_g("-b takes only one parameter with format `%s'"),
  84. $self->{fields}{'Format'});
  85. }
  86. my $sourcepackage = $self->{fields}{'Source'};
  87. my $basenamerev = $self->get_basename(1);
  88. my $basename = $self->get_basename();
  89. my $basedirname = $basename;
  90. $basedirname =~ s/_/-/;
  91. sanity_check($dir);
  92. my $old_cwd = getcwd();
  93. chdir($dir) ||
  94. syserr(_g("unable to chdir to `%s'"), $dir);
  95. # Check for uncommitted files.
  96. # To support dpkg-source -i, remove any ignored files from the
  97. # output of bzr status.
  98. open(my $bzr_status_fh, '-|', 'bzr', 'status') ||
  99. subprocerr('bzr status');
  100. my @files;
  101. while (<$bzr_status_fh>) {
  102. chomp;
  103. next unless s/^ +//;
  104. if (! length $diff_ignore_regex ||
  105. ! m/$diff_ignore_regex/o) {
  106. push @files, $_;
  107. }
  108. }
  109. close($bzr_status_fh) || syserr(_g('bzr status exited nonzero'));
  110. if (@files) {
  111. error(_g('uncommitted, not-ignored changes in working directory: %s'),
  112. join(' ', @files));
  113. }
  114. chdir($old_cwd) ||
  115. syserr(_g("unable to chdir to `%s'"), $old_cwd);
  116. my $tmp = tempdir("$dirname.bzr.XXXXXX", DIR => $updir);
  117. push_exit_handler(sub { erasedir($tmp) });
  118. my $tardir = "$tmp/$dirname";
  119. system('bzr', 'branch', $dir, $tardir);
  120. subprocerr("bzr branch $dir $tardir") if $?;
  121. # Remove the working tree.
  122. system('bzr', 'remove-tree', $tardir);
  123. subprocerr("bzr remove-tree $tardir") if $?;
  124. # Some branch metadata files are unhelpful.
  125. unlink("$tardir/.bzr/branch/branch-name",
  126. "$tardir/.bzr/branch/parent");
  127. # Create the tar file
  128. my $debianfile = "$basenamerev.bzr.tar." . $self->{options}{comp_ext};
  129. info(_g('building %s in %s'),
  130. $sourcepackage, $debianfile);
  131. my $tar = Dpkg::Source::Archive->new(filename => $debianfile,
  132. compression => $self->{options}{compression},
  133. compression_level => $self->{options}{comp_level});
  134. $tar->create(chdir => $tmp);
  135. $tar->add_directory($dirname);
  136. $tar->finish();
  137. erasedir($tmp);
  138. pop_exit_handler();
  139. $self->add_file($debianfile);
  140. }
  141. # Called after a tarball is unpacked, to check out the working copy.
  142. sub do_extract {
  143. my ($self, $newdirectory) = @_;
  144. my $fields = $self->{fields};
  145. my $dscdir = $self->{basedir};
  146. my $basename = $self->get_basename();
  147. my $basenamerev = $self->get_basename(1);
  148. my @files = $self->get_files();
  149. if (@files > 1) {
  150. error(_g('format v3.0 uses only one source file'));
  151. }
  152. my $tarfile = $files[0];
  153. my $comp_ext_regex = compression_get_file_extension_regex();
  154. if ($tarfile !~ /^\Q$basenamerev\E\.bzr\.tar\.$comp_ext_regex$/) {
  155. error(_g('expected %s, got %s'),
  156. "$basenamerev.bzr.tar.$comp_ext_regex", $tarfile);
  157. }
  158. erasedir($newdirectory);
  159. # Extract main tarball
  160. info(_g('unpacking %s'), $tarfile);
  161. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  162. $tar->extract($newdirectory);
  163. sanity_check($newdirectory);
  164. my $old_cwd = getcwd();
  165. chdir($newdirectory) ||
  166. syserr(_g("unable to chdir to `%s'"), $newdirectory);
  167. # Reconstitute the working tree.
  168. system('bzr', 'checkout');
  169. subprocerr('bzr checkout') if $?;
  170. chdir($old_cwd) ||
  171. syserr(_g("unable to chdir to `%s'"), $old_cwd);
  172. }
  173. 1;