Package.pm 17 KB

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