Substvars.pm 7.0 KB

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