Index.pm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. # Copyright © 2009 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 <https://www.gnu.org/licenses/>.
  15. package Dpkg::Index;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.00';
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Control;
  22. use parent qw(Dpkg::Interface::Storable);
  23. use overload
  24. '@{}' => sub { return $_[0]->{order} },
  25. fallback => 1;
  26. =encoding utf8
  27. =head1 NAME
  28. Dpkg::Index - generic index of control information
  29. =head1 DESCRIPTION
  30. This object represent a set of Dpkg::Control objects.
  31. =head1 METHODS
  32. =over 4
  33. =item $index = Dpkg::Index->new(%opts)
  34. Creates a new empty index. See set_options() for more details.
  35. =cut
  36. sub new {
  37. my ($this, %opts) = @_;
  38. my $class = ref($this) || $this;
  39. my $self = {
  40. items => {},
  41. order => [],
  42. get_key_func => sub { return $_[0]->{Package} },
  43. type => CTRL_UNKNOWN,
  44. };
  45. bless $self, $class;
  46. $self->set_options(%opts);
  47. if (exists $opts{load}) {
  48. $self->load($opts{load});
  49. }
  50. return $self;
  51. }
  52. =item $index->set_options(%opts)
  53. The "type" option is checked first to define default values for other
  54. options. Here are the relevant options: "get_key_func" is a function
  55. returning a key for the item passed in parameters. The index can only
  56. contain one item with a given key. The function used depends on the
  57. type: for CTRL_INFO_PKG, CTRL_INDEX_SRC, CTRL_INDEX_PKG and CTRL_PKG_DEB
  58. it's simply the Package field; for CTRL_PKG_SRC and CTRL_INFO_SRC, it's
  59. the Source field; for CTRL_CHANGELOG it's the Source and the Version
  60. fields (concatenated with an intermediary "_"); for CTRL_TESTS is either
  61. the Tests or Test-Command fields; for CTRL_FILE_CHANGES it's
  62. the Source, Version and Architecture fields (concatenated with "_");
  63. for CTRL_FILE_VENDOR it's the Vendor field; for CTRL_FILE_STATUS it's the
  64. Package and Architecture fields (concatenated with "_"). Otherwise it's
  65. the Package field by default.
  66. =cut
  67. sub set_options {
  68. my ($self, %opts) = @_;
  69. # Default values based on type
  70. if (exists $opts{type}) {
  71. my $t = $opts{type};
  72. if ($t == CTRL_INFO_PKG or $t == CTRL_INDEX_SRC or
  73. $t == CTRL_INDEX_PKG or $t == CTRL_PKG_DEB) {
  74. $self->{get_key_func} = sub { return $_[0]->{Package}; };
  75. } elsif ($t == CTRL_PKG_SRC or $t == CTRL_INFO_SRC) {
  76. $self->{get_key_func} = sub { return $_[0]->{Source}; };
  77. } elsif ($t == CTRL_CHANGELOG) {
  78. $self->{get_key_func} = sub {
  79. return $_[0]->{Source} . '_' . $_[0]->{Version};
  80. };
  81. } elsif ($t == CTRL_COPYRIGHT_HEADER) {
  82. # This is a bit pointless, because the value will almost always
  83. # be the same, but guarantees that we use a known field.
  84. $self->{get_key_func} = sub { return $_[0]->{Format}; };
  85. } elsif ($t == CTRL_COPYRIGHT_FILES) {
  86. $self->{get_key_func} = sub { return $_[0]->{Files}; };
  87. } elsif ($t == CTRL_COPYRIGHT_LICENSE) {
  88. $self->{get_key_func} = sub { return $_[0]->{License}; };
  89. } elsif ($t == CTRL_TESTS) {
  90. $self->{get_key_func} = sub {
  91. return $_[0]->{Tests} || $_[0]->{'Test-Command'};
  92. };
  93. } elsif ($t == CTRL_FILE_CHANGES) {
  94. $self->{get_key_func} = sub {
  95. return $_[0]->{Source} . '_' . $_[0]->{Version} . '_' .
  96. $_[0]->{Architecture};
  97. };
  98. } elsif ($t == CTRL_FILE_VENDOR) {
  99. $self->{get_key_func} = sub { return $_[0]->{Vendor}; };
  100. } elsif ($t == CTRL_FILE_STATUS) {
  101. $self->{get_key_func} = sub {
  102. return $_[0]->{Package} . '_' . $_[0]->{Architecture};
  103. };
  104. }
  105. }
  106. # Options set by the user override default values
  107. $self->{$_} = $opts{$_} foreach keys %opts;
  108. }
  109. =item $index->get_type()
  110. Returns the type of control information stored. See the type parameter
  111. set during new().
  112. =cut
  113. sub get_type {
  114. my $self = shift;
  115. return $self->{type};
  116. }
  117. =item $index->add($item, [$key])
  118. Add a new item in the index. If the $key parameter is omitted, the key
  119. will be generated with the get_key_func function (see set_options() for
  120. details).
  121. =cut
  122. sub add {
  123. my ($self, $item, $key) = @_;
  124. $key //= $self->{get_key_func}($item);
  125. if (not exists $self->{items}{$key}) {
  126. push @{$self->{order}}, $key;
  127. }
  128. $self->{items}{$key} = $item;
  129. }
  130. =item $index->load($file)
  131. Reads the file and creates all items parsed. Returns the number of items
  132. parsed. Handles compressed files transparently based on their extensions.
  133. =item $index->parse($fh, $desc)
  134. Reads the filehandle and creates all items parsed. When called multiple
  135. times, the parsed stanzas are accumulated.
  136. Returns the number of items parsed.
  137. =cut
  138. sub parse {
  139. my ($self, $fh, $desc) = @_;
  140. my $item = $self->new_item();
  141. my $i = 0;
  142. while ($item->parse($fh, $desc)) {
  143. $self->add($item);
  144. $item = $self->new_item();
  145. $i++;
  146. }
  147. return $i;
  148. }
  149. =item $index->save($file)
  150. Writes the content of the index in a file. Auto-compresses files
  151. based on their extensions.
  152. =item $item = $index->new_item()
  153. Creates a new item. Mainly useful for derived objects that would want
  154. to override this method to return something else than a Dpkg::Control
  155. object.
  156. =cut
  157. sub new_item {
  158. my $self = shift;
  159. return Dpkg::Control->new(type => $self->{type});
  160. }
  161. =item $item = $index->get_by_key($key)
  162. Returns the item identified by $key or undef.
  163. =cut
  164. sub get_by_key {
  165. my ($self, $key) = @_;
  166. return $self->{items}{$key} if exists $self->{items}{$key};
  167. return;
  168. }
  169. =item @keys = $index->get_keys(%criteria)
  170. Returns the keys of items that matches all the criteria. The key of the
  171. %criteria hash is a field name and the value is either a regex that needs
  172. to match the field value, or a reference to a function that must return
  173. true and that receives the field value as single parameter, or a scalar
  174. that must be equal to the field value.
  175. =cut
  176. sub get_keys {
  177. my ($self, %crit) = @_;
  178. my @selected = @{$self->{order}};
  179. foreach my $s_crit (keys %crit) { # search criteria
  180. if (ref($crit{$s_crit}) eq 'Regexp') {
  181. @selected = grep {
  182. exists $self->{items}{$_}{$s_crit} and
  183. $self->{items}{$_}{$s_crit} =~ $crit{$s_crit}
  184. } @selected;
  185. } elsif (ref($crit{$s_crit}) eq 'CODE') {
  186. @selected = grep {
  187. &{$crit{$s_crit}}($self->{items}{$_}{$s_crit});
  188. } @selected;
  189. } else {
  190. @selected = grep {
  191. exists $self->{items}{$_}{$s_crit} and
  192. $self->{items}{$_}{$s_crit} eq $crit{$s_crit}
  193. } @selected;
  194. }
  195. }
  196. return @selected;
  197. }
  198. =item @items = $index->get(%criteria)
  199. Returns all the items that matches all the criteria.
  200. =cut
  201. sub get {
  202. my ($self, %crit) = @_;
  203. return map { $self->{items}{$_} } $self->get_keys(%crit);
  204. }
  205. =item $index->remove_by_key($key)
  206. Remove the item identified by the given key.
  207. =cut
  208. sub remove_by_key {
  209. my ($self, $key) = @_;
  210. @{$self->{order}} = grep { $_ ne $key } @{$self->{order}};
  211. return delete $self->{items}{$key};
  212. }
  213. =item @items = $index->remove(%criteria)
  214. Returns and removes all the items that matches all the criteria.
  215. =cut
  216. sub remove {
  217. my ($self, %crit) = @_;
  218. my @keys = $self->get_keys(%crit);
  219. my (%keys, @ret);
  220. foreach my $key (@keys) {
  221. $keys{$key} = 1;
  222. push @ret, $self->{items}{$key} if defined wantarray;
  223. delete $self->{items}{$key};
  224. }
  225. @{$self->{order}} = grep { not exists $keys{$_} } @{$self->{order}};
  226. return @ret;
  227. }
  228. =item $index->merge($other_index, %opts)
  229. Merge the entries of the other index. While merging, the keys of the merged
  230. index are used, they are not re-computed (unless you have set the options
  231. "keep_keys" to "0"). It's your responsibility to ensure that they have been
  232. computed with the same function.
  233. =cut
  234. sub merge {
  235. my ($self, $other, %opts) = @_;
  236. $opts{keep_keys} //= 1;
  237. foreach my $key ($other->get_keys()) {
  238. $self->add($other->get_by_key($key), $opts{keep_keys} ? $key : undef);
  239. }
  240. }
  241. =item $index->sort(\&sortfunc)
  242. Sort the index with the given sort function. If no function is given, an
  243. alphabetic sort is done based on the keys. The sort function receives the
  244. items themselves as parameters and not the keys.
  245. =cut
  246. sub sort {
  247. my ($self, $func) = @_;
  248. if (defined $func) {
  249. @{$self->{order}} = sort {
  250. &$func($self->{items}{$a}, $self->{items}{$b})
  251. } @{$self->{order}};
  252. } else {
  253. @{$self->{order}} = sort @{$self->{order}};
  254. }
  255. }
  256. =item $str = $index->output()
  257. =item "$index"
  258. Get a string representation of the index. The Dpkg::Control objects are
  259. output in the order which they have been read or added except if the order
  260. have been changed with sort().
  261. =item $index->output($fh)
  262. Print the string representation of the index to a filehandle.
  263. =cut
  264. sub output {
  265. my ($self, $fh) = @_;
  266. my $str = '';
  267. foreach my $key ($self->get_keys()) {
  268. if (defined $fh) {
  269. print { $fh } $self->get_by_key($key) . "\n";
  270. }
  271. if (defined wantarray) {
  272. $str .= $self->get_by_key($key) . "\n";
  273. }
  274. }
  275. return $str;
  276. }
  277. =back
  278. =head1 CHANGES
  279. =head2 Version 1.00 (dpkg 1.15.6)
  280. Mark the module as public.
  281. =cut
  282. 1;