Substvars.pm 9.8 KB

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