Package.pm 16 KB

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