V1.pm 16 KB

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