Package.pm 16 KB

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