Package.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. \.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. .shelf
  98. .svn
  99. CVS
  100. DEADJOE
  101. RCS
  102. _MTN
  103. _darcs
  104. {arch}
  105. );
  106. ## use critic
  107. =over 4
  108. =item my $string = get_default_diff_ignore_regex()
  109. Returns the default diff ignore regex.
  110. =cut
  111. sub get_default_diff_ignore_regex {
  112. return $diff_ignore_default_regex;
  113. }
  114. =item set_default_diff_ignore_regex($string)
  115. Set a regex as the new default diff ignore regex.
  116. =cut
  117. sub set_default_diff_ignore_regex {
  118. my ($regex) = @_;
  119. $diff_ignore_default_regex = $regex;
  120. }
  121. =item my @array = get_default_tar_ignore_pattern()
  122. Returns the default tar ignore pattern, as an array.
  123. =cut
  124. sub get_default_tar_ignore_pattern {
  125. return @tar_ignore_default_pattern;
  126. }
  127. =item $p = Dpkg::Source::Package->new(filename => $dscfile, options => {})
  128. Creates a new object corresponding to the source package described
  129. by the file $dscfile.
  130. The options hash supports the following options:
  131. =over 8
  132. =item skip_debianization
  133. If set to 1, do not apply Debian changes on the extracted source package.
  134. =item skip_patches
  135. If set to 1, do not apply Debian-specific patches. This options is
  136. specific for source packages using format "2.0" and "3.0 (quilt)".
  137. =item require_valid_signature
  138. If set to 1, the check_signature() method will be stricter and will error
  139. out if the signature can't be verified.
  140. =item copy_orig_tarballs
  141. If set to 1, the extraction will copy the upstream tarballs next the
  142. target directory. This is useful if you want to be able to rebuild the
  143. source package after its extraction.
  144. =back
  145. =cut
  146. # Object methods
  147. sub new {
  148. my ($this, %args) = @_;
  149. my $class = ref($this) || $this;
  150. my $self = {
  151. fields => Dpkg::Control->new(type => CTRL_PKG_SRC),
  152. options => {},
  153. checksums => Dpkg::Checksums->new(),
  154. };
  155. bless $self, $class;
  156. if (exists $args{options}) {
  157. $self->{options} = $args{options};
  158. }
  159. if (exists $args{filename}) {
  160. $self->initialize($args{filename});
  161. $self->init_options();
  162. }
  163. return $self;
  164. }
  165. sub init_options {
  166. my ($self) = @_;
  167. # Use full ignore list by default
  168. # note: this function is not called by V1 packages
  169. $self->{options}{diff_ignore_regex} //= $diff_ignore_default_regex;
  170. $self->{options}{diff_ignore_regex} .= '|(?:^|/)debian/source/local-.*$';
  171. if (defined $self->{options}{tar_ignore}) {
  172. $self->{options}{tar_ignore} = [ @tar_ignore_default_pattern ]
  173. unless @{$self->{options}{tar_ignore}};
  174. } else {
  175. $self->{options}{tar_ignore} = [ @tar_ignore_default_pattern ];
  176. }
  177. push @{$self->{options}{tar_ignore}}, 'debian/source/local-options',
  178. 'debian/source/local-patch-header';
  179. # Skip debianization while specific to some formats has an impact
  180. # on code common to all formats
  181. $self->{options}{skip_debianization} //= 0;
  182. # Set default compressor for new formats.
  183. $self->{options}{compression} //= 'xz';
  184. $self->{options}{comp_level} //= compression_get_property($self->{options}{compression},
  185. 'default_level');
  186. $self->{options}{comp_ext} //= compression_get_property($self->{options}{compression},
  187. 'file_ext');
  188. }
  189. sub initialize {
  190. my ($self, $filename) = @_;
  191. my ($fn, $dir) = fileparse($filename);
  192. error(_g('%s is not the name of a file'), $filename) unless $fn;
  193. $self->{basedir} = $dir || './';
  194. $self->{filename} = $fn;
  195. # Read the fields
  196. my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  197. $fields->load($filename);
  198. $self->{fields} = $fields;
  199. $self->{is_signed} = $fields->get_option('is_pgp_signed');
  200. foreach my $f (qw(Source Version Files)) {
  201. unless (defined($fields->{$f})) {
  202. error(_g('missing critical source control field %s'), $f);
  203. }
  204. }
  205. $self->{checksums}->add_from_control($fields, use_files_for_md5 => 1);
  206. $self->upgrade_object_type(0);
  207. }
  208. sub upgrade_object_type {
  209. my ($self, $update_format) = @_;
  210. $update_format //= 1;
  211. $self->{fields}{'Format'} //= '1.0';
  212. my $format = $self->{fields}{'Format'};
  213. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  214. my ($version, $variant, $major, $minor) = ($1, $2, $1, undef);
  215. if (defined $variant and $variant ne lc $variant) {
  216. error(_g("source package format '%s' is not supported: %s"),
  217. $format, _g('format variant must be in lowercase'));
  218. }
  219. $major =~ s/\.[\d\.]+$//;
  220. my $module = "Dpkg::Source::Package::V$major";
  221. $module .= '::' . ucfirst $variant if defined $variant;
  222. eval "require $module; \$minor = \$${module}::CURRENT_MINOR_VERSION;";
  223. $minor //= 0;
  224. if ($update_format) {
  225. $self->{fields}{'Format'} = "$major.$minor";
  226. $self->{fields}{'Format'} .= " ($variant)" if defined $variant;
  227. }
  228. if ($@) {
  229. error(_g("source package format '%s' is not supported: %s"),
  230. $format, $@);
  231. }
  232. bless $self, $module;
  233. } else {
  234. error(_g("invalid Format field `%s'"), $format);
  235. }
  236. }
  237. =item $p->get_filename()
  238. Returns the filename of the DSC file.
  239. =cut
  240. sub get_filename {
  241. my ($self) = @_;
  242. return $self->{basedir} . $self->{filename};
  243. }
  244. =item $p->get_files()
  245. Returns the list of files referenced by the source package. The filenames
  246. usually do not have any path information.
  247. =cut
  248. sub get_files {
  249. my ($self) = @_;
  250. return $self->{checksums}->get_files();
  251. }
  252. =item $p->check_checksums()
  253. Verify the checksums embedded in the DSC file. It requires the presence of
  254. the other files constituting the source package. If any inconsistency is
  255. discovered, it immediately errors out.
  256. =cut
  257. sub check_checksums {
  258. my ($self) = @_;
  259. my $checksums = $self->{checksums};
  260. # add_from_file verify the checksums if they are already existing
  261. foreach my $file ($checksums->get_files()) {
  262. $checksums->add_from_file($self->{basedir} . $file, key => $file);
  263. }
  264. }
  265. sub get_basename {
  266. my ($self, $with_revision) = @_;
  267. my $f = $self->{fields};
  268. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  269. error(_g('source and version are required to compute the source basename'));
  270. }
  271. my $v = Dpkg::Version->new($f->{'Version'});
  272. my $vs = $v->as_string(omit_epoch => 1, omit_revision => !$with_revision);
  273. return $f->{'Source'} . '_' . $vs;
  274. }
  275. sub find_original_tarballs {
  276. my ($self, %opts) = @_;
  277. $opts{extension} //= compression_get_file_extension_regex();
  278. $opts{include_main} //= 1;
  279. $opts{include_supplementary} //= 1;
  280. my $basename = $self->get_basename();
  281. my @tar;
  282. foreach my $dir ('.', $self->{basedir}, $self->{options}{origtardir}) {
  283. next unless defined($dir) and -d $dir;
  284. opendir(my $dir_dh, $dir) or syserr(_g('cannot opendir %s'), $dir);
  285. push @tar, map { "$dir/$_" } grep {
  286. ($opts{include_main} and
  287. /^\Q$basename\E\.orig\.tar\.$opts{extension}$/) or
  288. ($opts{include_supplementary} and
  289. /^\Q$basename\E\.orig-[[:alnum:]-]+\.tar\.$opts{extension}$/)
  290. } readdir($dir_dh);
  291. closedir($dir_dh);
  292. }
  293. return @tar;
  294. }
  295. =item $bool = $p->is_signed()
  296. Returns 1 if the DSC files contains an embedded OpenPGP signature.
  297. Otherwise returns 0.
  298. =cut
  299. sub is_signed {
  300. my $self = shift;
  301. return $self->{is_signed};
  302. }
  303. =item $p->check_signature()
  304. Implement the same OpenPGP signature check that dpkg-source does.
  305. In case of problems, it prints a warning or errors out.
  306. If the object has been created with the "require_valid_signature" option,
  307. then any problem will result in a fatal error.
  308. =cut
  309. sub check_signature {
  310. my ($self) = @_;
  311. my $dsc = $self->get_filename();
  312. my @exec;
  313. if (find_command('gpgv2')) {
  314. push @exec, 'gpgv2';
  315. } elsif (find_command('gpgv')) {
  316. push @exec, 'gpgv';
  317. } elsif (find_command('gpg2')) {
  318. push @exec, 'gpg2', '--no-default-keyring', '-q', '--verify';
  319. } elsif (find_command('gpg')) {
  320. push @exec, 'gpg', '--no-default-keyring', '-q', '--verify';
  321. }
  322. if (scalar(@exec)) {
  323. if (defined $ENV{HOME} and -r "$ENV{HOME}/.gnupg/trustedkeys.gpg") {
  324. push @exec, '--keyring', "$ENV{HOME}/.gnupg/trustedkeys.gpg";
  325. }
  326. foreach my $vendor_keyring (run_vendor_hook('keyrings')) {
  327. if (-r $vendor_keyring) {
  328. push @exec, '--keyring', $vendor_keyring;
  329. }
  330. }
  331. push @exec, $dsc;
  332. my ($stdout, $stderr);
  333. spawn(exec => \@exec, wait_child => 1, nocheck => 1,
  334. to_string => \$stdout, error_to_string => \$stderr,
  335. timeout => 10);
  336. if (WIFEXITED($?)) {
  337. my $gpg_status = WEXITSTATUS($?);
  338. print { *STDERR } "$stdout$stderr" if $gpg_status;
  339. if ($gpg_status == 1 or ($gpg_status &&
  340. $self->{options}{require_valid_signature}))
  341. {
  342. error(_g('failed to verify signature on %s'), $dsc);
  343. } elsif ($gpg_status) {
  344. warning(_g('failed to verify signature on %s'), $dsc);
  345. }
  346. } else {
  347. subprocerr("@exec");
  348. }
  349. } else {
  350. if ($self->{options}{require_valid_signature}) {
  351. error(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  352. } else {
  353. warning(_g("could not verify signature on %s since gpg isn't installed"), $dsc);
  354. }
  355. }
  356. }
  357. sub parse_cmdline_options {
  358. my ($self, @opts) = @_;
  359. foreach (@opts) {
  360. if (not $self->parse_cmdline_option($_)) {
  361. warning(_g('%s is not a valid option for %s'), $_, ref($self));
  362. }
  363. }
  364. }
  365. sub parse_cmdline_option {
  366. return 0;
  367. }
  368. =item $p->extract($targetdir)
  369. Extracts the source package in the target directory $targetdir. Beware
  370. that if $targetdir already exists, it will be erased.
  371. =cut
  372. sub extract {
  373. my $self = shift;
  374. my $newdirectory = $_[0];
  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(@_) };
  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. =head1 AUTHOR
  522. Raphaël Hertzog, E<lt>hertzog@debian.orgE<gt>
  523. =cut
  524. 1;