V1.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. # Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Package::V1;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "0.01";
  19. use base 'Dpkg::Source::Package';
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Compression;
  24. use Dpkg::Source::Archive;
  25. use Dpkg::Source::Patch;
  26. use Dpkg::Exit;
  27. use Dpkg::Source::Functions qw(erasedir);
  28. use Dpkg::Source::Package::V3::native;
  29. use POSIX;
  30. use File::Basename;
  31. use File::Temp qw(tempfile);
  32. use File::Spec;
  33. our $CURRENT_MINOR_VERSION = "0";
  34. sub init_options {
  35. my ($self) = @_;
  36. # Don't call $self->SUPER::init_options() on purpose, V1.0 has no
  37. # ignore by default
  38. $self->{'options'}{'sourcestyle'} ||= 'X';
  39. $self->{'options'}{'skip_debianization'} ||= 0;
  40. }
  41. sub parse_cmdline_option {
  42. my ($self, $opt) = @_;
  43. my $o = $self->{'options'};
  44. if ($opt =~ m/^-s([akpursnAKPUR])$/) {
  45. warning(_g("-s%s option overrides earlier -s%s option"), $1,
  46. $o->{'sourcestyle'}) if $o->{'sourcestyle'} ne 'X';
  47. $o->{'sourcestyle'} = $1;
  48. $o->{'copy_orig_tarballs'} = 0 if $1 eq 'n'; # Extract option -sn
  49. return 1;
  50. } elsif ($opt =~ m/^--skip-debianization$/) {
  51. $o->{'skip_debianization'} = 1;
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. sub do_extract {
  57. my ($self, $newdirectory) = @_;
  58. my $sourcestyle = $self->{'options'}{'sourcestyle'};
  59. my $fields = $self->{'fields'};
  60. $sourcestyle =~ y/X/p/;
  61. $sourcestyle =~ m/[pun]/ ||
  62. usageerr(_g("source handling style -s%s not allowed with -x"),
  63. $sourcestyle);
  64. my $dscdir = $self->{'basedir'};
  65. my $basename = $self->get_basename();
  66. my $basenamerev = $self->get_basename(1);
  67. # V1.0 only supports gzip compression
  68. my ($tarfile, $difffile);
  69. foreach my $file ($self->get_files()) {
  70. if ($file =~ /^(?:\Q$basename\E\.orig|\Q$basenamerev\E)\.tar\.gz$/) {
  71. error(_g("multiple tarfiles in v1.0 source package")) if $tarfile;
  72. $tarfile = $file;
  73. } elsif ($file =~ /^\Q$basenamerev\E\.diff\.gz$/) {
  74. $difffile = $file;
  75. } else {
  76. error(_g("unrecognized file for a %s source package: %s"),
  77. "v1.0", $file);
  78. }
  79. }
  80. error(_g("no tarfile in Files field")) unless $tarfile;
  81. my $native = $difffile ? 0 : 1;
  82. if ($native and ($tarfile =~ /\.orig\.tar\.gz$/)) {
  83. warning(_g("native package with .orig.tar"));
  84. $native = 0; # V3::native doesn't handle orig.tar
  85. }
  86. if ($native) {
  87. Dpkg::Source::Package::V3::native::do_extract($self, $newdirectory);
  88. } else {
  89. my $expectprefix = $newdirectory;
  90. $expectprefix .= '.orig';
  91. erasedir($newdirectory);
  92. if (-e $expectprefix) {
  93. rename($expectprefix, "$newdirectory.tmp-keep") ||
  94. syserr(_g("unable to rename `%s' to `%s'"), $expectprefix,
  95. "$newdirectory.tmp-keep");
  96. }
  97. info(_g("unpacking %s"), $tarfile);
  98. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  99. $tar->extract($expectprefix);
  100. if ($sourcestyle =~ /u/) {
  101. # -su: keep .orig directory unpacked
  102. if (-e "$newdirectory.tmp-keep") {
  103. error(_g("unable to keep orig directory (already exists)"));
  104. }
  105. system('cp', '-ar', '--', $expectprefix, "$newdirectory.tmp-keep");
  106. subprocerr("cp $expectprefix to $newdirectory.tmp-keep") if $?;
  107. }
  108. rename($expectprefix, $newdirectory) ||
  109. syserr(_g("failed to rename newly-extracted %s to %s"),
  110. $expectprefix, $newdirectory);
  111. # rename the copied .orig directory
  112. if (-e "$newdirectory.tmp-keep") {
  113. rename("$newdirectory.tmp-keep", $expectprefix) ||
  114. syserr(_g("failed to rename saved %s to %s"),
  115. "$newdirectory.tmp-keep", $expectprefix);
  116. }
  117. }
  118. if ($difffile and not $self->{'options'}{'skip_debianization'}) {
  119. my $patch = "$dscdir$difffile";
  120. info(_g("applying %s"), $difffile);
  121. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  122. my $analysis = $patch_obj->apply($newdirectory, force_timestamp => 1,
  123. timestamp => time());
  124. my @files = grep { ! m{^[^/]+/debian/} }
  125. sort keys %{$analysis->{'filepatched'}};
  126. info(_g("upstream files that have been modified: %s"),
  127. "\n " . join("\n ", @files)) if scalar @files;
  128. }
  129. }
  130. sub can_build {
  131. my ($self, $dir) = @_;
  132. # As long as we can use gzip, we can do it as we have
  133. # native packages as fallback
  134. return ($self->{'options'}{'compression'} eq "gzip",
  135. _g("only supports gzip compression"));
  136. }
  137. sub do_build {
  138. my ($self, $dir) = @_;
  139. my $sourcestyle = $self->{'options'}{'sourcestyle'};
  140. my @argv = @{$self->{'options'}{'ARGV'}};
  141. my @tar_ignore = map { "--exclude=$_" } @{$self->{'options'}{'tar_ignore'}};
  142. my $diff_ignore_regexp = $self->{'options'}{'diff_ignore_regexp'};
  143. if (scalar(@argv) > 1) {
  144. usageerr(_g("-b takes at most a directory and an orig source ".
  145. "argument (with v1.0 source package)"));
  146. }
  147. $sourcestyle =~ y/X/A/;
  148. unless ($sourcestyle =~ m/[akpursnAKPUR]/) {
  149. usageerr(_g("source handling style -s%s not allowed with -b"),
  150. $sourcestyle);
  151. }
  152. my $sourcepackage = $self->{'fields'}{'Source'};
  153. my $basenamerev = $self->get_basename(1);
  154. my $basename = $self->get_basename();
  155. my $basedirname = $basename;
  156. $basedirname =~ s/_/-/;
  157. # Try to find a .orig tarball for the package
  158. my $origdir = "$dir.orig";
  159. my $origtargz = $self->get_basename() . ".orig.tar.gz";
  160. if (-e $origtargz) {
  161. unless (-f $origtargz) {
  162. error(_g("packed orig `%s' exists but is not a plain file"), $origtargz);
  163. }
  164. } else {
  165. $origtargz = undef;
  166. }
  167. if (@argv) {
  168. # We have a second-argument <orig-dir> or <orig-targz>, check what it
  169. # is to decide the mode to use
  170. my $origarg = shift(@argv);
  171. if (length($origarg)) {
  172. stat($origarg) ||
  173. syserr(_g("cannot stat orig argument %s"), $origarg);
  174. if (-d _) {
  175. $origdir = File::Spec->catdir($origarg);
  176. $sourcestyle =~ y/aA/rR/;
  177. unless ($sourcestyle =~ m/[ursURS]/) {
  178. error(_g("orig argument is unpacked but source handling " .
  179. "style -s%s calls for packed (.orig.tar.<ext>)"),
  180. $sourcestyle);
  181. }
  182. } elsif (-f _) {
  183. $origtargz = $origarg;
  184. $sourcestyle =~ y/aA/pP/;
  185. unless ($sourcestyle =~ m/[kpsKPS]/) {
  186. error(_g("orig argument is packed but source handling " .
  187. "style -s%s calls for unpacked (.orig/)"),
  188. $sourcestyle);
  189. }
  190. } else {
  191. error(_g("orig argument %s is not a plain file or directory"),
  192. $origarg);
  193. }
  194. } else {
  195. $sourcestyle =~ y/aA/nn/;
  196. $sourcestyle =~ m/n/ ||
  197. error(_g("orig argument is empty (means no orig, no diff) " .
  198. "but source handling style -s%s wants something"),
  199. $sourcestyle);
  200. }
  201. } elsif ($sourcestyle =~ m/[aA]/) {
  202. # We have no explicit <orig-dir> or <orig-targz>, try to use
  203. # a .orig tarball first, then a .orig directory and fall back to
  204. # creating a native .tar.gz
  205. if ($origtargz) {
  206. $sourcestyle =~ y/aA/pP/; # .orig.tar.<ext>
  207. } else {
  208. if (stat($origdir)) {
  209. unless (-d _) {
  210. error(_g("unpacked orig `%s' exists but is not a directory"),
  211. $origdir);
  212. }
  213. $sourcestyle =~ y/aA/rR/; # .orig directory
  214. } elsif ($! != ENOENT) {
  215. syserr(_g("unable to stat putative unpacked orig `%s'"), $origdir);
  216. } else {
  217. $sourcestyle =~ y/aA/nn/; # Native tar.gz
  218. }
  219. }
  220. }
  221. my ($dirname, $dirbase) = fileparse($dir);
  222. if ($dirname ne $basedirname) {
  223. warning(_g("source directory '%s' is not <sourcepackage>" .
  224. "-<upstreamversion> '%s'"), $dir, $basedirname);
  225. }
  226. my ($tarname, $tardirname, $tardirbase, $origdirname);
  227. if ($sourcestyle ne 'n') {
  228. my ($origdirname, $origdirbase) = fileparse($origdir);
  229. if ($origdirname ne "$basedirname.orig") {
  230. warning(_g(".orig directory name %s is not <package>" .
  231. "-<upstreamversion> (wanted %s)"),
  232. $origdirname, "$basedirname.orig");
  233. }
  234. $tardirbase = $origdirbase;
  235. $tardirname = $origdirname;
  236. $tarname = $origtargz || "$basename.orig.tar.gz";
  237. unless ($tarname =~ /\Q$basename\E\.orig\.tar\.gz/) {
  238. warning(_g(".orig.tar name %s is not <package>_<upstreamversion>" .
  239. ".orig.tar (wanted %s)"),
  240. $tarname, "$basename.orig.tar.gz");
  241. }
  242. }
  243. if ($sourcestyle eq "n") {
  244. $self->{'options'}{'ARGV'} = []; # ensure we have no error
  245. Dpkg::Source::Package::V3::native::do_build($self, $dir);
  246. } elsif ($sourcestyle =~ m/[nurUR]/) {
  247. if (stat($tarname)) {
  248. unless ($sourcestyle =~ m/[nUR]/) {
  249. error(_g("tarfile `%s' already exists, not overwriting, " .
  250. "giving up; use -sU or -sR to override"), $tarname);
  251. }
  252. } elsif ($! != ENOENT) {
  253. syserr(_g("unable to check for existence of `%s'"), $tarname);
  254. }
  255. info(_g("building %s in %s"),
  256. $sourcepackage, $tarname);
  257. my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
  258. DIR => getcwd(), UNLINK => 0);
  259. my $tar = Dpkg::Source::Archive->new(filename => $newtar,
  260. compression => compression_guess_from_filename($tarname),
  261. compression_level => $self->{'options'}{'comp_level'});
  262. $tar->create(options => \@tar_ignore, 'chdir' => $tardirbase);
  263. $tar->add_directory($tardirname);
  264. $tar->finish();
  265. rename($newtar, $tarname) ||
  266. syserr(_g("unable to rename `%s' (newly created) to `%s'"),
  267. $newtar, $tarname);
  268. chmod(0666 &~ umask(), $tarname) ||
  269. syserr(_g("unable to change permission of `%s'"), $tarname);
  270. } else {
  271. info(_g("building %s using existing %s"),
  272. $sourcepackage, $tarname);
  273. }
  274. $self->add_file($tarname) if $tarname;
  275. if ($sourcestyle =~ m/[kpKP]/) {
  276. if (stat($origdir)) {
  277. unless ($sourcestyle =~ m/[KP]/) {
  278. error(_g("orig dir `%s' already exists, not overwriting, ".
  279. "giving up; use -sA, -sK or -sP to override"),
  280. $origdir);
  281. }
  282. push @Dpkg::Exit::handlers, sub { erasedir($origdir) };
  283. erasedir($origdir);
  284. pop @Dpkg::Exit::handlers;
  285. } elsif ($! != ENOENT) {
  286. syserr(_g("unable to check for existence of orig dir `%s'"),
  287. $origdir);
  288. }
  289. my $tar = Dpkg::Source::Archive->new(filename => $origtargz);
  290. $tar->extract($origdir);
  291. }
  292. my $ur; # Unrepresentable changes
  293. if ($sourcestyle =~ m/[kpursKPUR]/) {
  294. my $diffname = "$basenamerev.diff.gz";
  295. info(_g("building %s in %s"),
  296. $sourcepackage, $diffname);
  297. my ($ndfh, $newdiffgz) = tempfile("$diffname.new.XXXXXX",
  298. DIR => getcwd(), UNLINK => 0);
  299. push @Dpkg::Exit::handlers, sub { unlink($newdiffgz) };
  300. my $diff = Dpkg::Source::Patch->new(filename => $newdiffgz,
  301. compression => "gzip");
  302. $diff->create();
  303. $diff->add_diff_directory($origdir, $dir,
  304. basedirname => $basedirname,
  305. diff_ignore_regexp => $diff_ignore_regexp,
  306. options => []); # Force empty set of options to drop the
  307. # default -p option
  308. $diff->finish() || $ur++;
  309. pop @Dpkg::Exit::handlers;
  310. my $analysis = $diff->analyze($origdir);
  311. my @files = grep { ! m{^debian/} } map { s{^[^/]+/+}{}; $_ }
  312. sort keys %{$analysis->{'filepatched'}};
  313. if (scalar @files) {
  314. warning(_g("the diff modifies the following upstream files: %s"),
  315. "\n " . join("\n ", @files));
  316. info(_g("use the '3.0 (quilt)' format to have separate and " .
  317. "documented changes to upstream files, see dpkg-source(1)"));
  318. }
  319. rename($newdiffgz, $diffname) ||
  320. syserr(_g("unable to rename `%s' (newly created) to `%s'"),
  321. $newdiffgz, $diffname);
  322. chmod(0666 &~ umask(), $diffname) ||
  323. syserr(_g("unable to change permission of `%s'"), $diffname);
  324. $self->add_file($diffname);
  325. }
  326. if ($sourcestyle =~ m/[prPR]/) {
  327. erasedir($origdir);
  328. }
  329. if ($ur) {
  330. printf(STDERR _g("%s: unrepresentable changes to source")."\n",
  331. $progname);
  332. exit(1);
  333. }
  334. }
  335. # vim: set et sw=4 ts=8
  336. 1;