Package.pm 17 KB

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