V1.pm 14 KB

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