bzr.pm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/perl
  2. #
  3. # bzr support for dpkg-source
  4. #
  5. # Copyright © 2007 Colin Watson <cjwatson@debian.org>.
  6. # Based on Dpkg::Source::Package::V3_0::git, which is:
  7. # Copyright © 2007 Joey Hess <joeyh@debian.org>.
  8. # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. package Dpkg::Source::Package::V3::bzr;
  24. use strict;
  25. use warnings;
  26. use base 'Dpkg::Source::Package';
  27. use Cwd;
  28. use File::Basename;
  29. use File::Find;
  30. use File::Temp qw(tempdir);
  31. use Dpkg;
  32. use Dpkg::Gettext;
  33. use Dpkg::Compression;
  34. use Dpkg::ErrorHandling qw(error subprocerr syserr info);
  35. use Dpkg::Version qw(check_version);
  36. use Dpkg::Source::Archive;
  37. use Dpkg::Exit;
  38. use Dpkg::Source::Functions qw(erasedir);
  39. our $CURRENT_MINOR_VERSION = "0";
  40. sub import {
  41. foreach my $dir (split(/:/, $ENV{PATH})) {
  42. if (-x "$dir/bzr") {
  43. return 1;
  44. }
  45. }
  46. error(_g("This source package can only be manipulated using bzr, which is not in the PATH."));
  47. }
  48. sub sanity_check {
  49. my $srcdir = shift;
  50. if (! -d "$srcdir/.bzr") {
  51. error(_g("source directory is not the top directory of a bzr repository (%s/.bzr not present), but Format bzr was specified"),
  52. $srcdir);
  53. }
  54. # Symlinks from .bzr to outside could cause unpack failures, or
  55. # point to files they shouldn't, so check for and don't allow.
  56. if (-l "$srcdir/.bzr") {
  57. error(_g("%s is a symlink"), "$srcdir/.bzr");
  58. }
  59. my $abs_srcdir = Cwd::abs_path($srcdir);
  60. find(sub {
  61. if (-l $_) {
  62. if (Cwd::abs_path(readlink($_)) !~ /^\Q$abs_srcdir\E(\/|$)/) {
  63. error(_g("%s is a symlink to outside %s"),
  64. $File::Find::name, $srcdir);
  65. }
  66. }
  67. }, "$srcdir/.bzr");
  68. return 1;
  69. }
  70. sub can_build {
  71. my ($self, $dir) = @_;
  72. return (-d "$dir/.bzr", _g("doesn't contain a bzr repository"));
  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_regexp = $self->{'options'}{'diff_ignore_regexp'};
  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(BZR_STATUS, '-|', "bzr", "status") ||
  99. subprocerr("bzr status");
  100. my @files;
  101. while (<BZR_STATUS>) {
  102. chomp;
  103. next unless s/^ +//;
  104. if (! length $diff_ignore_regexp ||
  105. ! m/$diff_ignore_regexp/o) {
  106. push @files, $_;
  107. }
  108. }
  109. close(BZR_STATUS) || 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 @Dpkg::Exit::handlers, sub { erasedir($tmp) };
  118. my $tardir = "$tmp/$dirname";
  119. system("bzr", "branch", $dir, $tardir);
  120. $? && subprocerr("bzr branch $dir $tardir");
  121. # Remove the working tree.
  122. system("bzr", "remove-tree", $tardir);
  123. # Some branch metadata files are unhelpful.
  124. unlink("$tardir/.bzr/branch/branch-name",
  125. "$tardir/.bzr/branch/parent");
  126. # Create the tar file
  127. my $debianfile = "$basenamerev.bzr.tar." . $self->{'options'}{'comp_ext'};
  128. info(_g("building %s in %s"),
  129. $sourcepackage, $debianfile);
  130. my $tar = Dpkg::Source::Archive->new(filename => $debianfile,
  131. compression => $self->{'options'}{'compression'},
  132. compression_level => $self->{'options'}{'comp_level'});
  133. $tar->create('chdir' => $tmp);
  134. $tar->add_directory($dirname);
  135. $tar->finish();
  136. erasedir($tmp);
  137. pop @Dpkg::Exit::handlers;
  138. $self->add_file($debianfile);
  139. }
  140. # Called after a tarball is unpacked, to check out the working copy.
  141. sub do_extract {
  142. my ($self, $newdirectory) = @_;
  143. my $fields = $self->{'fields'};
  144. my $dscdir = $self->{'basedir'};
  145. check_version($fields->{'Version'});
  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. if ($tarfile !~ /^\Q$basenamerev\E\.bzr\.tar\.$comp_regex$/) {
  154. error(_g("expected %s, got %s"),
  155. "$basenamerev.bzr.tar.$comp_regex", $tarfile);
  156. }
  157. erasedir($newdirectory);
  158. # Extract main tarball
  159. info(_g("unpacking %s"), $tarfile);
  160. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  161. $tar->extract($newdirectory);
  162. sanity_check($newdirectory);
  163. my $old_cwd = getcwd();
  164. chdir($newdirectory) ||
  165. syserr(_g("unable to chdir to `%s'"), $newdirectory);
  166. # Reconstitute the working tree.
  167. system("bzr", "checkout");
  168. chdir($old_cwd) ||
  169. syserr(_g("unable to chdir to `%s'"), $old_cwd);
  170. }
  171. 1;