Package.pm 18 KB

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