Package.pm 18 KB

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