Substvars.pm 7.5 KB

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