Fields.pm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package Dpkg::Fields;
  2. use strict;
  3. use warnings;
  4. use base qw(Exporter);
  5. use Dpkg::Deps qw(@src_dep_fields @pkg_dep_fields);
  6. our @EXPORT_OK = qw(capit %control_src_fields %control_pkg_fields
  7. $control_src_field_regex $control_pkg_field_regex);
  8. our %EXPORT_TAGS = ('list' => [qw(%control_src_fields %control_pkg_fields
  9. $control_src_field_regex $control_pkg_field_regex)]);
  10. # Some variables (list of fields)
  11. our %control_src_fields;
  12. our %control_pkg_fields;
  13. $control_src_fields{$_} = 1 foreach (qw(Bugs Dm-Upload-Allowed
  14. Homepage Origin Maintainer Priority Section Source Standards-Version
  15. Uploaders Vcs-Browser Vcs-Arch Vcs-Bzr Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg
  16. Vcs-Mtn Vcs-Svn));
  17. $control_src_fields{$_} = 1 foreach (@src_dep_fields);
  18. $control_pkg_fields{$_} = 1 foreach (qw(Architecture Bugs Description Essential
  19. Homepage Installer-Menu-Item Kernel-Version Package Package-Type
  20. Priority Section Subarchitecture Tag));
  21. $control_pkg_fields{$_} = 1 foreach (@pkg_dep_fields);
  22. our $control_src_field_regex = "(?:" . join("|", keys %control_src_fields) . ")";
  23. our $control_pkg_field_regex = "(?:" . join("|", keys %control_pkg_fields) . ")";
  24. # Some functions
  25. sub capit {
  26. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  27. return join '-', @pieces;
  28. }
  29. package Dpkg::Fields::Object;
  30. =head1 OTHER OBJECTS
  31. =head2 Dpkg::Fields::Object
  32. This object is used to tie a hash. It implements hash-like functions by
  33. normalizing the name of fields received in keys (using
  34. Dpkg::Fields::capit). It also stores the order in which fields have been
  35. added in order to be able to dump them in the same order.
  36. =cut
  37. use Tie::Hash;
  38. our @ISA = qw(Tie::ExtraHash Tie::Hash);
  39. use Dpkg::ErrorHandling;
  40. use Dpkg::Gettext;
  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 tied(%hash)->find_custom_field($name)
  109. Scan the fields and look for a user specific field whose name matches the
  110. following regex: /X[SBC]+-$name/i. Return the name of the field found or
  111. undef if nothing has been found.
  112. =cut
  113. sub find_custom_field {
  114. my ($self, $name) = @_;
  115. foreach my $key (keys %{$self->[0]}) {
  116. return $key if $key =~ /^X[SBC]*-\Q$name\E$/i;
  117. }
  118. return undef;
  119. }
  120. =head2 tied(%hash)->get_custom_field($name)
  121. Identify a user field and retrieve its value.
  122. =cut
  123. sub get_custom_field {
  124. my ($self, $name) = @_;
  125. my $key = $self->find_custom_field($name);
  126. return $self->[0]->{$key} if defined $key;
  127. return undef;
  128. }
  129. =head2 my $str = tied(%hash)->dump()
  130. =head2 tied(%hash)->dump($fh)
  131. Dump the raw content of the hash either as a string or to a filehandle.
  132. =cut
  133. sub dump {
  134. my ($self, $fh) = @_;
  135. my $str = "";
  136. foreach (@{$self->[1]}) {
  137. if (exists $self->[0]->{$_}) {
  138. print $fh "$_: " . $self->[0]->{$_} . "\n" if $fh;
  139. $str .= "$_: " . $self->[0]->{$_} . "\n" if defined wantarray;
  140. }
  141. }
  142. return $str;
  143. }
  144. =head2 tied(%hash)->set_field_importance(@fields)
  145. Define the order in which fields will be displayed in the output() method.
  146. =cut
  147. sub set_field_importance {
  148. my ($self, @fields) = @_;
  149. my $i = 1;
  150. $self->[2] = {};
  151. $self->[2]{$_} = $i++ foreach (@fields);
  152. }
  153. =head2 tied(%hash)->output($fh, $substvars)
  154. If $fh is defined, print the fields on the $fh filehandle after
  155. substitution of variables defined in the Dpkg::Substvars object.
  156. Also returns the string of what would printed on the filehandle.
  157. =cut
  158. sub output {
  159. my ($self, $fh, $substvars) = @_;
  160. my $str = "";
  161. my $imp = $self->[2]; # Hash of relative importance
  162. # Add substvars to refer to other fields
  163. if (defined($substvars)) {
  164. foreach my $f (keys %{$self->[0]}) {
  165. $substvars->set("F:$f", $self->[0]->{$f});
  166. $substvars->no_warn("F:$f");
  167. }
  168. }
  169. my @keys = sort {
  170. if (defined $imp->{$a} && defined $imp->{$b}) {
  171. $imp->{$a} <=> $imp->{$b};
  172. } elsif (defined($imp->{$a})) {
  173. -1;
  174. } elsif (defined($imp->{$b})) {
  175. 1;
  176. } else {
  177. $a cmp $b;
  178. }
  179. } keys %{$self->[0]};
  180. foreach my $f (@keys) {
  181. my $v = $self->[0]->{$f};
  182. if (defined($substvars)) {
  183. $v = $substvars->substvars($v);
  184. }
  185. $v =~ m/\S/ || next; # delete whitespace-only fields
  186. $v =~ m/\n\S/ &&
  187. internerr("field %s has newline then non whitespace >%s<",
  188. $f, $v);
  189. $v =~ m/\n[ \t]*\n/ &&
  190. internerr("field %s has blank lines >%s<", $f, $v);
  191. $v =~ m/\n$/ &&
  192. internerr("field %s has trailing newline >%s<", $f, $v);
  193. if (defined($substvars)) {
  194. $v =~ s/,[\s,]*,/,/g;
  195. $v =~ s/^\s*,\s*//;
  196. $v =~ s/\s*,\s*$//;
  197. }
  198. $v =~ s/\$\{\}/\$/g;
  199. if ($fh) {
  200. print $fh "$f: $v\n" || syserr(_g("write error on control data"));
  201. }
  202. if (defined wantarray) {
  203. $str .= "$f: $v\n";
  204. }
  205. }
  206. return $str;
  207. }
  208. 1;