Package.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. # Copyright © 2008 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;
  16. use strict;
  17. use warnings;
  18. use Dpkg::Gettext;
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Control;
  21. use Dpkg::Checksums;
  22. use Dpkg::Version;
  23. use Dpkg::Compression;
  24. use Dpkg::Exit;
  25. use Dpkg::Path qw(check_files_are_the_same);
  26. use Dpkg::IPC;
  27. use Dpkg::Vendor qw(run_vendor_hook);
  28. use POSIX;
  29. use File::Basename;
  30. # Public variables
  31. our $diff_ignore_default_regexp = '
  32. # Ignore general backup files
  33. (?:^|/).*~$|
  34. # Ignore emacs recovery files
  35. (?:^|/)\.#.*$|
  36. # Ignore vi swap files
  37. (?:^|/)\..*\.sw.$|
  38. # Ignore baz-style junk files or directories
  39. (?:^|/),,.*(?:$|/.*$)|
  40. # File-names that should be ignored (never directories)
  41. (?:^|/)(?:DEADJOE|\.arch-inventory|\.(?:bzr|cvs|hg|git)ignore)$|
  42. # File or directory names that should be ignored
  43. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg(?:tags)?|_darcs|\.git|
  44. \.shelf|_MTN|\.be|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  45. ';
  46. # Take out comments and newlines
  47. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  48. $diff_ignore_default_regexp =~ s/\n//sg;
  49. no warnings 'qw';
  50. our @tar_ignore_default_pattern = qw(
  51. *.a
  52. *.la
  53. *.o
  54. *.so
  55. .*.sw?
  56. *~
  57. ,,*
  58. .[#~]*
  59. .arch-ids
  60. .arch-inventory
  61. .be
  62. .bzr
  63. .bzr.backup
  64. .bzr.tags
  65. .bzrignore
  66. .cvsignore
  67. .deps
  68. .git
  69. .gitignore
  70. .hg
  71. .hgignore
  72. .hgtags
  73. .shelf
  74. .svn
  75. CVS
  76. DEADJOE
  77. RCS
  78. _MTN
  79. _darcs
  80. {arch}
  81. );
  82. # Object methods
  83. sub new {
  84. my ($this, %args) = @_;
  85. my $class = ref($this) || $this;
  86. my $self = {
  87. 'fields' => Dpkg::Control->new(type => CTRL_PKG_SRC),
  88. 'options' => {},
  89. };
  90. bless $self, $class;
  91. if (exists $args{'options'}) {
  92. $self->{'options'} = $args{'options'};
  93. }
  94. if (exists $args{"filename"}) {
  95. $self->initialize($args{"filename"});
  96. $self->init_options();
  97. }
  98. return $self;
  99. }
  100. sub init_options {
  101. my ($self) = @_;
  102. # Use full ignore list by default
  103. # note: this function is not called by V1 packages
  104. $self->{'options'}{'diff_ignore_regexp'} ||= $diff_ignore_default_regexp;
  105. if (defined $self->{'options'}{'tar_ignore'}) {
  106. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ]
  107. unless @{$self->{'options'}{'tar_ignore'}};
  108. } else {
  109. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ];
  110. }
  111. # Skip debianization while specific to some formats has an impact
  112. # on code common to all formats
  113. $self->{'options'}{'skip_debianization'} ||= 0;
  114. }
  115. sub initialize {
  116. my ($self, $filename) = @_;
  117. my ($fn, $dir) = fileparse($filename);
  118. error(_g("%s is not the name of a file"), $filename) unless $fn;
  119. $self->{'basedir'} = $dir || "./";
  120. $self->{'filename'} = $fn;
  121. # Check if it contains a signature
  122. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  123. $self->{'is_signed'} = 0;
  124. while (<DSC>) {
  125. next if /^\s*$/o;
  126. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  127. last;
  128. }
  129. close(DSC);
  130. # Read the fields
  131. open(CDATA, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  132. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  133. $fields->parse_fh(\*CDATA, sprintf(_g("source control file %s"), $filename));
  134. close(CDATA);
  135. $self->{'fields'} = $fields;
  136. foreach my $f (qw(Source Format Version Files)) {
  137. unless (defined($fields->{$f})) {
  138. error(_g("missing critical source control field %s"), $f);
  139. }
  140. }
  141. $self->parse_files();
  142. $self->upgrade_object_type(0);
  143. }
  144. sub upgrade_object_type {
  145. my ($self, $update_format) = @_;
  146. $update_format = 1 unless defined $update_format;
  147. my $format = $self->{'fields'}{'Format'};
  148. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  149. my ($version, $variant, $major, $minor) = ($1, $2, $1, undef);
  150. $major =~ s/\.[\d\.]+$//;
  151. my $module = "Dpkg::Source::Package::V$major";
  152. $module .= "::$variant" if defined $variant;
  153. eval "require $module; \$minor = \$${module}::CURRENT_MINOR_VERSION;";
  154. $minor = 0 unless defined $minor;
  155. if ($update_format) {
  156. $self->{'fields'}{'Format'} = "$major.$minor";
  157. $self->{'fields'}{'Format'} .= " ($variant)" if defined $variant;
  158. }
  159. if ($@) {
  160. error(_g("source package format `%s' is not supported (Perl module %s is required)"), $format, $module);
  161. }
  162. bless $self, $module;
  163. } else {
  164. error(_g("invalid Format field `%s'"), $format);
  165. }
  166. }
  167. sub get_filename {
  168. my ($self) = @_;
  169. return $self->{'basedir'} . $self->{'filename'};
  170. }
  171. sub get_files {
  172. my ($self) = @_;
  173. return keys %{$self->{'files'}};
  174. }
  175. sub parse_files {
  176. my ($self) = @_;
  177. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  178. my $files = $self->{'fields'}{'Files'};
  179. foreach my $file (split(/\n/, $files)) {
  180. next if $file eq '';
  181. $file =~ m/^($check_regex{md5}) # checksum
  182. [ \t]+(\d+) # size
  183. [ \t]+($rx_fname) # filename
  184. $/x
  185. || error(_g("Files field contains bad line `%s'"), $file);
  186. if (exists $self->{'files'}{$3}) {
  187. error(_g("file `%s' listed twice in Files field"), $3);
  188. } else {
  189. $self->{'files'}{$3} = $2;
  190. }
  191. }
  192. }
  193. sub check_checksums {
  194. my ($self) = @_;
  195. my ($fields, %checksum, %size) = $self->{'fields'};
  196. my $has_md5 = 1;
  197. if (not exists $fields->{'Checksums-Md5'}) {
  198. $fields->{'Checksums-Md5'} = $fields->{'Files'};
  199. $has_md5 = 0;
  200. }
  201. # extract the checksums from the fields in two hashes
  202. readallchecksums($self->{'fields'}, \%checksum, \%size);
  203. delete $fields->{'Checksums-Md5'} unless $has_md5;
  204. # getchecksums verify the checksums if they are pre-filled
  205. foreach my $file ($self->get_files()) {
  206. getchecksums($self->{'basedir'} . $file, $checksum{$file},
  207. \$size{$file});
  208. }
  209. }
  210. sub get_basename {
  211. my ($self, $with_revision) = @_;
  212. my $f = $self->{'fields'};
  213. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  214. error(_g("source and version are required to compute the source basename"));
  215. }
  216. my $v = Dpkg::Version->new($f->{'Version'});
  217. my $basename = $f->{'Source'} . "_" . $v->version();
  218. if ($with_revision and $f->{'Version'} =~ /-/) {
  219. $basename .= "-" . $v->revision();
  220. }
  221. return $basename;
  222. }
  223. sub find_original_tarballs {
  224. my ($self, $ext) = @_;
  225. $ext ||= $compression_re_file_ext;
  226. my $basename = $self->get_basename();
  227. my @tar;
  228. foreach my $dir (".", $self->{'basedir'}, $self->{'options'}{'origtardir'}) {
  229. next unless defined($dir) and -d $dir;
  230. opendir(DIR, $dir) || syserr(_g("cannot opendir %s"), $dir);
  231. push @tar, map { "$dir/$_" }
  232. grep { /^\Q$basename\E\.orig(-[\w-]+)?\.tar\.$ext$/ }
  233. readdir(DIR);
  234. closedir(DIR);
  235. }
  236. return @tar;
  237. }
  238. sub is_signed {
  239. my $self = shift;
  240. return $self->{'is_signed'};
  241. }
  242. sub check_signature {
  243. my ($self) = @_;
  244. my $dsc = $self->get_filename();
  245. my @exec;
  246. if (-x '/usr/bin/gpgv') {
  247. push @exec, "gpgv";
  248. } elsif (-x '/usr/bin/gpg') {
  249. push @exec, "gpg", "--no-default-keyring", "-q", "--verify";
  250. }
  251. if (scalar(@exec)) {
  252. if (-r "$ENV{'HOME'}/.gnupg/trustedkeys.gpg") {
  253. push @exec, "--keyring", "$ENV{'HOME'}/.gnupg/trustedkeys.gpg";
  254. }
  255. foreach my $vendor_keyring (run_vendor_hook('keyrings')) {
  256. if (-r $vendor_keyring) {
  257. push @exec, "--keyring", $vendor_keyring;
  258. }
  259. }
  260. push @exec, $dsc;
  261. my ($stdout, $stderr);
  262. fork_and_exec('exec' => \@exec, wait_child => 1, nocheck => 1,
  263. to_string => \$stdout, error_to_string => \$stderr,
  264. timeout => 10);
  265. if (WIFEXITED($?)) {
  266. my $gpg_status = WEXITSTATUS($?);
  267. print STDERR "$stdout$stderr" if $gpg_status;
  268. if ($gpg_status == 1 or ($gpg_status &&
  269. $self->{'options'}{'require_valid_signature'}))
  270. {
  271. error(_g("failed to verify signature on %s"), $dsc);
  272. } elsif ($gpg_status) {
  273. warning(_g("failed to verify signature on %s"), $dsc);
  274. }
  275. } else {
  276. subprocerr("@exec");
  277. }
  278. } else {
  279. if ($self->{'options'}{'require_valid_signature'}) {
  280. error(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  281. } else {
  282. warning(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  283. }
  284. }
  285. }
  286. sub parse_cmdline_options {
  287. my ($self, @opts) = @_;
  288. foreach (@opts) {
  289. if (not $self->parse_cmdline_option($_)) {
  290. warning(_g("%s is not a valid option for %s"), $_, ref($self));
  291. }
  292. }
  293. }
  294. sub parse_cmdline_option {
  295. return 0;
  296. }
  297. sub extract {
  298. my $self = shift;
  299. my $newdirectory = $_[0];
  300. my ($ok, $error) = version_check($self->{'fields'}{'Version'});
  301. error($error) unless $ok;
  302. # Copy orig tarballs
  303. if ($self->{'options'}{'copy_orig_tarballs'}) {
  304. my $basename = $self->get_basename();
  305. my ($dirname, $destdir) = fileparse($newdirectory);
  306. $destdir ||= "./";
  307. my $ext = $compression_re_file_ext;
  308. foreach my $orig (grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$ext$/ }
  309. $self->get_files())
  310. {
  311. my $src = File::Spec->catfile($self->{'basedir'}, $orig);
  312. my $dst = File::Spec->catfile($destdir, $orig);
  313. if (not check_files_are_the_same($src, $dst, 1)) {
  314. system('cp', '--', $src, $dst);
  315. subprocerr("cp $src to $dst") if $?;
  316. }
  317. }
  318. }
  319. # Try extract
  320. eval { $self->do_extract(@_) };
  321. if ($@) {
  322. &$_() foreach reverse @Dpkg::Exit::handlers;
  323. die $@;
  324. }
  325. # Store format if non-standard so that next build keeps the same format
  326. if ($self->{'fields'}{'Format'} ne "1.0" and
  327. not $self->{'options'}{'skip_debianization'})
  328. {
  329. my $srcdir = File::Spec->catdir($newdirectory, "debian", "source");
  330. my $format_file = File::Spec->catfile($srcdir, "format");
  331. mkdir($srcdir) unless -e $srcdir;
  332. open(FORMAT, ">", $format_file) || syserr(_g("cannot write %s"), $format_file);
  333. print FORMAT $self->{'fields'}{'Format'} . "\n";
  334. close(FORMAT);
  335. }
  336. # Make sure debian/rules is executable
  337. my $rules = File::Spec->catfile($newdirectory, "debian", "rules");
  338. my @s = lstat($rules);
  339. if (not scalar(@s)) {
  340. unless ($! == ENOENT) {
  341. syserr(_g("cannot stat %s"), $rules);
  342. }
  343. warning(_g("%s does not exist"), $rules)
  344. unless $self->{'options'}{'skip_debianization'};
  345. } elsif (-f _) {
  346. chmod($s[2] | 0111, $rules) ||
  347. syserr(_g("cannot make %s executable"), $rules);
  348. } else {
  349. warning(_g("%s is not a plain file"), $rules);
  350. }
  351. }
  352. sub do_extract {
  353. internerr("Dpkg::Source::Package doesn't know how to unpack a " .
  354. "source package. Use one of the subclasses.");
  355. }
  356. # Function used specifically during creation of a source package
  357. sub build {
  358. my $self = shift;
  359. eval { $self->do_build(@_) };
  360. if ($@) {
  361. &$_() foreach reverse @Dpkg::Exit::handlers;
  362. die $@;
  363. }
  364. }
  365. sub do_build {
  366. internerr("Dpkg::Source::Package doesn't know how to build a " .
  367. "source package. Use one of the subclasses.");
  368. }
  369. sub can_build {
  370. my ($self, $dir) = @_;
  371. return (0, "can_build() has not been overriden");
  372. }
  373. sub add_file {
  374. my ($self, $filename) = @_;
  375. my ($fn, $dir) = fileparse($filename);
  376. if (exists $self->{'files'}{$fn}) {
  377. internerr("tried to add file '%s' twice", $fn);
  378. }
  379. my (%sums, $size);
  380. getchecksums($filename, \%sums, \$size);
  381. $self->{'files'}{$fn} = $size;
  382. foreach my $alg (sort keys %sums) {
  383. $self->{'fields'}{"Checksums-$alg"} .= "\n$sums{$alg} $size $fn";
  384. }
  385. $self->{'fields'}{'Files'}.= "\n$sums{md5} $size $fn";
  386. }
  387. sub write_dsc {
  388. my ($self, %opts) = @_;
  389. my $fields = $self->{'fields'};
  390. foreach my $f (keys %{$opts{'override'}}) {
  391. $fields->{$f} = $opts{'override'}{$f};
  392. }
  393. unless($opts{'nocheck'}) {
  394. foreach my $f (qw(Source Version)) {
  395. unless (defined($fields->{$f})) {
  396. error(_g("missing information for critical output field %s"), $f);
  397. }
  398. }
  399. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  400. unless (defined($fields->{$f})) {
  401. warning(_g("missing information for output field %s"), $f);
  402. }
  403. }
  404. }
  405. foreach my $f (keys %{$opts{'remove'}}) {
  406. delete $fields->{$f};
  407. }
  408. my $filename = $opts{'filename'};
  409. unless (defined $filename) {
  410. $filename = $self->get_basename(1) . ".dsc";
  411. }
  412. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  413. delete $fields->{'Checksums-Md5'}; # identical with Files field
  414. $fields->apply_substvars($opts{'substvars'});
  415. $fields->output(\*DSC);
  416. close(DSC);
  417. }
  418. # vim: set et sw=4 ts=8
  419. 1;