Files.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright © 2014-2015 Guillem Jover <guillem@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::Dist::Files;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use parent qw(Dpkg::Interface::Storable);
  22. sub new {
  23. my ($this, %opts) = @_;
  24. my $class = ref($this) || $this;
  25. my $self = {
  26. options => [],
  27. files => {},
  28. };
  29. foreach my $opt (keys %opts) {
  30. $self->{$opt} = $opts{$opt};
  31. }
  32. bless $self, $class;
  33. return $self;
  34. }
  35. sub reset {
  36. my $self = shift;
  37. $self->{files} = {};
  38. }
  39. sub parse_filename {
  40. my ($self, $fn) = @_;
  41. my $file;
  42. if ($fn =~ m/^(([-+.0-9a-z]+)_([^_]+)_([-\w]+)\.([a-z0-9.]+))$/) {
  43. $file->{filename} = $1;
  44. $file->{package} = $2;
  45. $file->{version} = $3;
  46. $file->{arch} = $4;
  47. $file->{package_type} = $5;
  48. } elsif ($fn =~ m/^([-+.,_0-9a-zA-Z]+)$/) {
  49. $file->{filename} = $1;
  50. } else {
  51. $file = undef;
  52. }
  53. return $file;
  54. }
  55. sub parse {
  56. my ($self, $fh, $desc) = @_;
  57. my $count = 0;
  58. local $_;
  59. binmode $fh;
  60. while (<$fh>) {
  61. chomp;
  62. my $file;
  63. if (m/^(\S+) (\S+) (\S+)$/) {
  64. $file = $self->parse_filename($1);
  65. error(g_('badly formed package name in files list file, line %d'), $.)
  66. unless defined $file;
  67. $file->{section} = $2;
  68. $file->{priority} = $3;
  69. } else {
  70. error(g_('badly formed line in files list file, line %d'), $.);
  71. }
  72. if (defined $self->{files}->{$file->{filename}}) {
  73. warning(g_('duplicate files list entry for file %s (line %d)'),
  74. $file->{filename}, $.);
  75. } else {
  76. $count++;
  77. $self->{files}->{$file->{filename}} = $file;
  78. }
  79. }
  80. return $count;
  81. }
  82. sub get_files {
  83. my $self = shift;
  84. return map { $self->{files}->{$_} } sort keys %{$self->{files}};
  85. }
  86. sub get_file {
  87. my ($self, $filename) = @_;
  88. return $self->{files}->{$filename};
  89. }
  90. sub add_file {
  91. my ($self, $filename, $section, $priority) = @_;
  92. my $file = $self->parse_filename($filename);
  93. error(g_('invalid filename %s'), $filename) unless defined $file;
  94. $file->{section} = $section;
  95. $file->{priority} = $priority;
  96. $self->{files}->{$filename} = $file;
  97. }
  98. sub del_file {
  99. my ($self, $filename) = @_;
  100. delete $self->{files}->{$filename};
  101. }
  102. sub filter {
  103. my ($self, %opts) = @_;
  104. my $remove = $opts{remove} // sub { 0 };
  105. my $keep = $opts{keep} // sub { 1 };
  106. foreach my $filename (keys %{$self->{files}}) {
  107. my $file = $self->{files}->{$filename};
  108. if (not &$keep($file) or &$remove($file)) {
  109. delete $self->{files}->{$filename};
  110. }
  111. }
  112. }
  113. sub output {
  114. my ($self, $fh) = @_;
  115. my $str = '';
  116. binmode $fh if defined $fh;
  117. foreach my $filename (sort keys %{$self->{files}}) {
  118. my $file = $self->{files}->{$filename};
  119. my $entry = "$filename $file->{section} $file->{priority}\n";
  120. print { $fh } $entry if defined $fh;
  121. $str .= $entry;
  122. }
  123. return $str;
  124. }
  125. 1;