Package.pm 18 KB

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