Fields.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. # Import capit
  42. Dpkg::Fields->import('capit');
  43. # $self->[0] is the real hash
  44. # $self->[1] is an array containing the ordered list of keys
  45. # $self->[2] is an hash describing the relative importance of each field
  46. # (used to sort the output).
  47. =head2 Dpkg::Fields::Object->new()
  48. Return a reference to a tied hash implementing storage of simple
  49. "field: value" mapping as used in many Debian-specific files.
  50. =cut
  51. sub new {
  52. my $hash = {};
  53. tie %{$hash}, 'Dpkg::Fields::Object';
  54. return $hash;
  55. }
  56. sub TIEHASH {
  57. my $class = shift;
  58. return bless [{}, [], {}], $class;
  59. }
  60. sub FETCH {
  61. my ($self, $key) = @_;
  62. $key = capit($key);
  63. return $self->[0]->{$key} if exists $self->[0]->{$key};
  64. return undef;
  65. }
  66. sub STORE {
  67. my ($self, $key, $value) = @_;
  68. $key = capit($key);
  69. if (not exists $self->[0]->{$key}) {
  70. push @{$self->[1]}, $key;
  71. }
  72. $self->[0]->{$key} = $value;
  73. }
  74. sub EXISTS {
  75. my ($self, $key) = @_;
  76. $key = capit($key);
  77. return exists $self->[0]->{$key};
  78. }
  79. sub DELETE {
  80. my ($self, $key) = @_;
  81. $key = capit($key);
  82. if (exists $self->[0]->{$key}) {
  83. delete $self->[0]->{$key};
  84. @{$self->[1]} = grep { $_ ne $key } @{$self->[1]};
  85. return 1;
  86. } else {
  87. return 0;
  88. }
  89. }
  90. sub FIRSTKEY {
  91. my $self = shift;
  92. foreach (@{$self->[1]}) {
  93. return $_ if exists $self->[0]->{$_};
  94. }
  95. }
  96. sub NEXTKEY {
  97. my ($self, $last) = @_;
  98. my $found = 0;
  99. foreach (@{$self->[1]}) {
  100. if ($found) {
  101. return $_ if exists $self->[0]->{$_};
  102. } else {
  103. $found = 1 if $_ eq $last;
  104. }
  105. }
  106. return undef;
  107. }
  108. =head2 my $str = tied(%hash)->dump()
  109. =head2 tied(%hash)->dump($fh)
  110. Dump the raw content of the hash either as a string or to a filehandle.
  111. =cut
  112. sub dump {
  113. my ($self, $fh) = @_;
  114. my $str = "";
  115. foreach (@{$self->[1]}) {
  116. if (exists $self->[0]->{$_}) {
  117. print $fh "$_: " . $self->[0]->{$_} . "\n" if $fh;
  118. $str .= "$_: " . $self->[0]->{$_} . "\n" if defined wantarray;
  119. }
  120. }
  121. return $str;
  122. }
  123. =head2 tied(%hash)->set_field_importance(@fields)
  124. Define the order in which fields will be displayed in the output() method.
  125. =cut
  126. sub set_field_importance {
  127. my ($self, @fields) = @_;
  128. my $i = 1;
  129. $self->[2] = {};
  130. $self->[2]{$_} = $i++ foreach (@fields);
  131. }
  132. =head2 tied(%hash)->output($fh, $substvars)
  133. If $fh is defined, print the fields on the $fh filehandle after
  134. substitution of variables defined in the Dpkg::Substvars object.
  135. Also returns the string of what would printed on the filehandle.
  136. =cut
  137. sub output {
  138. my ($self, $fh, $substvars) = @_;
  139. my $str = "";
  140. my $imp = $self->[2]; # Hash of relative importance
  141. # Add substvars to refer to other fields
  142. if (defined($substvars)) {
  143. foreach my $f (keys %{$self->[0]}) {
  144. $substvars->set("F:$f", $self->[0]->{$f});
  145. }
  146. }
  147. my @keys = sort {
  148. if (defined $imp->{$a} && defined $imp->{$b}) {
  149. $imp->{$a} <=> $imp->{$b};
  150. } elsif (defined($imp->{$a})) {
  151. -1;
  152. } elsif (defined($imp->{$b})) {
  153. 1;
  154. } else {
  155. $a cmp $b;
  156. }
  157. } keys %{$self->[0]};
  158. foreach my $f (@keys) {
  159. my $v = $self->[0]->{$f};
  160. if (defined($substvars)) {
  161. $v = $substvars->substvars($v);
  162. }
  163. $v =~ m/\S/ || next; # delete whitespace-only fields
  164. $v =~ m/\n\S/ &&
  165. internerr(_g("field %s has newline then non whitespace >%s<"),
  166. $f, $v);
  167. $v =~ m/\n[ \t]*\n/ &&
  168. internerr(_g("field %s has blank lines >%s<"), $f, $v);
  169. $v =~ m/\n$/ &&
  170. internerr(_g("field %s has trailing newline >%s<"), $f, $v);
  171. if (defined($substvars)) {
  172. $v =~ s/,[\s,]*,/,/g;
  173. $v =~ s/^\s*,\s*//;
  174. $v =~ s/\s*,\s*$//;
  175. }
  176. $v =~ s/\$\{\}/\$/g;
  177. if ($fh) {
  178. print $fh "$f: $v\n" || syserr(_g("write error on control data"));
  179. }
  180. if (defined wantarray) {
  181. $str .= "$f: $v\n";
  182. }
  183. }
  184. return $str;
  185. }
  186. 1;