Substvars.pm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. # Copyright © 2006-2009,2012 Guillem Jover <guillem@debian.org>
  2. # Copyright © 2007-2010 Raphaël Hertzog <hertzog@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package Dpkg::Substvars;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.02';
  20. use Dpkg ();
  21. use Dpkg::Arch qw(get_host_arch);
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Gettext;
  24. use Carp;
  25. use POSIX qw(:errno_h);
  26. use parent qw(Dpkg::Interface::Storable);
  27. my $maxsubsts = 50;
  28. =encoding utf8
  29. =head1 NAME
  30. Dpkg::Substvars - handle variable substitution in strings
  31. =head1 DESCRIPTION
  32. It provides some an object which is able to substitute variables in
  33. strings.
  34. =head1 METHODS
  35. =over 8
  36. =item my $s = Dpkg::Substvars->new($file)
  37. Create a new object that can do substitutions. By default it contains
  38. generic substitutions like ${Newline}, ${Space}, ${Tab}, ${dpkg:Version}
  39. and ${dpkg:Upstream-Version}.
  40. Additional substitutions will be read from the $file passed as parameter.
  41. It keeps track of which substitutions were actually used (only counting
  42. substvars(), not get()), and warns about unused substvars when asked to. The
  43. substitutions that are always present are not included in these warnings.
  44. =cut
  45. sub new {
  46. my ($this, $arg) = @_;
  47. my $class = ref($this) || $this;
  48. my $self = {
  49. vars => {
  50. 'Newline' => "\n",
  51. 'Space' => ' ',
  52. 'Tab' => "\t",
  53. 'dpkg:Version' => $Dpkg::PROGVERSION,
  54. 'dpkg:Upstream-Version' => $Dpkg::PROGVERSION,
  55. },
  56. used => {},
  57. msg_prefix => '',
  58. };
  59. $self->{vars}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
  60. bless $self, $class;
  61. $self->mark_as_used($_) foreach keys %{$self->{vars}};
  62. if ($arg) {
  63. $self->load($arg) if -e $arg;
  64. }
  65. return $self;
  66. }
  67. =item $s->set($key, $value)
  68. Add/replace a substitution.
  69. =cut
  70. sub set {
  71. my ($self, $key, $value) = @_;
  72. $self->{vars}{$key} = $value;
  73. }
  74. =item $s->set_as_used($key, $value)
  75. Add/replace a substitution and mark it as used (no warnings will be produced
  76. even if unused).
  77. =cut
  78. sub set_as_used {
  79. my ($self, $key, $value) = @_;
  80. $self->set($key, $value);
  81. $self->mark_as_used($key);
  82. }
  83. =item $s->get($key)
  84. Get the value of a given substitution.
  85. =cut
  86. sub get {
  87. my ($self, $key) = @_;
  88. return $self->{vars}{$key};
  89. }
  90. =item $s->delete($key)
  91. Remove a given substitution.
  92. =cut
  93. sub delete {
  94. my ($self, $key) = @_;
  95. delete $self->{used}{$key};
  96. return delete $self->{vars}{$key};
  97. }
  98. =item $s->mark_as_used($key)
  99. Prevents warnings about a unused substitution, for example if it is provided by
  100. default.
  101. =cut
  102. sub mark_as_used {
  103. my ($self, $key) = @_;
  104. $self->{used}{$key}++;
  105. }
  106. =item $s->no_warn($key)
  107. Obsolete function, use mark_as_used() instead.
  108. =cut
  109. sub no_warn {
  110. my ($self, $key) = @_;
  111. carp 'obsolete no_warn() function, use mark_as_used() instead';
  112. $self->mark_as_used($key);
  113. }
  114. =item $s->load($file)
  115. Add new substitutions read from $file.
  116. =item $s->parse($fh, $desc)
  117. Add new substitutions read from the filehandle. $desc is used to identify
  118. the filehandle in error messages.
  119. =cut
  120. sub parse {
  121. my ($self, $fh, $varlistfile) = @_;
  122. local $_;
  123. binmode($fh);
  124. while (<$fh>) {
  125. next if m/^\s*\#/ || !m/\S/;
  126. s/\s*\n$//;
  127. if (! m/^(\w[-:0-9A-Za-z]*)\=(.*)$/) {
  128. error(_g('bad line in substvars file %s at line %d'),
  129. $varlistfile, $.);
  130. }
  131. $self->{vars}{$1} = $2;
  132. }
  133. }
  134. =item $s->set_version_substvars($sourceversion, $binaryversion)
  135. Defines ${binary:Version}, ${source:Version} and
  136. ${source:Upstream-Version} based on the given version strings.
  137. These will never be warned about when unused.
  138. =cut
  139. sub set_version_substvars {
  140. my ($self, $sourceversion, $binaryversion) = @_;
  141. # Handle old function signature taking only one argument.
  142. $binaryversion ||= $sourceversion;
  143. # For backwards compatibility on binNMUs that do not use the Binary-Only
  144. # field on the changelog, always fix up the source version.
  145. $sourceversion =~ s/\+b[0-9]+$//;
  146. $self->{vars}{'binary:Version'} = $binaryversion;
  147. $self->{vars}{'source:Version'} = $sourceversion;
  148. $self->{vars}{'source:Upstream-Version'} = $sourceversion;
  149. $self->{vars}{'source:Upstream-Version'} =~ s/-[^-]*$//;
  150. # XXX: Source-Version is now deprecated, remove in the future.
  151. $self->{vars}{'Source-Version'} = $binaryversion;
  152. $self->mark_as_used($_) foreach qw/binary:Version source:Version source:Upstream-Version Source-Version/;
  153. }
  154. =item $s->set_arch_substvars()
  155. Defines architecture variables: ${Arch}.
  156. This will never be warned about when unused.
  157. =cut
  158. sub set_arch_substvars {
  159. my ($self) = @_;
  160. $self->set_as_used('Arch', get_host_arch());
  161. }
  162. =item $newstring = $s->substvars($string)
  163. Substitutes variables in $string and return the result in $newstring.
  164. =cut
  165. sub substvars {
  166. my ($self, $v, %opts) = @_;
  167. my $lhs;
  168. my $vn;
  169. my $rhs = '';
  170. my $count = 0;
  171. $opts{msg_prefix} = $self->{msg_prefix} unless exists $opts{msg_prefix};
  172. $opts{no_warn} = 0 unless exists $opts{no_warn};
  173. while ($v =~ m/^(.*?)\$\{([-:0-9a-z]+)\}(.*)$/si) {
  174. # If we have consumed more from the leftover data, then
  175. # reset the recursive counter.
  176. $count = 0 if (length($3) < length($rhs));
  177. if ($count >= $maxsubsts) {
  178. error($opts{msg_prefix} .
  179. _g("too many substitutions - recursive ? - in \`%s'"), $v);
  180. }
  181. $lhs = $1; $vn = $2; $rhs = $3;
  182. if (defined($self->{vars}{$vn})) {
  183. $v = $lhs . $self->{vars}{$vn} . $rhs;
  184. $self->mark_as_used($vn);
  185. $count++;
  186. } else {
  187. warning($opts{msg_prefix} . _g('unknown substitution variable ${%s}'),
  188. $vn) unless $opts{no_warn};
  189. $v = $lhs . $rhs;
  190. }
  191. }
  192. return $v;
  193. }
  194. =item $s->warn_about_unused()
  195. Issues warning about any variables that were set, but not used
  196. =cut
  197. sub warn_about_unused {
  198. my ($self, %opts) = @_;
  199. $opts{msg_prefix} = $self->{msg_prefix} unless exists $opts{msg_prefix};
  200. foreach my $vn (keys %{$self->{vars}}) {
  201. next if $self->{used}{$vn};
  202. # Empty substitutions variables are ignored on the basis
  203. # that they are not required in the current situation
  204. # (example: debhelper's misc:Depends in many cases)
  205. next if $self->{vars}{$vn} eq '';
  206. warning($opts{msg_prefix} . _g('unused substitution variable ${%s}'),
  207. $vn);
  208. }
  209. }
  210. =item $s->set_msg_prefix($prefix)
  211. Define a prefix displayed before all warnings/error messages output
  212. by the module.
  213. =cut
  214. sub set_msg_prefix {
  215. my ($self, $prefix) = @_;
  216. $self->{msg_prefix} = $prefix;
  217. }
  218. =item $s->save($file)
  219. Store all substitutions variables except the automatic ones in the
  220. indicated file.
  221. =item "$s"
  222. Return a string representation of all substitutions variables except the
  223. automatic ones.
  224. =item $str = $s->output($fh)
  225. Print all substitutions variables except the automatic ones in the
  226. filehandle and return the content written.
  227. =cut
  228. sub output {
  229. my ($self, $fh) = @_;
  230. my $str = '';
  231. # Store all non-automatic substitutions only
  232. foreach my $vn (sort keys %{$self->{vars}}) {
  233. next if /^(?:(?:dpkg|source|binary):(?:Source-)?Version|Space|Tab|Newline|Arch|Source-Version|F:.+)$/;
  234. my $line = "$vn=" . $self->{vars}{$vn} . "\n";
  235. print { $fh } $line if defined $fh;
  236. $str .= $line;
  237. }
  238. return $str;
  239. }
  240. =back
  241. =head1 CHANGES
  242. =head2 Version 1.02
  243. New argument: Accept a $binaryversion in $s->set_version_substvars(),
  244. passing a single argument is still supported.
  245. New method: $s->mark_as_used().
  246. Deprecated method: $s->no_warn(), use $s->mark_as_used() instead.
  247. =head2 Version 1.01
  248. New method: $s->set_as_used().
  249. =head1 AUTHOR
  250. Raphaël Hertzog <hertzog@debian.org>.
  251. =cut
  252. 1;