quilt.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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::V3::quilt;
  16. use strict;
  17. use warnings;
  18. # Based on wig&pen implementation
  19. use base 'Dpkg::Source::Package::V2';
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Source::Patch;
  24. use Dpkg::Source::Functions qw(erasedir);
  25. use Dpkg::IPC;
  26. use Dpkg::Vendor qw(get_current_vendor run_vendor_hook);
  27. use Dpkg::Control;
  28. use Dpkg::Changelog::Parse;
  29. use POSIX;
  30. use File::Basename;
  31. use File::Spec;
  32. use File::Path;
  33. our $CURRENT_MINOR_VERSION = "0";
  34. sub init_options {
  35. my ($self) = @_;
  36. $self->{'options'}{'single-debian-patch'} = 0
  37. unless exists $self->{'options'}{'single-debian-patch'};
  38. $self->{'options'}{'allow-version-of-quilt-db'} = []
  39. unless exists $self->{'options'}{'allow-version-of-quilt-db'};
  40. $self->SUPER::init_options();
  41. }
  42. sub parse_cmdline_option {
  43. my ($self, $opt) = @_;
  44. return 1 if $self->SUPER::parse_cmdline_option($opt);
  45. if ($opt =~ /^--single-debian-patch$/) {
  46. $self->{'options'}{'single-debian-patch'} = 1;
  47. return 1;
  48. } elsif ($opt =~ /^--allow-version-of-quilt-db=(.*)$/) {
  49. push @{$self->{'options'}{'allow-version-of-quilt-db'}}, $1;
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. sub can_build {
  55. my ($self, $dir) = @_;
  56. my ($code, $msg) = $self->SUPER::can_build($dir);
  57. return ($code, $msg) if $code eq 0;
  58. my $pd = File::Spec->catdir($dir, "debian", "patches");
  59. if (-e $pd and not -d _) {
  60. return (0, sprintf(_g("%s should be a directory or non-existing"), $pd));
  61. }
  62. my $series_vendor = $self->get_series_file($dir);
  63. my $series_main = File::Spec->catfile($pd, "series");
  64. foreach my $series ($series_vendor, $series_main) {
  65. if (defined($series) and -e $series and not -f _) {
  66. return (0, sprintf(_g("%s should be a file or non-existing"), $series));
  67. }
  68. }
  69. return (1, "");
  70. }
  71. sub get_autopatch_name {
  72. my ($self) = @_;
  73. if ($self->{'options'}{'single-debian-patch'}) {
  74. return "debian-changes";
  75. } else {
  76. return "debian-changes-" . $self->{'fields'}{'Version'};
  77. }
  78. }
  79. sub get_series_file {
  80. my ($self, $dir) = @_;
  81. my $pd = File::Spec->catdir($dir, "debian", "patches");
  82. my $vendor = lc(get_current_vendor() || "debian");
  83. foreach (File::Spec->catfile($pd, "$vendor.series"),
  84. File::Spec->catfile($pd, "series")) {
  85. return $_ if -e $_;
  86. }
  87. return undef;
  88. }
  89. sub read_patch_list {
  90. my ($self, $file, %opts) = @_;
  91. return () if not defined $file or not -f $file;
  92. $opts{"warn_options"} = 0 unless defined($opts{"warn_options"});
  93. $opts{"skip_auto"} = 0 unless defined($opts{"skip_auto"});
  94. my @patches;
  95. my $auto_patch = $self->get_autopatch_name();
  96. open(SERIES, "<" , $file) || syserr(_g("cannot read %s"), $file);
  97. while(defined($_ = <SERIES>)) {
  98. chomp; s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  99. s/(^|\s+)#.*$//; # Strip comment
  100. next unless $_;
  101. if (/^(\S+)\s+(.*)$/) {
  102. $_ = $1;
  103. if ($2 ne '-p1') {
  104. warning(_g("the series file (%s) contains unsupported " .
  105. "options ('%s', line %s), dpkg-source might " .
  106. "fail when applying patches."),
  107. $file, $2, $.) if $opts{"warn_options"};
  108. }
  109. }
  110. next if $opts{"skip_auto"} and $_ eq $auto_patch;
  111. push @patches, $_;
  112. }
  113. close(SERIES);
  114. return @patches;
  115. }
  116. sub create_quilt_db {
  117. my ($self, $dir) = @_;
  118. my $db_dir = File::Spec->catdir($dir, ".pc");
  119. if (not -d $db_dir) {
  120. mkdir $db_dir or syserr(_g("cannot mkdir %s"), $db_dir);
  121. }
  122. my $version_file = File::Spec->catfile($db_dir, ".version");
  123. if (not -e $version_file) {
  124. open(VERSION, ">", $version_file);
  125. print VERSION "2\n";
  126. close(VERSION);
  127. }
  128. }
  129. sub apply_quilt_patch {
  130. my ($self, $dir, $patch, %opts) = @_;
  131. $opts{"verbose"} = 0 unless defined($opts{"verbose"});
  132. $opts{"timestamp"} = time() unless defined($opts{"timestamp"});
  133. my $path = File::Spec->catfile($dir, "debian", "patches", $patch);
  134. my $obj = Dpkg::Source::Patch->new(filename => $path);
  135. info(_g("applying %s"), $patch) if $opts{"verbose"};
  136. $obj->apply($dir, timestamp => $opts{"timestamp"},
  137. force_timestamp => 1, create_dirs => 1, remove_backup => 0,
  138. options => [ '-s', '-t', '-F', '0', '-N', '-p1', '-u',
  139. '-V', 'never', '-g0', '-E', '-b',
  140. '-B', ".pc/$patch/" ]);
  141. }
  142. sub get_patches {
  143. my ($self, $dir, %opts) = @_;
  144. my $series = $self->get_series_file($dir);
  145. return $self->read_patch_list($series, %opts);
  146. }
  147. sub apply_patches {
  148. my ($self, $dir, %opts) = @_;
  149. if ($opts{'usage'} eq 'unpack') {
  150. $opts{'verbose'} = 1;
  151. } elsif ($opts{'usage'} eq 'build') {
  152. $opts{'warn_options'} = 1;
  153. $opts{'verbose'} = 0;
  154. }
  155. my $patches = $opts{"patches"};
  156. # Update debian/patches/series symlink if needed to allow quilt usage
  157. my $series = $self->get_series_file($dir);
  158. return unless $series; # No series, no patches
  159. my $basename = basename($series);
  160. if ($basename ne "series") {
  161. my $dest = File::Spec->catfile($dir, "debian", "patches", "series");
  162. unlink($dest) if -l $dest;
  163. unless (-f _) { # Don't overwrite real files
  164. symlink($basename, $dest) ||
  165. syserr(_g("can't create symlink %s"), $dest);
  166. }
  167. }
  168. unless (defined($patches)) {
  169. $patches = [ $self->get_patches($dir, %opts) ];
  170. }
  171. return unless scalar(@$patches);
  172. # Apply patches
  173. $self->create_quilt_db($dir);
  174. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  175. my @applied = $self->read_patch_list($pc_applied);
  176. my @patches = $self->read_patch_list($self->get_series_file($dir));
  177. open(APPLIED, '>>', $pc_applied) || syserr(_g("cannot write %s"), $pc_applied);
  178. $opts{"timestamp"} = time();
  179. foreach my $patch (@$patches) {
  180. $self->apply_quilt_patch($dir, $patch, %opts);
  181. print APPLIED "$patch\n";
  182. }
  183. close(APPLIED);
  184. }
  185. sub prepare_build {
  186. my ($self, $dir) = @_;
  187. $self->SUPER::prepare_build($dir);
  188. # Skip .pc directories of quilt by default and ignore difference
  189. # on debian/patches/series symlinks and d/p/.dpkg-source-applied
  190. # stamp file created by ourselves
  191. my $func = sub {
  192. return 1 if $_[0] =~ m{^debian/patches/series$} and -l $_[0];
  193. return 1 if $_[0] =~ /^.pc(\/|$)/;
  194. return 1 if $_[0] =~ /$self->{'options'}{'diff_ignore_regexp'}/;
  195. return 0;
  196. };
  197. $self->{'diff_options'}{'diff_ignore_func'} = $func;
  198. }
  199. sub do_build {
  200. my ($self, $dir) = @_;
  201. my $pc_ver = File::Spec->catfile($dir, ".pc", ".version");
  202. if (-f $pc_ver) {
  203. open(VER, "<", $pc_ver) || syserr(_g("cannot read %s"), $pc_ver);
  204. my $version = <VER>;
  205. chomp $version;
  206. close(VER);
  207. if ($version != 2) {
  208. if (scalar grep { $version eq $_ }
  209. @{$self->{'options'}{'allow-version-of-quilt-db'}})
  210. {
  211. warning(_g("unsupported version of the quilt metadata: %s"),
  212. $version);
  213. } else {
  214. error(_g("unsupported version of the quilt metadata: %s"),
  215. $version);
  216. }
  217. }
  218. }
  219. $self->SUPER::do_build($dir);
  220. }
  221. sub check_patches_applied {
  222. my ($self, $dir) = @_;
  223. my $pc_applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  224. my @applied = $self->read_patch_list($pc_applied);
  225. my @patches = $self->read_patch_list($self->get_series_file($dir));
  226. my @to_apply;
  227. foreach my $patch (@patches) {
  228. next if scalar grep { $_ eq $patch } @applied;
  229. push @to_apply, $patch;
  230. }
  231. if (scalar(@to_apply)) {
  232. my $first_patch = File::Spec->catfile($dir, "debian", "patches",
  233. $to_apply[0]);
  234. my $patch_obj = Dpkg::Source::Patch->new(filename => $first_patch);
  235. if ($patch_obj->check_apply($dir)) {
  236. warning(_g("patches have not been applied, applying them now " .
  237. "(use --no-preparation to override)"));
  238. $self->apply_patches($dir, usage => 'preparation', verbose => 1,
  239. patches => \@to_apply);
  240. }
  241. }
  242. }
  243. sub register_autopatch {
  244. my ($self, $dir) = @_;
  245. sub add_line {
  246. my ($file, $line) = @_;
  247. open(FILE, ">>", $file) || syserr(_g("cannot write %s"), $file);
  248. print FILE "$line\n";
  249. close(FILE);
  250. }
  251. sub drop_line {
  252. my ($file, $re) = @_;
  253. open(FILE, "<", $file) || syserr(_g("cannot read %s"), $file);
  254. my @lines = <FILE>;
  255. close(FILE);
  256. open(FILE, ">", $file) || syserr(_g("cannot write %s"), $file);
  257. print(FILE $_) foreach grep { not /^\Q$re\E\s*$/ } @lines;
  258. close(FILE);
  259. }
  260. my $auto_patch = $self->get_autopatch_name();
  261. my @patches = $self->get_patches($dir);
  262. my $has_patch = (grep { $_ eq $auto_patch } @patches) ? 1 : 0;
  263. my $series = $self->get_series_file($dir);
  264. $series ||= File::Spec->catfile($dir, "debian", "patches", "series");
  265. my $applied = File::Spec->catfile($dir, ".pc", "applied-patches");
  266. my $patch = File::Spec->catfile($dir, "debian", "patches", $auto_patch);
  267. if (-e $patch) {
  268. # Add auto_patch to series file
  269. if (not $has_patch) {
  270. add_line($series, $auto_patch);
  271. add_line($applied, $auto_patch);
  272. }
  273. # Ensure quilt meta-data are created and in sync with some trickery:
  274. # reverse-apply the patch, drop .pc/$patch, re-apply it
  275. # with the correct options to recreate the backup files
  276. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  277. $patch_obj->apply($dir, add_options => ['-R', '-E']);
  278. erasedir(File::Spec->catdir($dir, ".pc", $auto_patch));
  279. $self->apply_quilt_patch($dir, $auto_patch);
  280. } else {
  281. # Remove auto_patch from series
  282. if ($has_patch) {
  283. drop_line($series, $auto_patch);
  284. drop_line($applied, $auto_patch);
  285. erasedir(File::Spec->catdir($dir, ".pc", $auto_patch));
  286. }
  287. # Clean up empty series
  288. unlink($series) if not -s $series;
  289. }
  290. }
  291. sub get_patch_header {
  292. my ($self, $dir, $previous) = @_;
  293. my $ph = File::Spec->catfile($dir, "debian", "source", "patch-header");
  294. my $text;
  295. if (-f $ph) {
  296. open(PH, "<", $ph) || syserr(_g("cannot read %s"), $ph);
  297. $text = join("", <PH>);
  298. close(PH);
  299. return $text;
  300. }
  301. my $ch_info = changelog_parse(offset => 0, count => 1,
  302. file => File::Spec->catfile($dir, "debian", "changelog"));
  303. return '' if not defined $ch_info;
  304. return $self->SUPER::get_patch_header($dir, $previous)
  305. if $self->{'options'}{'single-debian-patch'};
  306. my $header = Dpkg::Control->new(type => CTRL_UNKNOWN);
  307. $header->{'Description'} = "Upstream changes introduced in version " .
  308. $ch_info->{'Version'} . "\n";
  309. $header->{'Description'} .=
  310. "This patch has been created by dpkg-source during the package build.
  311. Here's the last changelog entry, hopefully it gives details on why
  312. those changes were made:\n";
  313. $header->{'Description'} .= $ch_info->{'Changes'} . "\n";
  314. $header->{'Description'} .=
  315. "\nThe person named in the Author field signed this changelog entry.\n";
  316. $header->{'Author'} = $ch_info->{'Maintainer'};
  317. $text = "$header";
  318. run_vendor_hook("extend-patch-header", \$text, $ch_info);
  319. $text .= "\n---
  320. The information above should follow the Patch Tagging Guidelines, please
  321. checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
  322. are templates for supplementary fields that you might want to add:
  323. Origin: <vendor|upstream|other>, <url of original patch>
  324. Bug: <url in upstream bugtracker>
  325. Bug-Debian: http://bugs.debian.org/<bugnumber>
  326. Forwarded: <no|not-needed|url proving that it has been forwarded>
  327. Reviewed-By: <name and email of someone who approved the patch>
  328. Last-Update: <YYYY-MM-DD>\n\n";
  329. return $text;
  330. }
  331. # vim:et:sw=4:ts=8
  332. 1;