Fields.pm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package Dpkg::Fields;
  2. use strict;
  3. use warnings;
  4. use Exporter;
  5. use Dpkg::Deps qw(@src_dep_fields @pkg_dep_fields);
  6. our @ISA = qw(Exporter);
  7. our @EXPORT_OK = qw(capit %control_src_fields %control_pkg_fields
  8. $control_src_field_regex $control_pkg_field_regex);
  9. our %EXPORT_TAGS = ('list' => [qw(%control_src_fields %control_pkg_fields
  10. $control_src_field_regex $control_pkg_field_regex)]);
  11. # Some variables (list of fields)
  12. our %control_src_fields;
  13. our %control_pkg_fields;
  14. $control_src_fields{$_} = 1 foreach (qw(Bugs Dm-Upload-Allowed
  15. Homepage Origin Maintainer Priority Section Source Standards-Version
  16. Uploaders Vcs-Browser Vcs-Arch Vcs-Bzr Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg
  17. Vcs-Mtn Vcs-Svn));
  18. $control_src_fields{$_} = 1 foreach (@src_dep_fields);
  19. $control_pkg_fields{$_} = 1 foreach (qw(Architecture Bugs Description Essential
  20. Homepage Installer-Menu-Item Kernel-Version Package Package-Type
  21. Priority Section Subarchitecture Tag));
  22. $control_pkg_fields{$_} = 1 foreach (@pkg_dep_fields);
  23. our $control_src_field_regex = "(?:" . join("|", keys %control_src_fields) . ")";
  24. our $control_pkg_field_regex = "(?:" . join("|", keys %control_pkg_fields) . ")";
  25. # Some functions
  26. sub capit {
  27. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  28. return join '-', @pieces;
  29. }
  30. package Dpkg::Fields::Object;
  31. =head1 OTHER OBJECTS
  32. =head2 Dpkg::Fields::Object
  33. This object is used to tie a hash. It implements hash-like functions by
  34. normalizing the name of fields received in keys (using
  35. Dpkg::Fields::capit). It also stores the order in which fields have been
  36. added in order to be able to dump them in the same order.
  37. =cut
  38. use Tie::Hash;
  39. our @ISA = qw(Tie::ExtraHash Tie::Hash);
  40. use Dpkg::ErrorHandling qw(internerr syserr);
  41. use Dpkg::Gettext;
  42. # Import capit
  43. Dpkg::Fields->import('capit');
  44. # $self->[0] is the real hash
  45. # $self->[1] is an array containing the ordered list of keys
  46. # $self->[2] is an hash describing the relative importance of each field
  47. # (used to sort the output).
  48. =head2 Dpkg::Fields::Object->new()
  49. Return a reference to a tied hash implementing storage of simple
  50. "field: value" mapping as used in many Debian-specific files.
  51. =cut
  52. sub new {
  53. my $hash = {};
  54. tie %{$hash}, 'Dpkg::Fields::Object';
  55. return $hash;
  56. }
  57. sub TIEHASH {
  58. my $class = shift;
  59. return bless [{}, [], {}], $class;
  60. }
  61. sub FETCH {
  62. my ($self, $key) = @_;
  63. $key = capit($key);
  64. return $self->[0]->{$key} if exists $self->[0]->{$key};
  65. return undef;
  66. }
  67. sub STORE {
  68. my ($self, $key, $value) = @_;
  69. $key = capit($key);
  70. if (not exists $self->[0]->{$key}) {
  71. push @{$self->[1]}, $key;
  72. }
  73. $self->[0]->{$key} = $value;
  74. }
  75. sub EXISTS {
  76. my ($self, $key) = @_;
  77. $key = capit($key);
  78. return exists $self->[0]->{$key};
  79. }
  80. sub DELETE {
  81. my ($self, $key) = @_;
  82. $key = capit($key);
  83. if (exists $self->[0]->{$key}) {
  84. delete $self->[0]->{$key};
  85. @{$self->[1]} = grep { $_ ne $key } @{$self->[1]};
  86. return 1;
  87. } else {
  88. return 0;
  89. }
  90. }
  91. sub FIRSTKEY {
  92. my $self = shift;
  93. foreach (@{$self->[1]}) {
  94. return $_ if exists $self->[0]->{$_};
  95. }
  96. }
  97. sub NEXTKEY {
  98. my ($self, $last) = @_;
  99. my $found = 0;
  100. foreach (@{$self->[1]}) {
  101. if ($found) {
  102. return $_ if exists $self->[0]->{$_};
  103. } else {
  104. $found = 1 if $_ eq $last;
  105. }
  106. }
  107. return undef;
  108. }
  109. =head2 my $str = tied(%hash)->dump()
  110. =head2 tied(%hash)->dump($fh)
  111. Dump the raw content of the hash either as a string or to a filehandle.
  112. =cut
  113. sub dump {
  114. my ($self, $fh) = @_;
  115. my $str = "";
  116. foreach (@{$self->[1]}) {
  117. if (exists $self->[0]->{$_}) {
  118. print $fh "$_: " . $self->[0]->{$_} . "\n" if $fh;
  119. $str .= "$_: " . $self->[0]->{$_} . "\n" if defined wantarray;
  120. }
  121. }
  122. return $str;
  123. }
  124. =head2 tied(%hash)->set_field_importance(@fields)
  125. Define the order in which fields will be displayed in the output() method.
  126. =cut
  127. sub set_field_importance {
  128. my ($self, @fields) = @_;
  129. my $i = 1;
  130. $self->[2] = {};
  131. $self->[2]{$_} = $i++ foreach (@fields);
  132. }
  133. =head2 tied(%hash)->output($fh, $substvars)
  134. If $fh is defined, print the fields on the $fh filehandle after
  135. substitution of variables defined in the Dpkg::Substvars object.
  136. Also returns the string of what would printed on the filehandle.
  137. =cut
  138. sub output {
  139. my ($self, $fh, $substvars) = @_;
  140. my $str = "";
  141. my $imp = $self->[2]; # Hash of relative importance
  142. # Add substvars to refer to other fields
  143. if (defined($substvars)) {
  144. foreach my $f (keys %{$self->[0]}) {
  145. $substvars->set("F:$f", $self->[0]->{$f});
  146. }
  147. }
  148. my @keys = sort {
  149. if (defined $imp->{$a} && defined $imp->{$b}) {
  150. $imp->{$a} <=> $imp->{$b};
  151. } elsif (defined($imp->{$a})) {
  152. -1;
  153. } elsif (defined($imp->{$b})) {
  154. 1;
  155. } else {
  156. $a cmp $b;
  157. }
  158. } keys %{$self->[0]};
  159. foreach my $f (@keys) {
  160. my $v = $self->[0]->{$f};
  161. if (defined($substvars)) {
  162. $v = $substvars->substvars($v);
  163. }
  164. $v =~ m/\S/ || next; # delete whitespace-only fields
  165. $v =~ m/\n\S/ &&
  166. internerr(_g("field %s has newline then non whitespace >%s<"),
  167. $f, $v);
  168. $v =~ m/\n[ \t]*\n/ &&
  169. internerr(_g("field %s has blank lines >%s<"), $f, $v);
  170. $v =~ m/\n$/ &&
  171. internerr(_g("field %s has trailing newline >%s<"), $f, $v);
  172. if (defined($substvars)) {
  173. $v =~ s/,[\s,]*,/,/g;
  174. $v =~ s/^\s*,\s*//;
  175. $v =~ s/\s*,\s*$//;
  176. }
  177. $v =~ s/\$\{\}/\$/g;
  178. if ($fh) {
  179. print $fh "$f: $v\n" || syserr(_g("write error on control data"));
  180. }
  181. if (defined wantarray) {
  182. $str .= "$f: $v\n";
  183. }
  184. }
  185. return $str;
  186. }
  187. 1;