Substvars.pm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. # Copyright © 2006-2009, 2012-2015 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.04';
  20. use POSIX qw(:errno_h);
  21. use Dpkg ();
  22. use Dpkg::Arch qw(get_host_arch);
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::Gettext;
  25. use parent 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. =cut
  34. use constant {
  35. SUBSTVAR_ATTR_USED => 1,
  36. SUBSTVAR_ATTR_AUTO => 2,
  37. SUBSTVAR_ATTR_OLD => 4,
  38. };
  39. =head1 METHODS
  40. =over 8
  41. =item $s = Dpkg::Substvars->new($file)
  42. Create a new object that can do substitutions. By default it contains
  43. generic substitutions like ${Newline}, ${Space}, ${Tab}, ${dpkg:Version}
  44. and ${dpkg:Upstream-Version}.
  45. Additional substitutions will be read from the $file passed as parameter.
  46. It keeps track of which substitutions were actually used (only counting
  47. substvars(), not get()), and warns about unused substvars when asked to. The
  48. substitutions that are always present are not included in these warnings.
  49. =cut
  50. sub new {
  51. my ($this, $arg) = @_;
  52. my $class = ref($this) || $this;
  53. my $self = {
  54. vars => {
  55. 'Newline' => "\n",
  56. 'Space' => ' ',
  57. 'Tab' => "\t",
  58. 'dpkg:Version' => $Dpkg::PROGVERSION,
  59. 'dpkg:Upstream-Version' => $Dpkg::PROGVERSION,
  60. },
  61. attr => {},
  62. msg_prefix => '',
  63. };
  64. $self->{vars}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
  65. bless $self, $class;
  66. my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;
  67. $self->{attr}{$_} = $attr foreach keys %{$self->{vars}};
  68. if ($arg) {
  69. $self->load($arg) if -e $arg;
  70. }
  71. return $self;
  72. }
  73. =item $s->set($key, $value)
  74. Add/replace a substitution.
  75. =cut
  76. sub set {
  77. my ($self, $key, $value, $attr) = @_;
  78. $attr //= 0;
  79. $self->{vars}{$key} = $value;
  80. $self->{attr}{$key} = $attr;
  81. }
  82. =item $s->set_as_used($key, $value)
  83. Add/replace a substitution and mark it as used (no warnings will be produced
  84. even if unused).
  85. =cut
  86. sub set_as_used {
  87. my ($self, $key, $value) = @_;
  88. $self->set($key, $value, SUBSTVAR_ATTR_USED);
  89. }
  90. =item $s->set_as_auto($key, $value)
  91. Add/replace a substitution and mark it as used and automatic (no warnings
  92. will be produced even if unused).
  93. =cut
  94. sub set_as_auto {
  95. my ($self, $key, $value) = @_;
  96. $self->set($key, $value, SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO);
  97. }
  98. =item $s->get($key)
  99. Get the value of a given substitution.
  100. =cut
  101. sub get {
  102. my ($self, $key) = @_;
  103. return $self->{vars}{$key};
  104. }
  105. =item $s->delete($key)
  106. Remove a given substitution.
  107. =cut
  108. sub delete {
  109. my ($self, $key) = @_;
  110. delete $self->{attr}{$key};
  111. return delete $self->{vars}{$key};
  112. }
  113. =item $s->mark_as_used($key)
  114. Prevents warnings about a unused substitution, for example if it is provided by
  115. default.
  116. =cut
  117. sub mark_as_used {
  118. my ($self, $key) = @_;
  119. $self->{attr}{$key} |= SUBSTVAR_ATTR_USED;
  120. }
  121. =item $s->no_warn($key)
  122. Obsolete function, use mark_as_used() instead.
  123. =cut
  124. sub no_warn {
  125. my ($self, $key) = @_;
  126. warnings::warnif('deprecated',
  127. 'obsolete no_warn() function, use mark_as_used() instead');
  128. $self->mark_as_used($key);
  129. }
  130. =item $s->load($file)
  131. Add new substitutions read from $file.
  132. =item $s->parse($fh, $desc)
  133. Add new substitutions read from the filehandle. $desc is used to identify
  134. the filehandle in error messages.
  135. =cut
  136. sub parse {
  137. my ($self, $fh, $varlistfile) = @_;
  138. local $_;
  139. binmode($fh);
  140. while (<$fh>) {
  141. next if m/^\s*\#/ || !m/\S/;
  142. s/\s*\n$//;
  143. if (! m/^(\w[-:0-9A-Za-z]*)\=(.*)$/) {
  144. error(g_('bad line in substvars file %s at line %d'),
  145. $varlistfile, $.);
  146. }
  147. $self->set($1, $2);
  148. }
  149. }
  150. =item $s->set_version_substvars($sourceversion, $binaryversion)
  151. Defines ${binary:Version}, ${source:Version} and
  152. ${source:Upstream-Version} based on the given version strings.
  153. These will never be warned about when unused.
  154. =cut
  155. sub set_version_substvars {
  156. my ($self, $sourceversion, $binaryversion) = @_;
  157. # Handle old function signature taking only one argument.
  158. $binaryversion //= $sourceversion;
  159. # For backwards compatibility on binNMUs that do not use the Binary-Only
  160. # field on the changelog, always fix up the source version.
  161. $sourceversion =~ s/\+b[0-9]+$//;
  162. my $upstreamversion = $sourceversion;
  163. $upstreamversion =~ s/-[^-]*$//;
  164. my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;
  165. $self->set('binary:Version', $binaryversion, $attr);
  166. $self->set('source:Version', $sourceversion, $attr);
  167. $self->set('source:Upstream-Version', $upstreamversion, $attr);
  168. # XXX: Source-Version is now deprecated, remove in the future.
  169. $self->set('Source-Version', $binaryversion, $attr | SUBSTVAR_ATTR_OLD);
  170. }
  171. =item $s->set_arch_substvars()
  172. Defines architecture variables: ${Arch}.
  173. This will never be warned about when unused.
  174. =cut
  175. sub set_arch_substvars {
  176. my $self = shift;
  177. my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;
  178. $self->set('Arch', get_host_arch(), $attr);
  179. }
  180. =item $newstring = $s->substvars($string)
  181. Substitutes variables in $string and return the result in $newstring.
  182. =cut
  183. sub substvars {
  184. my ($self, $v, %opts) = @_;
  185. my $lhs;
  186. my $vn;
  187. my $rhs = '';
  188. my $count = 0;
  189. $opts{msg_prefix} //= $self->{msg_prefix};
  190. $opts{no_warn} //= 0;
  191. while ($v =~ m/^(.*?)\$\{([-:0-9a-z]+)\}(.*)$/si) {
  192. # If we have consumed more from the leftover data, then
  193. # reset the recursive counter.
  194. $count = 0 if (length($3) < length($rhs));
  195. if ($count >= $maxsubsts) {
  196. error($opts{msg_prefix} .
  197. g_("too many substitutions - recursive ? - in '%s'"), $v);
  198. }
  199. $lhs = $1;
  200. $vn = $2;
  201. $rhs = $3;
  202. if (defined($self->{vars}{$vn})) {
  203. $v = $lhs . $self->{vars}{$vn} . $rhs;
  204. $self->mark_as_used($vn);
  205. $count++;
  206. if (not $opts{no_warn} and $self->{attr}{$vn} & SUBSTVAR_ATTR_OLD) {
  207. warning($opts{msg_prefix} .
  208. g_('deprecated substitution variable ${%s}'), $vn);
  209. }
  210. } else {
  211. warning($opts{msg_prefix} . g_('unknown substitution variable ${%s}'),
  212. $vn) unless $opts{no_warn};
  213. $v = $lhs . $rhs;
  214. }
  215. }
  216. return $v;
  217. }
  218. =item $s->warn_about_unused()
  219. Issues warning about any variables that were set, but not used.
  220. =cut
  221. sub warn_about_unused {
  222. my ($self, %opts) = @_;
  223. $opts{msg_prefix} //= $self->{msg_prefix};
  224. foreach my $vn (keys %{$self->{vars}}) {
  225. next if $self->{attr}{$vn} & SUBSTVAR_ATTR_USED;
  226. # Empty substitutions variables are ignored on the basis
  227. # that they are not required in the current situation
  228. # (example: debhelper's misc:Depends in many cases)
  229. next if $self->{vars}{$vn} eq '';
  230. warning($opts{msg_prefix} . g_('unused substitution variable ${%s}'),
  231. $vn);
  232. }
  233. }
  234. =item $s->set_msg_prefix($prefix)
  235. Define a prefix displayed before all warnings/error messages output
  236. by the module.
  237. =cut
  238. sub set_msg_prefix {
  239. my ($self, $prefix) = @_;
  240. $self->{msg_prefix} = $prefix;
  241. }
  242. =item $s->filter(remove => $rmfunc)
  243. =item $s->filter(keep => $keepfun)
  244. Filter the substitution variables, either removing or keeping all those
  245. that return true when &$rmfunc($key) or &keepfunc($key) is called.
  246. =cut
  247. sub filter {
  248. my ($self, %opts) = @_;
  249. my $remove = $opts{remove} // sub { 0 };
  250. my $keep = $opts{keep} // sub { 1 };
  251. foreach my $vn (keys %{$self->{vars}}) {
  252. $self->delete($vn) if &$remove($vn) or not &$keep($vn);
  253. }
  254. }
  255. =item $s->save($file)
  256. Store all substitutions variables except the automatic ones in the
  257. indicated file.
  258. =item "$s"
  259. Return a string representation of all substitutions variables except the
  260. automatic ones.
  261. =item $str = $s->output($fh)
  262. Print all substitutions variables except the automatic ones in the
  263. filehandle and return the content written.
  264. =cut
  265. sub output {
  266. my ($self, $fh) = @_;
  267. my $str = '';
  268. # Store all non-automatic substitutions only
  269. foreach my $vn (sort keys %{$self->{vars}}) {
  270. next if $self->{attr}{$vn} & SUBSTVAR_ATTR_AUTO;
  271. my $line = "$vn=" . $self->{vars}{$vn} . "\n";
  272. print { $fh } $line if defined $fh;
  273. $str .= $line;
  274. }
  275. return $str;
  276. }
  277. =back
  278. =head1 CHANGES
  279. =head2 Version 1.04 (dpkg 1.18.0)
  280. New method: $s->filter().
  281. =head2 Version 1.03 (dpkg 1.17.11)
  282. New method: $s->set_as_auto().
  283. =head2 Version 1.02 (dpkg 1.16.5)
  284. New argument: Accept a $binaryversion in $s->set_version_substvars(),
  285. passing a single argument is still supported.
  286. New method: $s->mark_as_used().
  287. Deprecated method: $s->no_warn(), use $s->mark_as_used() instead.
  288. =head2 Version 1.01 (dpkg 1.16.4)
  289. New method: $s->set_as_used().
  290. =head2 Version 1.00 (dpkg 1.15.6)
  291. Mark the module as public.
  292. =head1 AUTHOR
  293. Raphaël Hertzog <hertzog@debian.org>.
  294. =cut
  295. 1;