Native.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright © 2008 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::V3::Native;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use parent qw(Dpkg::Source::Package);
  20. use Dpkg;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Compression;
  24. use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
  25. use Dpkg::Version;
  26. use Dpkg::Source::Archive;
  27. use Dpkg::Source::Functions qw(erasedir);
  28. use Cwd;
  29. use File::Basename;
  30. use File::Temp qw(tempfile);
  31. our $CURRENT_MINOR_VERSION = '0';
  32. sub do_extract {
  33. my ($self, $newdirectory) = @_;
  34. my $sourcestyle = $self->{options}{sourcestyle};
  35. my $fields = $self->{fields};
  36. my $dscdir = $self->{basedir};
  37. my $basename = $self->get_basename();
  38. my $basenamerev = $self->get_basename(1);
  39. my $tarfile;
  40. my $comp_ext_regex = compression_get_file_extension_regex();
  41. foreach my $file ($self->get_files()) {
  42. if ($file =~ /^\Q$basenamerev\E\.tar\.$comp_ext_regex$/) {
  43. error(_g('multiple tarfiles in v1.0 source package')) if $tarfile;
  44. $tarfile = $file;
  45. } else {
  46. error(_g('unrecognized file for a native source package: %s'), $file);
  47. }
  48. }
  49. error(_g('no tarfile in Files field')) unless $tarfile;
  50. erasedir($newdirectory);
  51. info(_g('unpacking %s'), $tarfile);
  52. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  53. $tar->extract($newdirectory);
  54. }
  55. sub can_build {
  56. my ($self, $dir) = @_;
  57. my $v = Dpkg::Version->new($self->{fields}->{'Version'});
  58. return (0, _g('native package version may not have a revision'))
  59. unless $v->is_native();
  60. return 1;
  61. }
  62. sub do_build {
  63. my ($self, $dir) = @_;
  64. my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
  65. my @argv = @{$self->{options}{ARGV}};
  66. if (scalar(@argv)) {
  67. usageerr(_g("-b takes only one parameter with format `%s'"),
  68. $self->{fields}{'Format'});
  69. }
  70. my $sourcepackage = $self->{fields}{'Source'};
  71. my $basenamerev = $self->get_basename(1);
  72. my $tarname = "$basenamerev.tar." . $self->{options}{comp_ext};
  73. info(_g('building %s in %s'), $sourcepackage, $tarname);
  74. my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
  75. DIR => getcwd(), UNLINK => 0);
  76. push_exit_handler(sub { unlink($newtar) });
  77. my ($dirname, $dirbase) = fileparse($dir);
  78. my $tar = Dpkg::Source::Archive->new(filename => $newtar,
  79. compression => compression_guess_from_filename($tarname),
  80. compression_level => $self->{options}{comp_level});
  81. $tar->create(options => \@tar_ignore, chdir => $dirbase);
  82. $tar->add_directory($dirname);
  83. $tar->finish();
  84. rename($newtar, $tarname)
  85. or syserr(_g("unable to rename `%s' (newly created) to `%s'"),
  86. $newtar, $tarname);
  87. pop_exit_handler();
  88. chmod(0666 &~ umask(), $tarname)
  89. or syserr(_g("unable to change permission of `%s'"), $tarname);
  90. $self->add_file($tarname);
  91. }
  92. 1;