Substvars.pm 6.5 KB

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