Package.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. # Copyright © 2008-2011 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. =encoding utf8
  17. =head1 NAME
  18. Dpkg::Source::Package - manipulate Debian source packages
  19. =head1 DESCRIPTION
  20. This module provides an object that can manipulate Debian source
  21. packages. While it supports both the extraction and the creation
  22. of source packages, the only API that is officially supported
  23. is the one that supports the extraction of the source package.
  24. =head1 FUNCTIONS
  25. =cut
  26. use strict;
  27. use warnings;
  28. our $VERSION = "1.0";
  29. use Dpkg::Gettext;
  30. use Dpkg::ErrorHandling;
  31. use Dpkg::Control;
  32. use Dpkg::Checksums;
  33. use Dpkg::Version;
  34. use Dpkg::Compression;
  35. use Dpkg::Exit;
  36. use Dpkg::Path qw(check_files_are_the_same find_command);
  37. use Dpkg::IPC;
  38. use Dpkg::Vendor qw(run_vendor_hook);
  39. use POSIX;
  40. use File::Basename;
  41. # Public variables
  42. our $diff_ignore_default_regexp = '
  43. # Ignore general backup files
  44. (?:^|/).*~$|
  45. # Ignore emacs recovery files
  46. (?:^|/)\.#.*$|
  47. # Ignore vi swap files
  48. (?:^|/)\..*\.sw.$|
  49. # Ignore baz-style junk files or directories
  50. (?:^|/),,.*(?:$|/.*$)|
  51. # File-names that should be ignored (never directories)
  52. (?:^|/)(?:DEADJOE|\.arch-inventory|\.(?:bzr|cvs|hg|git)ignore)$|
  53. # File or directory names that should be ignored
  54. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|
  55. \.hg(?:tags|sigs)?|_darcs|\.git(?:attributes|modules)?|
  56. \.shelf|_MTN|\.be|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  57. ';
  58. # Take out comments and newlines
  59. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  60. $diff_ignore_default_regexp =~ s/\n//sg;
  61. no warnings 'qw'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
  62. our @tar_ignore_default_pattern = qw(
  63. *.a
  64. *.la
  65. *.o
  66. *.so
  67. .*.sw?
  68. */*~
  69. ,,*
  70. .[#~]*
  71. .arch-ids
  72. .arch-inventory
  73. .be
  74. .bzr
  75. .bzr.backup
  76. .bzr.tags
  77. .bzrignore
  78. .cvsignore
  79. .deps
  80. .git
  81. .gitattributes
  82. .gitignore
  83. .gitmodules
  84. .hg
  85. .hgignore
  86. .hgsigs
  87. .hgtags
  88. .shelf
  89. .svn
  90. CVS
  91. DEADJOE
  92. RCS
  93. _MTN
  94. _darcs
  95. {arch}
  96. );
  97. =over 4
  98. =item $p = Dpkg::Source::Package->new(filename => $dscfile, options => {})
  99. Creates a new object corresponding to the source package described
  100. by the file $dscfile.
  101. The options hash supports the following options:
  102. =over 8
  103. =item skip_debianization
  104. If set to 1, do not apply Debian changes on the extracted source package.
  105. =item skip_patches
  106. If set to 1, do not apply Debian-specific patches. This options is
  107. specific for source packages using format "2.0" and "3.0 (quilt)".
  108. =item require_valid_signature
  109. If set to 1, the check_signature() method will be stricter and will error
  110. out if the signature can't be verified.
  111. =item copy_orig_tarballs
  112. If set to 1, the extraction will copy the upstream tarballs next the
  113. target directory. This is useful if you want to be able to rebuild the
  114. source package after its extraction.
  115. =back
  116. =cut
  117. # Object methods
  118. sub new {
  119. my ($this, %args) = @_;
  120. my $class = ref($this) || $this;
  121. my $self = {
  122. 'fields' => Dpkg::Control->new(type => CTRL_PKG_SRC),
  123. 'options' => {},
  124. 'checksums' => Dpkg::Checksums->new(),
  125. };
  126. bless $self, $class;
  127. if (exists $args{'options'}) {
  128. $self->{'options'} = $args{'options'};
  129. }
  130. if (exists $args{"filename"}) {
  131. $self->initialize($args{"filename"});
  132. $self->init_options();
  133. }
  134. return $self;
  135. }
  136. sub init_options {
  137. my ($self) = @_;
  138. # Use full ignore list by default
  139. # note: this function is not called by V1 packages
  140. $self->{'options'}{'diff_ignore_regexp'} ||= $diff_ignore_default_regexp;
  141. $self->{'options'}{'diff_ignore_regexp'} .= '|(?:^|/)debian/source/local-.*$';
  142. if (defined $self->{'options'}{'tar_ignore'}) {
  143. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ]
  144. unless @{$self->{'options'}{'tar_ignore'}};
  145. } else {
  146. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ];
  147. }
  148. push @{$self->{'options'}{'tar_ignore'}}, "debian/source/local-options",
  149. "debian/source/local-patch-header";
  150. # Skip debianization while specific to some formats has an impact
  151. # on code common to all formats
  152. $self->{'options'}{'skip_debianization'} ||= 0;
  153. }
  154. sub initialize {
  155. my ($self, $filename) = @_;
  156. my ($fn, $dir) = fileparse($filename);
  157. error(_g("%s is not the name of a file"), $filename) unless $fn;
  158. $self->{'basedir'} = $dir || "./";
  159. $self->{'filename'} = $fn;
  160. # Check if it contains a signature
  161. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  162. $self->{'is_signed'} = 0;
  163. while (<DSC>) {
  164. next if /^\s*$/o;
  165. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----\s*$/o;
  166. last;
  167. }
  168. close(DSC);
  169. # Read the fields
  170. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  171. $fields->load($filename);
  172. $self->{'fields'} = $fields;
  173. foreach my $f (qw(Source Version Files)) {
  174. unless (defined($fields->{$f})) {
  175. error(_g("missing critical source control field %s"), $f);
  176. }
  177. }
  178. $self->{'checksums'}->add_from_control($fields, use_files_for_md5 => 1);
  179. $self->upgrade_object_type(0);
  180. }
  181. sub upgrade_object_type {
  182. my ($self, $update_format) = @_;
  183. $update_format = 1 unless defined $update_format;
  184. $self->{'fields'}{'Format'} = '1.0'
  185. unless exists $self->{'fields'}{'Format'};
  186. my $format = $self->{'fields'}{'Format'};
  187. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  188. my ($version, $variant, $major, $minor) = ($1, $2, $1, undef);
  189. $major =~ s/\.[\d\.]+$//;
  190. my $module = "Dpkg::Source::Package::V$major";
  191. $module .= "::$variant" if defined $variant;
  192. eval "require $module; \$minor = \$${module}::CURRENT_MINOR_VERSION;";
  193. $minor = 0 unless defined $minor;
  194. if ($update_format) {
  195. $self->{'fields'}{'Format'} = "$major.$minor";
  196. $self->{'fields'}{'Format'} .= " ($variant)" if defined $variant;
  197. }
  198. if ($@) {
  199. error(_g("source package format '%s' is not supported: %s"),
  200. $format, $@);
  201. }
  202. bless $self, $module;
  203. } else {
  204. error(_g("invalid Format field `%s'"), $format);
  205. }
  206. }
  207. =item $p->get_filename()
  208. Returns the filename of the DSC file.
  209. =cut
  210. sub get_filename {
  211. my ($self) = @_;
  212. return $self->{'basedir'} . $self->{'filename'};
  213. }
  214. =item $p->get_files()
  215. Returns the list of files referenced by the source package. The filenames
  216. usually do not have any path information.
  217. =cut
  218. sub get_files {
  219. my ($self) = @_;
  220. return $self->{'checksums'}->get_files();
  221. }
  222. =item $p->check_checksums()
  223. Verify the checksums embedded in the DSC file. It requires the presence of
  224. the other files constituting the source package. If any inconsistency is
  225. discovered, it immediately errors out.
  226. =cut
  227. sub check_checksums {
  228. my ($self) = @_;
  229. my $checksums = $self->{'checksums'};
  230. # add_from_file verify the checksums if they are already existing
  231. foreach my $file ($checksums->get_files()) {
  232. $checksums->add_from_file($self->{'basedir'} . $file, key => $file);
  233. }
  234. }
  235. sub get_basename {
  236. my ($self, $with_revision) = @_;
  237. my $f = $self->{'fields'};
  238. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  239. error(_g("source and version are required to compute the source basename"));
  240. }
  241. my $v = Dpkg::Version->new($f->{'Version'});
  242. my $basename = $f->{'Source'} . "_" . $v->version();
  243. if ($with_revision and $f->{'Version'} =~ /-/) {
  244. $basename .= "-" . $v->revision();
  245. }
  246. return $basename;
  247. }
  248. sub find_original_tarballs {
  249. my ($self, %opts) = @_;
  250. $opts{extension} = $compression_re_file_ext unless exists $opts{extension};
  251. $opts{include_main} = 1 unless exists $opts{include_main};
  252. $opts{include_supplementary} = 1 unless exists $opts{include_supplementary};
  253. my $basename = $self->get_basename();
  254. my @tar;
  255. foreach my $dir (".", $self->{'basedir'}, $self->{'options'}{'origtardir'}) {
  256. next unless defined($dir) and -d $dir;
  257. opendir(DIR, $dir) || syserr(_g("cannot opendir %s"), $dir);
  258. push @tar, map { "$dir/$_" } grep {
  259. ($opts{include_main} and
  260. /^\Q$basename\E\.orig\.tar\.$opts{extension}$/) or
  261. ($opts{include_supplementary} and
  262. /^\Q$basename\E\.orig-[[:alnum:]-]+\.tar\.$opts{extension}$/)
  263. } readdir(DIR);
  264. closedir(DIR);
  265. }
  266. return @tar;
  267. }
  268. =item $bool = $p->is_signed()
  269. Returns 1 if the DSC files contains an embedded OpenPGP signature.
  270. Otherwise returns 0.
  271. =cut
  272. sub is_signed {
  273. my $self = shift;
  274. return $self->{'is_signed'};
  275. }
  276. =item $p->check_signature()
  277. Implement the same OpenPGP signature check that dpkg-source does.
  278. In case of problems, it prints a warning or errors out.
  279. If the object has been created with the "require_valid_signature" option,
  280. then any problem will result in a fatal error.
  281. =cut
  282. sub check_signature {
  283. my ($self) = @_;
  284. my $dsc = $self->get_filename();
  285. my @exec;
  286. if (find_command('gpgv')) {
  287. push @exec, "gpgv";
  288. } elsif (find_command('gpg')) {
  289. push @exec, "gpg", "--no-default-keyring", "-q", "--verify";
  290. }
  291. if (scalar(@exec)) {
  292. if (defined $ENV{'HOME'} and -r "$ENV{'HOME'}/.gnupg/trustedkeys.gpg") {
  293. push @exec, "--keyring", "$ENV{'HOME'}/.gnupg/trustedkeys.gpg";
  294. }
  295. foreach my $vendor_keyring (run_vendor_hook('keyrings')) {
  296. if (-r $vendor_keyring) {
  297. push @exec, "--keyring", $vendor_keyring;
  298. }
  299. }
  300. push @exec, $dsc;
  301. my ($stdout, $stderr);
  302. spawn('exec' => \@exec, wait_child => 1, nocheck => 1,
  303. to_string => \$stdout, error_to_string => \$stderr,
  304. timeout => 10);
  305. if (WIFEXITED($?)) {
  306. my $gpg_status = WEXITSTATUS($?);
  307. print STDERR "$stdout$stderr" if $gpg_status;
  308. if ($gpg_status == 1 or ($gpg_status &&
  309. $self->{'options'}{'require_valid_signature'}))
  310. {
  311. error(_g("failed to verify signature on %s"), $dsc);
  312. } elsif ($gpg_status) {
  313. warning(_g("failed to verify signature on %s"), $dsc);
  314. }
  315. } else {
  316. subprocerr("@exec");
  317. }
  318. } else {
  319. if ($self->{'options'}{'require_valid_signature'}) {
  320. error(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  321. } else {
  322. warning(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  323. }
  324. }
  325. }
  326. sub parse_cmdline_options {
  327. my ($self, @opts) = @_;
  328. foreach (@opts) {
  329. if (not $self->parse_cmdline_option($_)) {
  330. warning(_g("%s is not a valid option for %s"), $_, ref($self));
  331. }
  332. }
  333. }
  334. sub parse_cmdline_option {
  335. return 0;
  336. }
  337. =item $p->extract($targetdir)
  338. Extracts the source package in the target directory $targetdir. Beware
  339. that if $targetdir already exists, it will be erased.
  340. =cut
  341. sub extract {
  342. my $self = shift;
  343. my $newdirectory = $_[0];
  344. my ($ok, $error) = version_check($self->{'fields'}{'Version'});
  345. error($error) unless $ok;
  346. # Copy orig tarballs
  347. if ($self->{'options'}{'copy_orig_tarballs'}) {
  348. my $basename = $self->get_basename();
  349. my ($dirname, $destdir) = fileparse($newdirectory);
  350. $destdir ||= "./";
  351. my $ext = $compression_re_file_ext;
  352. foreach my $orig (grep { /^\Q$basename\E\.orig(-[[:alnum:]-]+)?\.tar\.$ext$/ }
  353. $self->get_files())
  354. {
  355. my $src = File::Spec->catfile($self->{'basedir'}, $orig);
  356. my $dst = File::Spec->catfile($destdir, $orig);
  357. if (not check_files_are_the_same($src, $dst, 1)) {
  358. system('cp', '--', $src, $dst);
  359. subprocerr("cp $src to $dst") if $?;
  360. }
  361. }
  362. }
  363. # Try extract
  364. eval { $self->do_extract(@_) };
  365. if ($@) {
  366. &$_() foreach reverse @Dpkg::Exit::handlers;
  367. die $@;
  368. }
  369. # Store format if non-standard so that next build keeps the same format
  370. if ($self->{'fields'}{'Format'} ne "1.0" and
  371. not $self->{'options'}{'skip_debianization'})
  372. {
  373. my $srcdir = File::Spec->catdir($newdirectory, "debian", "source");
  374. my $format_file = File::Spec->catfile($srcdir, "format");
  375. unless (-e $format_file) {
  376. mkdir($srcdir) unless -e $srcdir;
  377. open(FORMAT, ">", $format_file) || syserr(_g("cannot write %s"), $format_file);
  378. print FORMAT $self->{'fields'}{'Format'} . "\n";
  379. close(FORMAT);
  380. }
  381. }
  382. # Make sure debian/rules is executable
  383. my $rules = File::Spec->catfile($newdirectory, "debian", "rules");
  384. my @s = lstat($rules);
  385. if (not scalar(@s)) {
  386. unless ($! == ENOENT) {
  387. syserr(_g("cannot stat %s"), $rules);
  388. }
  389. warning(_g("%s does not exist"), $rules)
  390. unless $self->{'options'}{'skip_debianization'};
  391. } elsif (-f _) {
  392. chmod($s[2] | 0111, $rules) ||
  393. syserr(_g("cannot make %s executable"), $rules);
  394. } else {
  395. warning(_g("%s is not a plain file"), $rules);
  396. }
  397. }
  398. sub do_extract {
  399. internerr("Dpkg::Source::Package doesn't know how to unpack a " .
  400. "source package. Use one of the subclasses.");
  401. }
  402. # Function used specifically during creation of a source package
  403. sub before_build {
  404. my ($self, $dir) = @_;
  405. }
  406. sub build {
  407. my $self = shift;
  408. eval { $self->do_build(@_) };
  409. if ($@) {
  410. &$_() foreach reverse @Dpkg::Exit::handlers;
  411. die $@;
  412. }
  413. }
  414. sub after_build {
  415. my ($self, $dir) = @_;
  416. }
  417. sub do_build {
  418. internerr("Dpkg::Source::Package doesn't know how to build a " .
  419. "source package. Use one of the subclasses.");
  420. }
  421. sub can_build {
  422. my ($self, $dir) = @_;
  423. return (0, "can_build() has not been overriden");
  424. }
  425. sub add_file {
  426. my ($self, $filename) = @_;
  427. my ($fn, $dir) = fileparse($filename);
  428. if ($self->{'checksums'}->has_file($fn)) {
  429. internerr("tried to add file '%s' twice", $fn);
  430. }
  431. $self->{'checksums'}->add_from_file($filename, key => $fn);
  432. $self->{'checksums'}->export_to_control($self->{'fields'},
  433. use_files_for_md5 => 1);
  434. }
  435. sub commit {
  436. my $self = shift;
  437. eval { $self->do_commit(@_) };
  438. if ($@) {
  439. &$_() foreach reverse @Dpkg::Exit::handlers;
  440. die $@;
  441. }
  442. }
  443. sub do_commit {
  444. my ($self, $dir) = @_;
  445. info(_g("'%s' is not supported by the source format '%s'"),
  446. "dpkg-source --commit", $self->{'fields'}{'Format'});
  447. }
  448. sub write_dsc {
  449. my ($self, %opts) = @_;
  450. my $fields = $self->{'fields'};
  451. foreach my $f (keys %{$opts{'override'}}) {
  452. $fields->{$f} = $opts{'override'}{$f};
  453. }
  454. unless($opts{'nocheck'}) {
  455. foreach my $f (qw(Source Version)) {
  456. unless (defined($fields->{$f})) {
  457. error(_g("missing information for critical output field %s"), $f);
  458. }
  459. }
  460. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  461. unless (defined($fields->{$f})) {
  462. warning(_g("missing information for output field %s"), $f);
  463. }
  464. }
  465. }
  466. foreach my $f (keys %{$opts{'remove'}}) {
  467. delete $fields->{$f};
  468. }
  469. my $filename = $opts{'filename'};
  470. unless (defined $filename) {
  471. $filename = $self->get_basename(1) . ".dsc";
  472. }
  473. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  474. $fields->apply_substvars($opts{'substvars'});
  475. $fields->output(\*DSC);
  476. close(DSC);
  477. }
  478. =back
  479. =head1 AUTHOR
  480. Raphaël Hertzog, E<lt>hertzog@debian.orgE<gt>
  481. =cut
  482. 1;