Package.pm 17 KB

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