Fields.pm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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;
  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 tied(%hash)->find_custom_field($name)
  110. Scan the fields and look for a user specific field whose name matches the
  111. following regex: /X[SBC]+-$name/i. Return the name of the field found or
  112. undef if nothing has been found.
  113. =cut
  114. sub find_custom_field {
  115. my ($self, $name) = @_;
  116. foreach my $key (keys %{$self->[0]}) {
  117. return $key if $key =~ /^X[SBC]*-\Q$name\E$/i;
  118. }
  119. return undef;
  120. }
  121. =head2 tied(%hash)->get_custom_field($name)
  122. Identify a user field and retrieve its value.
  123. =cut
  124. sub get_custom_field {
  125. my ($self, $name) = @_;
  126. my $key = $self->find_custom_field($name);
  127. return $self->[0]->{$key} if defined $key;
  128. return undef;
  129. }
  130. =head2 my $str = tied(%hash)->dump()
  131. =head2 tied(%hash)->dump($fh)
  132. Dump the raw content of the hash either as a string or to a filehandle.
  133. =cut
  134. sub dump {
  135. my ($self, $fh) = @_;
  136. my $str = "";
  137. foreach (@{$self->[1]}) {
  138. if (exists $self->[0]->{$_}) {
  139. print $fh "$_: " . $self->[0]->{$_} . "\n" if $fh;
  140. $str .= "$_: " . $self->[0]->{$_} . "\n" if defined wantarray;
  141. }
  142. }
  143. return $str;
  144. }
  145. =head2 tied(%hash)->set_field_importance(@fields)
  146. Define the order in which fields will be displayed in the output() method.
  147. =cut
  148. sub set_field_importance {
  149. my ($self, @fields) = @_;
  150. my $i = 1;
  151. $self->[2] = {};
  152. $self->[2]{$_} = $i++ foreach (@fields);
  153. }
  154. =head2 tied(%hash)->output($fh, $substvars)
  155. If $fh is defined, print the fields on the $fh filehandle after
  156. substitution of variables defined in the Dpkg::Substvars object.
  157. Also returns the string of what would printed on the filehandle.
  158. =cut
  159. sub output {
  160. my ($self, $fh, $substvars) = @_;
  161. my $str = "";
  162. my $imp = $self->[2]; # Hash of relative importance
  163. # Add substvars to refer to other fields
  164. if (defined($substvars)) {
  165. foreach my $f (keys %{$self->[0]}) {
  166. $substvars->set("F:$f", $self->[0]->{$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(_g("field %s has newline then non whitespace >%s<"),
  188. $f, $v);
  189. $v =~ m/\n[ \t]*\n/ &&
  190. internerr(_g("field %s has blank lines >%s<"), $f, $v);
  191. $v =~ m/\n$/ &&
  192. internerr(_g("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;