Substvars.pm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # Copyright © 2007-2010 Raphaël Hertzog <hertzog@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 <http://www.gnu.org/licenses/>.
  15. package Dpkg::Substvars;
  16. use strict;
  17. use warnings;
  18. use Dpkg qw($version);
  19. use Dpkg::Arch qw(get_host_arch);
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Gettext;
  22. use POSIX qw(:errno_h);
  23. use base qw(Dpkg::Interface::Storable);
  24. my $maxsubsts = 50;
  25. =head1 NAME
  26. Dpkg::Substvars - handle variable substitution in strings
  27. =head1 DESCRIPTION
  28. It provides some an object which is able to substitute variables in
  29. strings.
  30. =head1 METHODS
  31. =over 8
  32. =item my $s = Dpkg::Substvars->new($file)
  33. Create a new object that can do substitutions. By default it contains
  34. generic substitutions like ${Newline}, ${Space}, ${Tab}, ${dpkg:Version}
  35. and ${dpkg:Upstream-Version}.
  36. Additional substitutions will be read from the $file passed as parameter.
  37. It keeps track of which substitutions were actually used (only counting
  38. substvars(), not get()), and warns about unused substvars when asked to. The
  39. substitutions that are always present are not included in these warnings.
  40. =cut
  41. sub new {
  42. my ($this, $arg) = @_;
  43. my $class = ref($this) || $this;
  44. my $self = {
  45. vars => {
  46. "Newline" => "\n",
  47. "Space" => " ",
  48. "Tab" => "\t",
  49. "dpkg:Version" => $version,
  50. "dpkg:Upstream-Version" => $version,
  51. },
  52. used => {},
  53. };
  54. $self->{'vars'}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
  55. bless $self, $class;
  56. $self->no_warn($_) foreach keys %{$self->{'vars'}};
  57. if ($arg) {
  58. $self->load($arg) if -e $arg;
  59. }
  60. return $self;
  61. }
  62. =item $s->set($key, $value)
  63. Add/replace a substitution.
  64. =cut
  65. sub set {
  66. my ($self, $key, $value) = @_;
  67. $self->{'vars'}{$key} = $value;
  68. }
  69. =item $s->get($key)
  70. Get the value of a given substitution.
  71. =cut
  72. sub get {
  73. my ($self, $key) = @_;
  74. return $self->{'vars'}{$key};
  75. }
  76. =item $s->delete($key)
  77. Remove a given substitution.
  78. =cut
  79. sub delete {
  80. my ($self, $key) = @_;
  81. delete $self->{'used'}{$key};
  82. return delete $self->{'vars'}{$key};
  83. }
  84. =item $s->no_warn($key)
  85. Prevents warnings about a unused substitution, for example if it is provided by
  86. default.
  87. =cut
  88. sub no_warn {
  89. my ($self, $key) = @_;
  90. $self->{'used'}{$key}++;
  91. }
  92. =item $s->load($file)
  93. Add new substitutions read from $file.
  94. =item $s->parse($fh, $desc)
  95. Add new substitutions read from the filehandle. $desc is used to identify
  96. the filehandle in error messages.
  97. =cut
  98. sub parse {
  99. my ($self, $fh, $varlistfile) = @_;
  100. binmode($fh);
  101. while (<$fh>) {
  102. next if m/^\s*\#/ || !m/\S/;
  103. s/\s*\n$//;
  104. m/^(\w[-:0-9A-Za-z]*)\=(.*)$/ ||
  105. error(_g("bad line in substvars file %s at line %d"),
  106. $varlistfile, $.);
  107. $self->{'vars'}{$1} = $2;
  108. }
  109. }
  110. =item $s->set_version_substvars($version)
  111. Defines ${binary:Version}, ${source:Version} and
  112. ${source:Upstream-Version} based on the given version string.
  113. These will never be warned about when unused.
  114. =cut
  115. sub set_version_substvars {
  116. my ($self, $version) = @_;
  117. $self->{'vars'}{'binary:Version'} = $version;
  118. $self->{'vars'}{'source:Version'} = $version;
  119. $self->{'vars'}{'source:Version'} =~ s/\+b[0-9]+$//;
  120. $self->{'vars'}{'source:Upstream-Version'} = $version;
  121. $self->{'vars'}{'source:Upstream-Version'} =~ s/-[^-]*$//;
  122. # XXX: Source-Version is now deprecated, remove in the future.
  123. $self->{'vars'}{'Source-Version'} = $version;
  124. $self->no_warn($_) foreach qw/binary:Version source:Version source:Upstream-Version Source-Version/;
  125. }
  126. =item $s->set_arch_substvars()
  127. Defines architecture variables: ${Arch}.
  128. This will never be warned about when unused.
  129. =cut
  130. sub set_arch_substvars {
  131. my ($self) = @_;
  132. $self->{'vars'}{'Arch'} = get_host_arch();
  133. $self->no_warn('Arch');
  134. }
  135. =item $newstring = $s->substvars($string)
  136. Substitutes variables in $string and return the result in $newstring.
  137. =cut
  138. sub substvars {
  139. my ($self, $v) = @_;
  140. my $lhs;
  141. my $vn;
  142. my $rhs = '';
  143. my $count = 0;
  144. while ($v =~ m/^(.*?)\$\{([-:0-9a-z]+)\}(.*)$/si) {
  145. # If we have consumed more from the leftover data, then
  146. # reset the recursive counter.
  147. $count = 0 if (length($3) < length($rhs));
  148. $count < $maxsubsts ||
  149. error(_g("too many substitutions - recursive ? - in \`%s'"), $v);
  150. $lhs = $1; $vn = $2; $rhs = $3;
  151. if (defined($self->{'vars'}{$vn})) {
  152. $v = $lhs . $self->{'vars'}{$vn} . $rhs;
  153. $self->no_warn($vn);
  154. $count++;
  155. } else {
  156. warning(_g("unknown substitution variable \${%s}"), $vn);
  157. $v = $lhs . $rhs;
  158. }
  159. }
  160. return $v;
  161. }
  162. =item $s->warn_about_unused()
  163. Issues warning about any variables that were set, but not used
  164. =cut
  165. sub warn_about_unused {
  166. my ($self) = @_;
  167. foreach my $vn (keys %{$self->{'vars'}}) {
  168. next if $self->{'used'}{$vn};
  169. # Empty substitutions variables are ignored on the basis
  170. # that they are not required in the current situation
  171. # (example: debhelper's misc:Depends in many cases)
  172. next if $self->{'vars'}{$vn} eq "";
  173. warning(_g("unused substitution variable \${%s}"), $vn);
  174. }
  175. }
  176. =item $s->save($file)
  177. Store all substitutions variables except the automatic ones in the
  178. indicated file.
  179. =item "$s"
  180. Return a string representation of all substitutions variables except the
  181. automatic ones.
  182. =item $str = $s->output($fh)
  183. Print all substitutions variables except the automatic ones in the
  184. filehandle and return the content written.
  185. =cut
  186. sub output {
  187. my ($self, $fh) = @_;
  188. my $str = "";
  189. # Store all non-automatic substitutions only
  190. foreach my $vn (sort keys %{$self->{'vars'}}) {
  191. next if /^(?:(?:dpkg|source|binary):(?:Source-)?Version|Space|Tab|Newline|Arch|Source-Version|F:.+)$/;
  192. my $line = "$vn=" . $self->{vars}{$vn} . "\n";
  193. print $fh $line if defined $fh;
  194. $str .= $line;
  195. }
  196. return $str;
  197. }
  198. =back
  199. =head1 AUTHOR
  200. Raphael Hertzog <hertzog@debian.org>.
  201. =cut
  202. 1;