Package.pm 16 KB

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