Package.pm 17 KB

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