Package.pm 17 KB

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