Native.pm 3.7 KB

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