V1.pm 14 KB

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