Package.pm 18 KB

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